/* Simple randomized test program for kmregion.
kmregion version 3.5, a region-based memory allocator
Copyright (C) 02021 Kragen Javier Sitaker
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
This program allocates a bunch of blocks, fills them up with
characters, and then deallocates them. By default, the sizes are
distributed with a uniform random distribution from 1 byte up to
16 KiB, so about half of them will be “large allocations” by
kmregion’s definition, and the other half will not. Each region gets
10000 allocations, which should be about 80 megs, before getting
deallocated. The program as a whole allocates 100 such regions, so
it writes about 8 gigs to memory.
The program itself only does very minimal correctness testing; the
intent is to run it under valgrind or something so that errors can be
detected.
*/
#include
#include
#include
#include
#include "kmregion.h"
#include
#include
#include
#include
int
main(int argc, char **argv)
{
size_t seed = (argc > 1) ? atoi(argv[1]) : getpid(),
sizebits = (argc > 2) ? atoi(argv[2]) : 14,
mask = (1UL << sizebits) - 1;
printf("%s %d %d; # %llu\n", argv[0], (int)seed, (int)sizebits, (unsigned long long)mask);
jmp_buf err;
if (setjmp(err)) {
fprintf(stderr, "Allocation error\n");
return 1;
}
for (size_t i = 0; i != 100; i++) {
kmregion *p = km_start(km_libc_disc, &err);
if (!p) abort();
for (size_t j = 0; j != 10000; j++) {
/* LCG from Newlib and MUSL, according to
* */
seed = (seed + 1) * 6364136223846793005UL;
size_t n = 1 + (seed & mask);
/* printf("km_new(p, %llu)\n", (unsigned long long)n); */
char *q = km_new(p, n);
if ((uintptr_t)q & 3) abort(); /* Minimal alignment check */
for (size_t k = 0; k != n; k++) q[k] = '!';
}
km_end(p);
}
return 0;
}