#define ALLN 1000000
void* allocations[ALLN] = {0};
size_t max_alloc = 1;
void insert_alloc(void* alloc) {
for (size_t i=0; i<ALLN; i++) {
if (!allocations[i]) {
allocations[i] = alloc;
if (i > max_alloc) {
max_alloc = i;
}
return;
}
}
}
#define ITERATIONS 100000
int test_with_random() {
srand(time(0));
for (int iteration=0; iteration<ITERATIONS; iteration++) {
int op = (rand() % 10) > 4 ? 0 : 1; // mmalloc : mfree
int len = (rand() % 10) > 8 ? 0 : 1; // large : small
if (op == 0) {
size_t alen = rand() % (len == 0 ? 1000000 : 1000);
void* alloc = mmalloc(alen);
// printf("mmalloc(%zu) -> %p\n", alen, alloc);
if (alloc)
memset(alloc, 'a', alen);
insert_alloc(alloc);
}
if (op == 1) {
size_t allocations_ix = rand() % max_alloc;
void* to_free = allocations[allocations_ix];
// printf("mfree(%p)\n", to_free);
mfree(to_free);
allocations[allocations_ix] = NULL;
}
}
print_pool();
summarize_pool();
}
int test_with_specific() {
void* alloc = mmalloc((1<<24) - sizeof(mregion));
print_pool();
summarize_pool();
mfree(alloc);
print_pool();
summarize_pool();
}
int main() {
pool = malloc(POOL_LENGTH);
region_0 = pool;
initialize();
// test_with_specific();
test_with_random();
}
Comments