low_level_alloc_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <thread> // NOLINT(build/c++11)
21 #include <unordered_map>
22 #include <utility>
23 
24 namespace absl {
25 namespace base_internal {
26 namespace {
27 
28 // This test doesn't use gtest since it needs to test that everything
29 // works before main().
30 #define TEST_ASSERT(x) \
31  if (!(x)) { \
32  printf("TEST_ASSERT(%s) FAILED ON LINE %d\n", #x, __LINE__); \
33  abort(); \
34  }
35 
36 // a block of memory obtained from the allocator
37 struct BlockDesc {
38  char *ptr; // pointer to memory
39  int len; // number of bytes
40  int fill; // filled with data starting with this
41 };
42 
43 // Check that the pattern placed in the block d
44 // by RandomizeBlockDesc is still there.
45 static void CheckBlockDesc(const BlockDesc &d) {
46  for (int i = 0; i != d.len; i++) {
47  TEST_ASSERT((d.ptr[i] & 0xff) == ((d.fill + i) & 0xff));
48  }
49 }
50 
51 // Fill the block "*d" with a pattern
52 // starting with a random byte.
53 static void RandomizeBlockDesc(BlockDesc *d) {
54  d->fill = rand() & 0xff;
55  for (int i = 0; i != d->len; i++) {
56  d->ptr[i] = (d->fill + i) & 0xff;
57  }
58 }
59 
60 // Use to indicate to the malloc hooks that
61 // this calls is from LowLevelAlloc.
62 static bool using_low_level_alloc = false;
63 
64 // n times, toss a coin, and based on the outcome
65 // either allocate a new block or deallocate an old block.
66 // New blocks are placed in a std::unordered_map with a random key
67 // and initialized with RandomizeBlockDesc().
68 // If keys conflict, the older block is freed.
69 // Old blocks are always checked with CheckBlockDesc()
70 // before being freed. At the end of the run,
71 // all remaining allocated blocks are freed.
72 // If use_new_arena is true, use a fresh arena, and then delete it.
73 // If call_malloc_hook is true and user_arena is true,
74 // allocations and deallocations are reported via the MallocHook
75 // interface.
76 static void Test(bool use_new_arena, bool call_malloc_hook, int n) {
77  typedef std::unordered_map<int, BlockDesc> AllocMap;
78  AllocMap allocated;
79  AllocMap::iterator it;
80  BlockDesc block_desc;
81  int rnd;
82  LowLevelAlloc::Arena *arena = 0;
83  if (use_new_arena) {
84  int32_t flags = call_malloc_hook ? LowLevelAlloc::kCallMallocHook : 0;
85  arena = LowLevelAlloc::NewArena(flags);
86  }
87  for (int i = 0; i != n; i++) {
88  if (i != 0 && i % 10000 == 0) {
89  printf(".");
90  fflush(stdout);
91  }
92 
93  switch (rand() & 1) { // toss a coin
94  case 0: // coin came up heads: add a block
95  using_low_level_alloc = true;
96  block_desc.len = rand() & 0x3fff;
97  block_desc.ptr =
98  reinterpret_cast<char *>(
99  arena == 0
100  ? LowLevelAlloc::Alloc(block_desc.len)
101  : LowLevelAlloc::AllocWithArena(block_desc.len, arena));
102  using_low_level_alloc = false;
103  RandomizeBlockDesc(&block_desc);
104  rnd = rand();
105  it = allocated.find(rnd);
106  if (it != allocated.end()) {
107  CheckBlockDesc(it->second);
108  using_low_level_alloc = true;
109  LowLevelAlloc::Free(it->second.ptr);
110  using_low_level_alloc = false;
111  it->second = block_desc;
112  } else {
113  allocated[rnd] = block_desc;
114  }
115  break;
116  case 1: // coin came up tails: remove a block
117  it = allocated.begin();
118  if (it != allocated.end()) {
119  CheckBlockDesc(it->second);
120  using_low_level_alloc = true;
121  LowLevelAlloc::Free(it->second.ptr);
122  using_low_level_alloc = false;
123  allocated.erase(it);
124  }
125  break;
126  }
127  }
128  // remove all remaining blocks
129  while ((it = allocated.begin()) != allocated.end()) {
130  CheckBlockDesc(it->second);
131  using_low_level_alloc = true;
132  LowLevelAlloc::Free(it->second.ptr);
133  using_low_level_alloc = false;
134  allocated.erase(it);
135  }
136  if (use_new_arena) {
138  }
139 }
140 
141 // LowLevelAlloc is designed to be safe to call before main().
142 static struct BeforeMain {
143  BeforeMain() {
144  Test(false, false, 50000);
145  Test(true, false, 50000);
146  Test(true, true, 50000);
147  }
148 } before_main;
149 
150 } // namespace
151 } // namespace base_internal
152 } // namespace absl
153 
154 int main(int argc, char *argv[]) {
155  // The actual test runs in the global constructor of `before_main`.
156  printf("PASS\n");
157  return 0;
158 }
int main(int argc, char *argv[])
int fill
static void * Alloc(size_t request) ABSL_ATTRIBUTE_SECTION(malloc_hook)
Definition: algorithm.h:29
char * ptr
#define TEST_ASSERT(x)
LowLevelAlloc::Arena * arena
static void Free(void *s) ABSL_ATTRIBUTE_SECTION(malloc_hook)
static void * AllocWithArena(size_t request, Arena *arena) ABSL_ATTRIBUTE_SECTION(malloc_hook)
static Arena * NewArena(int32_t flags)
static bool DeleteArena(Arena *arena)


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:19