Unify process_alloc() and process_realloc() with PROCESS_ALLOC_MODE_REALLOC bit
[moveable_pool.git] / pool_test_gen.c
1 #include <assert.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "rassert.h"
7
8 int rand_int(int n) {
9   assert(sizeof(long) > sizeof(int));
10   return (int)((long)rand() * n / (RAND_MAX + 1L));
11 }
12
13 int main(int argc, char **argv) {
14   if (argc < 5) {
15     printf("usage: %s n_items pool_size n_events item_size [do_base [seed]]\n", argv[0]);
16     exit(EXIT_FAILURE);
17   }
18   int n_items = atoi(argv[1]);
19   int pool_size = atoi(argv[2]);
20   int n_events = atoi(argv[3]);
21   int item_size = atoi(argv[4]);
22   bool do_base = argc >= 6 ? strcmp(argv[5], "false") != 0 : false;
23   int seed = argc >= 7 ? atoi(argv[6]) : 1;
24
25   int *items = malloc(n_items * sizeof(int));
26   rassert(items);
27   memset(items, -1, n_items * sizeof(int));
28
29   srand(seed);
30   int pool_used = 0;
31   for (int i = 0; i < n_events; ++i) {
32     int item = rand_int(n_items);
33     int old_size = items[item];
34     if (old_size == -1) {
35       int size = rand_int(item_size + 1);
36       bool success = pool_used + size <= pool_size;
37       printf(
38         "alloc %d %d %s\n",
39         item,
40         size,
41         success ? "true" : "false"
42       );
43       if (success) {
44         items[item] = size;
45         pool_used += size;
46       }
47     }
48     else if (rand_int(4)) {
49       int size = rand_int(item_size + 1);
50       bool success = pool_used + size - old_size <= pool_size;
51       printf(
52         "realloc%s %d %d %d %s\n",
53         do_base && rand_int(3) == 0 ? "_base" : "",
54         item,
55         old_size,
56         size,
57         success ? "true" : "false"
58       );
59       if (success) {
60         items[item] = size;
61         pool_used += size - old_size;
62       }
63     }
64     else {
65       printf("free %d %d\n", item, old_size);
66       items[item] = -1;
67       pool_used -= old_size;
68     }
69   }
70
71   return 0;
72 }