/* Simple 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 .
*/
#include
#include
#include
#include
#include "kmregion.h"
#include
#include
int
main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "Usage: %s 1000\n(for 5 million allocations)\n", argv[0]);
return 1;
}
size_t n = atoi(argv[1]);
/* Using longjmp error handling improves performance about 1%. */
jmp_buf err;
if (setjmp(err)) {
fprintf(stderr, "Allocation error\n");
return 1;
}
for (size_t i = 0; i < n; i++) {
kmregion *p = km_start(km_libc_disc, &err);
if (!p) abort();
struct n { int i; struct n *next; } *list = NULL;
for (size_t j = 0; j < 5000; j++) {
struct n *q = km_new(p, sizeof(*q));
q->i = j;
q->next = list;
list = q;
}
/* GCC was optimizing out the actual initialization of the list
* nodes! */
if (i == 23) {
size_t total = 0;
struct n *q = list;
while (q) {
total += q->i;
q = q->next;
}
printf("Total: %d\n", (int)total);
}
km_end(p);
}
return 0;
}