1 //===- llvm/unittest/Support/AllocatorTest.cpp - BumpPtrAllocator tests ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/Allocator.h"
10 #include "gtest/gtest.h"
17 TEST(AllocatorTest
, Basics
) {
18 BumpPtrAllocator Alloc
;
19 int *a
= (int*)Alloc
.Allocate(sizeof(int), alignof(int));
20 int *b
= (int*)Alloc
.Allocate(sizeof(int) * 10, alignof(int));
21 int *c
= (int*)Alloc
.Allocate(sizeof(int), alignof(int));
30 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
32 BumpPtrAllocator Alloc2
= std::move(Alloc
);
33 EXPECT_EQ(0U, Alloc
.GetNumSlabs());
34 EXPECT_EQ(1U, Alloc2
.GetNumSlabs());
36 // Make sure the old pointers still work. These are especially interesting
37 // under ASan or Valgrind.
43 Alloc
= std::move(Alloc2
);
44 EXPECT_EQ(0U, Alloc2
.GetNumSlabs());
45 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
48 // Allocate enough bytes to create three slabs.
49 TEST(AllocatorTest
, ThreeSlabs
) {
50 BumpPtrAllocator Alloc
;
51 Alloc
.Allocate(3000, 1);
52 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
53 Alloc
.Allocate(3000, 1);
54 EXPECT_EQ(2U, Alloc
.GetNumSlabs());
55 Alloc
.Allocate(3000, 1);
56 EXPECT_EQ(3U, Alloc
.GetNumSlabs());
59 // Allocate enough bytes to create two slabs, reset the allocator, and do it
61 TEST(AllocatorTest
, TestReset
) {
62 BumpPtrAllocator Alloc
;
64 // Allocate something larger than the SizeThreshold=4096.
65 (void)Alloc
.Allocate(5000, 1);
67 // Calling Reset should free all CustomSizedSlabs.
68 EXPECT_EQ(0u, Alloc
.GetNumSlabs());
70 Alloc
.Allocate(3000, 1);
71 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
72 Alloc
.Allocate(3000, 1);
73 EXPECT_EQ(2U, Alloc
.GetNumSlabs());
75 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
76 Alloc
.Allocate(3000, 1);
77 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
78 Alloc
.Allocate(3000, 1);
79 EXPECT_EQ(2U, Alloc
.GetNumSlabs());
82 // Test some allocations at varying alignments.
83 TEST(AllocatorTest
, TestAlignment
) {
84 BumpPtrAllocator Alloc
;
86 a
= (uintptr_t)Alloc
.Allocate(1, 2);
88 a
= (uintptr_t)Alloc
.Allocate(1, 4);
90 a
= (uintptr_t)Alloc
.Allocate(1, 8);
92 a
= (uintptr_t)Alloc
.Allocate(1, 16);
93 EXPECT_EQ(0U, a
& 15);
94 a
= (uintptr_t)Alloc
.Allocate(1, 32);
95 EXPECT_EQ(0U, a
& 31);
96 a
= (uintptr_t)Alloc
.Allocate(1, 64);
97 EXPECT_EQ(0U, a
& 63);
98 a
= (uintptr_t)Alloc
.Allocate(1, 128);
99 EXPECT_EQ(0U, a
& 127);
102 // Test allocating just over the slab size. This tests a bug where before the
103 // allocator incorrectly calculated the buffer end pointer.
104 TEST(AllocatorTest
, TestOverflow
) {
105 BumpPtrAllocator Alloc
;
107 // Fill the slab right up until the end pointer.
108 Alloc
.Allocate(4096, 1);
109 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
111 // If we don't allocate a new slab, then we will have overflowed.
112 Alloc
.Allocate(1, 1);
113 EXPECT_EQ(2U, Alloc
.GetNumSlabs());
116 // Test allocating with a size larger than the initial slab size.
117 TEST(AllocatorTest
, TestSmallSlabSize
) {
118 BumpPtrAllocator Alloc
;
120 Alloc
.Allocate(8000, 1);
121 EXPECT_EQ(1U, Alloc
.GetNumSlabs());
124 // Test requesting alignment that goes past the end of the current slab.
125 TEST(AllocatorTest
, TestAlignmentPastSlab
) {
126 BumpPtrAllocator Alloc
;
127 Alloc
.Allocate(4095, 1);
129 // Aligning the current slab pointer is likely to move it past the end of the
130 // slab, which would confuse any unsigned comparisons with the difference of
131 // the end pointer and the aligned pointer.
132 Alloc
.Allocate(1024, 8192);
134 EXPECT_EQ(2U, Alloc
.GetNumSlabs());
137 // Test allocating with a decreased growth delay.
138 TEST(AllocatorTest
, TestFasterSlabGrowthDelay
) {
139 const size_t SlabSize
= 4096;
140 // Decrease the growth delay to double the slab size every slab.
141 const size_t GrowthDelay
= 1;
142 BumpPtrAllocatorImpl
<MallocAllocator
, SlabSize
, SlabSize
, GrowthDelay
> Alloc
;
143 // Disable the red zone for this test. The additional bytes allocated for the
144 // red zone would change the allocation numbers we check below.
145 Alloc
.setRedZoneSize(0);
147 Alloc
.Allocate(SlabSize
, 1);
148 EXPECT_EQ(SlabSize
, Alloc
.getTotalMemory());
149 // We hit our growth delay with the previous allocation so the next
150 // allocation should get a twice as large slab.
151 Alloc
.Allocate(SlabSize
, 1);
152 EXPECT_EQ(SlabSize
* 3, Alloc
.getTotalMemory());
153 Alloc
.Allocate(SlabSize
, 1);
154 EXPECT_EQ(SlabSize
* 3, Alloc
.getTotalMemory());
156 // Both slabs are full again and hit the growth delay again, so the
157 // next allocation should again get a slab with four times the size of the
158 // original slab size. In total we now should have a memory size of:
159 // 1 + 2 + 4 * SlabSize.
160 Alloc
.Allocate(SlabSize
, 1);
161 EXPECT_EQ(SlabSize
* 7, Alloc
.getTotalMemory());
164 // Test allocating with a increased growth delay.
165 TEST(AllocatorTest
, TestSlowerSlabGrowthDelay
) {
166 const size_t SlabSize
= 16;
167 // Increase the growth delay to only double the slab size every 256 slabs.
168 const size_t GrowthDelay
= 256;
169 BumpPtrAllocatorImpl
<MallocAllocator
, SlabSize
, SlabSize
, GrowthDelay
> Alloc
;
170 // Disable the red zone for this test. The additional bytes allocated for the
171 // red zone would change the allocation numbers we check below.
172 Alloc
.setRedZoneSize(0);
174 // Allocate 256 slabs. We should keep getting slabs with the original size
175 // as we haven't hit our growth delay on the last allocation.
176 for (std::size_t i
= 0; i
< GrowthDelay
; ++i
)
177 Alloc
.Allocate(SlabSize
, 1);
178 EXPECT_EQ(SlabSize
* GrowthDelay
, Alloc
.getTotalMemory());
179 // Allocate another slab. This time we should get another slab allocated
180 // that is twice as large as the normal slab size.
181 Alloc
.Allocate(SlabSize
, 1);
182 EXPECT_EQ(SlabSize
* GrowthDelay
+ SlabSize
* 2, Alloc
.getTotalMemory());
185 // Mock slab allocator that returns slabs aligned on 4096 bytes. There is no
186 // easy portable way to do this, so this is kind of a hack.
187 class MockSlabAllocator
{
188 static size_t LastSlabSize
;
191 ~MockSlabAllocator() { }
193 void *Allocate(size_t Size
, size_t /*Alignment*/) {
194 // Allocate space for the alignment, the slab, and a void* that goes right
196 Align
Alignment(4096);
197 void *MemBase
= safe_malloc(Size
+ Alignment
.value() - 1 + sizeof(void *));
199 // Find the slab start.
200 void *Slab
= (void *)alignAddr((char*)MemBase
+ sizeof(void *), Alignment
);
202 // Hold a pointer to the base so we can free the whole malloced block.
203 ((void**)Slab
)[-1] = MemBase
;
209 void Deallocate(void *Slab
, size_t /*Size*/, size_t /*Alignment*/) {
210 free(((void**)Slab
)[-1]);
213 static size_t GetLastSlabSize() { return LastSlabSize
; }
216 size_t MockSlabAllocator::LastSlabSize
= 0;
218 // Allocate a large-ish block with a really large alignment so that the
219 // allocator will think that it has space, but after it does the alignment it
221 TEST(AllocatorTest
, TestBigAlignment
) {
222 BumpPtrAllocatorImpl
<MockSlabAllocator
> Alloc
;
224 // First allocate a tiny bit to ensure we have to re-align things.
225 (void)Alloc
.Allocate(1, 1);
227 // Now the big chunk with a big alignment.
228 (void)Alloc
.Allocate(3000, 2048);
230 // We test that the last slab size is not the default 4096 byte slab, but
231 // rather a custom sized slab that is larger.
232 EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u);
235 } // anonymous namespace