llvm.org GIT mirror llvm / 06de8a1
BumpPtrAllocator: don't accept 0 for the alignment parameter It seems unnecessary to have to use an extra branch to check for this special case. http://reviews.llvm.org/D4945 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216036 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 6 years ago
3 changed file(s) with 17 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
200200
201201 /// \brief Allocate space at the specified alignment.
202202 void *Allocate(size_t Size, size_t Alignment) {
203 assert(Alignment > 0 && "0-byte alignnment is not allowed. Use 1 instead.");
204
203205 // Keep track of how many bytes we've allocated.
204206 BytesAllocated += Size;
205
206 // 0-byte alignment means 1-byte alignment.
207 if (Alignment == 0)
208 Alignment = 1;
209207
210208 // Allocate the aligned space, going forwards from CurPtr.
211209 char *Ptr = alignPtr(CurPtr, Alignment);
157157 TEST(JITMemoryManagerTest, TestSmallGlobalInts) {
158158 std::unique_ptr MemMgr(
159159 JITMemoryManager::CreateDefaultMemManager());
160 uint8_t *a = (uint8_t *)MemMgr->allocateGlobal(8, 0);
160 uint8_t *a = (uint8_t *)MemMgr->allocateGlobal(8, 1);
161161 uint16_t *b = (uint16_t*)MemMgr->allocateGlobal(16, 2);
162162 uint32_t *c = (uint32_t*)MemMgr->allocateGlobal(32, 4);
163163 uint64_t *d = (uint64_t*)MemMgr->allocateGlobal(64, 8);
1616
1717 TEST(AllocatorTest, Basics) {
1818 BumpPtrAllocator Alloc;
19 int *a = (int*)Alloc.Allocate(sizeof(int), 0);
20 int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 0);
21 int *c = (int*)Alloc.Allocate(sizeof(int), 0);
19 int *a = (int*)Alloc.Allocate(sizeof(int), 1);
20 int *b = (int*)Alloc.Allocate(sizeof(int) * 10, 1);
21 int *c = (int*)Alloc.Allocate(sizeof(int), 1);
2222 *a = 1;
2323 b[0] = 2;
2424 b[9] = 2;
4848 // Allocate enough bytes to create three slabs.
4949 TEST(AllocatorTest, ThreeSlabs) {
5050 BumpPtrAllocator Alloc;
51 Alloc.Allocate(3000, 0);
51 Alloc.Allocate(3000, 1);
5252 EXPECT_EQ(1U, Alloc.GetNumSlabs());
53 Alloc.Allocate(3000, 0);
53 Alloc.Allocate(3000, 1);
5454 EXPECT_EQ(2U, Alloc.GetNumSlabs());
55 Alloc.Allocate(3000, 0);
55 Alloc.Allocate(3000, 1);
5656 EXPECT_EQ(3U, Alloc.GetNumSlabs());
5757 }
5858
6060 // again.
6161 TEST(AllocatorTest, TestReset) {
6262 BumpPtrAllocator Alloc;
63 Alloc.Allocate(3000, 0);
63 Alloc.Allocate(3000, 1);
6464 EXPECT_EQ(1U, Alloc.GetNumSlabs());
65 Alloc.Allocate(3000, 0);
65 Alloc.Allocate(3000, 1);
6666 EXPECT_EQ(2U, Alloc.GetNumSlabs());
6767 Alloc.Reset();
6868 EXPECT_EQ(1U, Alloc.GetNumSlabs());
69 Alloc.Allocate(3000, 0);
69 Alloc.Allocate(3000, 1);
7070 EXPECT_EQ(1U, Alloc.GetNumSlabs());
71 Alloc.Allocate(3000, 0);
71 Alloc.Allocate(3000, 1);
7272 EXPECT_EQ(2U, Alloc.GetNumSlabs());
7373 }
7474
9898 BumpPtrAllocator Alloc;
9999
100100 // Fill the slab right up until the end pointer.
101 Alloc.Allocate(4096, 0);
101 Alloc.Allocate(4096, 1);
102102 EXPECT_EQ(1U, Alloc.GetNumSlabs());
103103
104104 // If we don't allocate a new slab, then we will have overflowed.
105 Alloc.Allocate(1, 0);
105 Alloc.Allocate(1, 1);
106106 EXPECT_EQ(2U, Alloc.GetNumSlabs());
107107 }
108108
110110 TEST(AllocatorTest, TestSmallSlabSize) {
111111 BumpPtrAllocator Alloc;
112112
113 Alloc.Allocate(8000, 0);
113 Alloc.Allocate(8000, 1);
114114 EXPECT_EQ(1U, Alloc.GetNumSlabs());
115115 }
116116
154154 BumpPtrAllocatorImpl Alloc;
155155
156156 // First allocate a tiny bit to ensure we have to re-align things.
157 (void)Alloc.Allocate(1, 0);
157 (void)Alloc.Allocate(1, 1);
158158
159159 // Now the big chunk with a big alignment.
160160 (void)Alloc.Allocate(3000, 2048);