blob: a0223ea932b0b83fd1e35516dd5bbce7d7870f38 [file] [log] [blame]
Reid Klecknerc2d882d2009-07-23 18:34:13 +00001//===- llvm/unittest/Support/AllocatorTest.cpp - BumpPtrAllocator tests ---===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Reid Klecknerc2d882d2009-07-23 18:34:13 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/Allocator.h"
Reid Klecknerc2d882d2009-07-23 18:34:13 +000010#include "gtest/gtest.h"
Reid Kleckner4b1f2f42009-07-25 21:26:02 +000011#include <cstdlib>
Reid Klecknerc2d882d2009-07-23 18:34:13 +000012
13using namespace llvm;
14
15namespace {
16
17TEST(AllocatorTest, Basics) {
18 BumpPtrAllocator Alloc;
Jordan Roseca9ca542017-03-11 01:24:56 +000019 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));
Reid Klecknerc2d882d2009-07-23 18:34:13 +000022 *a = 1;
23 b[0] = 2;
24 b[9] = 2;
25 *c = 3;
26 EXPECT_EQ(1, *a);
27 EXPECT_EQ(2, b[0]);
28 EXPECT_EQ(2, b[9]);
29 EXPECT_EQ(3, *c);
30 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Chandler Carruth0e31ed92014-04-16 10:48:27 +000031
32 BumpPtrAllocator Alloc2 = std::move(Alloc);
33 EXPECT_EQ(0U, Alloc.GetNumSlabs());
34 EXPECT_EQ(1U, Alloc2.GetNumSlabs());
35
36 // Make sure the old pointers still work. These are especially interesting
37 // under ASan or Valgrind.
38 EXPECT_EQ(1, *a);
39 EXPECT_EQ(2, b[0]);
40 EXPECT_EQ(2, b[9]);
41 EXPECT_EQ(3, *c);
42
43 Alloc = std::move(Alloc2);
44 EXPECT_EQ(0U, Alloc2.GetNumSlabs());
45 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Reid Klecknerc2d882d2009-07-23 18:34:13 +000046}
47
48// Allocate enough bytes to create three slabs.
49TEST(AllocatorTest, ThreeSlabs) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000050 BumpPtrAllocator Alloc;
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000051 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000052 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000053 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000054 EXPECT_EQ(2U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000055 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000056 EXPECT_EQ(3U, Alloc.GetNumSlabs());
57}
58
59// Allocate enough bytes to create two slabs, reset the allocator, and do it
60// again.
61TEST(AllocatorTest, TestReset) {
Chandler Carrutha05a2212014-03-30 11:36:32 +000062 BumpPtrAllocator Alloc;
Hans Wennborgda5ddff72015-05-18 16:54:17 +000063
64 // Allocate something larger than the SizeThreshold=4096.
65 (void)Alloc.Allocate(5000, 1);
66 Alloc.Reset();
67 // Calling Reset should free all CustomSizedSlabs.
68 EXPECT_EQ(0u, Alloc.GetNumSlabs());
69
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000070 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000071 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000072 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000073 EXPECT_EQ(2U, Alloc.GetNumSlabs());
74 Alloc.Reset();
75 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000076 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000077 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Hans Wennborgfd1f0f12014-08-19 23:35:33 +000078 Alloc.Allocate(3000, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +000079 EXPECT_EQ(2U, Alloc.GetNumSlabs());
80}
81
82// Test some allocations at varying alignments.
83TEST(AllocatorTest, TestAlignment) {
84 BumpPtrAllocator Alloc;
85 uintptr_t a;
86 a = (uintptr_t)Alloc.Allocate(1, 2);
87 EXPECT_EQ(0U, a & 1);
88 a = (uintptr_t)Alloc.Allocate(1, 4);
89 EXPECT_EQ(0U, a & 3);
90 a = (uintptr_t)Alloc.Allocate(1, 8);
91 EXPECT_EQ(0U, a & 7);
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);
100}
101
102// Test allocating just over the slab size. This tests a bug where before the
103// allocator incorrectly calculated the buffer end pointer.
104TEST(AllocatorTest, TestOverflow) {
Chandler Carrutha05a2212014-03-30 11:36:32 +0000105 BumpPtrAllocator Alloc;
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000106
107 // Fill the slab right up until the end pointer.
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000108 Alloc.Allocate(4096, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000109 EXPECT_EQ(1U, Alloc.GetNumSlabs());
110
Dan Gohman01b443f2010-03-01 17:51:02 +0000111 // If we don't allocate a new slab, then we will have overflowed.
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000112 Alloc.Allocate(1, 1);
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000113 EXPECT_EQ(2U, Alloc.GetNumSlabs());
114}
115
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +0000116// Test allocating with a size larger than the initial slab size.
117TEST(AllocatorTest, TestSmallSlabSize) {
Chandler Carrutha05a2212014-03-30 11:36:32 +0000118 BumpPtrAllocator Alloc;
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +0000119
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000120 Alloc.Allocate(8000, 1);
Hans Wennborgd47b1d72014-08-17 18:31:18 +0000121 EXPECT_EQ(1U, Alloc.GetNumSlabs());
Argyrios Kyrtzidis16558f42012-03-01 20:36:32 +0000122}
123
Hans Wennborg44e27462014-09-07 04:24:31 +0000124// Test requesting alignment that goes past the end of the current slab.
125TEST(AllocatorTest, TestAlignmentPastSlab) {
126 BumpPtrAllocator Alloc;
Hans Wennborge5a96a52014-09-07 05:14:29 +0000127 Alloc.Allocate(4095, 1);
Hans Wennborg44e27462014-09-07 04:24:31 +0000128
Hans Wennborge5a96a52014-09-07 05:14:29 +0000129 // 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
Eric Christopher572e03a2015-06-19 01:53:21 +0000131 // the end pointer and the aligned pointer.
Hans Wennborg44e27462014-09-07 04:24:31 +0000132 Alloc.Allocate(1024, 8192);
133
134 EXPECT_EQ(2U, Alloc.GetNumSlabs());
135}
136
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000137// Mock slab allocator that returns slabs aligned on 4096 bytes. There is no
138// easy portable way to do this, so this is kind of a hack.
Chandler Carrutheed34662014-04-14 05:11:27 +0000139class MockSlabAllocator {
140 static size_t LastSlabSize;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000141
142public:
Chandler Carrutheed34662014-04-14 05:11:27 +0000143 ~MockSlabAllocator() { }
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000144
Chandler Carruth785a9222014-04-15 09:44:09 +0000145 void *Allocate(size_t Size, size_t /*Alignment*/) {
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000146 // Allocate space for the alignment, the slab, and a void* that goes right
147 // before the slab.
148 size_t Alignment = 4096;
Serge Pavlov76d8cce2018-02-20 05:41:26 +0000149 void *MemBase = safe_malloc(Size + Alignment - 1 + sizeof(void*));
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000150
Chandler Carruthf5babf92014-04-14 03:55:11 +0000151 // Find the slab start.
Hans Wennborg3b84f592014-09-02 21:51:35 +0000152 void *Slab = (void *)alignAddr((char*)MemBase + sizeof(void *), Alignment);
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000153
154 // Hold a pointer to the base so we can free the whole malloced block.
155 ((void**)Slab)[-1] = MemBase;
156
Chandler Carruthf5babf92014-04-14 03:55:11 +0000157 LastSlabSize = Size;
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000158 return Slab;
159 }
160
Chandler Carrutheed34662014-04-14 05:11:27 +0000161 void Deallocate(void *Slab, size_t Size) {
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000162 free(((void**)Slab)[-1]);
163 }
164
Chandler Carrutheed34662014-04-14 05:11:27 +0000165 static size_t GetLastSlabSize() { return LastSlabSize; }
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000166};
167
Chandler Carrutheed34662014-04-14 05:11:27 +0000168size_t MockSlabAllocator::LastSlabSize = 0;
169
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000170// Allocate a large-ish block with a really large alignment so that the
171// allocator will think that it has space, but after it does the alignment it
172// will not.
173TEST(AllocatorTest, TestBigAlignment) {
Chandler Carrutheed34662014-04-14 05:11:27 +0000174 BumpPtrAllocatorImpl<MockSlabAllocator> Alloc;
Chandler Carruthf5babf92014-04-14 03:55:11 +0000175
176 // First allocate a tiny bit to ensure we have to re-align things.
Hans Wennborgfd1f0f12014-08-19 23:35:33 +0000177 (void)Alloc.Allocate(1, 1);
Chandler Carruthf5babf92014-04-14 03:55:11 +0000178
179 // Now the big chunk with a big alignment.
180 (void)Alloc.Allocate(3000, 2048);
181
182 // We test that the last slab size is not the default 4096 byte slab, but
183 // rather a custom sized slab that is larger.
Chandler Carrutheed34662014-04-14 05:11:27 +0000184 EXPECT_GT(MockSlabAllocator::GetLastSlabSize(), 4096u);
Reid Kleckner4b1f2f42009-07-25 21:26:02 +0000185}
186
Reid Klecknerc2d882d2009-07-23 18:34:13 +0000187} // anonymous namespace