15 #include "absl/base/internal/low_level_alloc.h"
21 #include <unordered_map>
25 #include <emscripten.h>
28 #include "absl/container/node_hash_map.h"
32 namespace base_internal {
37 #define TEST_ASSERT(x) \
39 printf("TEST_ASSERT(%s) FAILED ON LINE %d\n", #x, __LINE__); \
52 static void CheckBlockDesc(
const BlockDesc &d) {
53 for (
int i = 0;
i !=
d.len;
i++) {
60 static void RandomizeBlockDesc(BlockDesc *d) {
61 d->fill = rand() & 0xff;
62 for (
int i = 0;
i !=
d->len;
i++) {
63 d->ptr[
i] = (
d->fill +
i) & 0xff;
69 static bool using_low_level_alloc =
false;
83 static void Test(
bool use_new_arena,
bool call_malloc_hook,
int n) {
94 for (
int i = 0;
i !=
n;
i++) {
95 if (i != 0 && i % 10000 == 0) {
100 switch (rand() & 1) {
102 using_low_level_alloc =
true;
103 block_desc.len = rand() & 0x3fff;
104 block_desc.ptr =
reinterpret_cast<char *
>(
107 : LowLevelAlloc::AllocWithArena(block_desc.
len,
arena));
108 using_low_level_alloc =
false;
109 RandomizeBlockDesc(&block_desc);
111 it = allocated.find(rnd);
112 if (
it != allocated.end()) {
113 CheckBlockDesc(
it->second);
114 using_low_level_alloc =
true;
116 using_low_level_alloc =
false;
117 it->second = block_desc;
119 allocated[rnd] = block_desc;
123 it = allocated.begin();
124 if (
it != allocated.end()) {
125 CheckBlockDesc(
it->second);
126 using_low_level_alloc =
true;
128 using_low_level_alloc =
false;
135 while ((
it = allocated.begin()) != allocated.end()) {
136 CheckBlockDesc(
it->second);
137 using_low_level_alloc =
true;
139 using_low_level_alloc =
false;
148 static struct BeforeMain {
150 Test(
false,
false, 50000);
151 Test(
true,
false, 50000);
152 Test(
true,
true, 50000);
161 int main(
int argc,
char *argv[]) {
164 #ifdef __EMSCRIPTEN__
168 if (ENVIRONMENT_IS_WEB) {
169 if (typeof TEST_FINISH ===
'function') {
172 console.error(
'Attempted to exit with status ' + $0);
173 console.error(
'But TEST_FINSIHED is not a function.');