Commit | Line | Data |
---|---|---|
fbc2792b JA |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <assert.h> | |
4 | ||
5 | #include "../smalloc.h" | |
6 | #include "../flist.h" | |
71471cb1 | 7 | #include "../arch/arch.h" |
9077ee4a | 8 | #include "debug.h" |
fbc2792b JA |
9 | |
10 | #define MAGIC1 0xa9b1c8d2 | |
11 | #define MAGIC2 0xf0a1e9b3 | |
12 | ||
13 | #define LOOPS 32 | |
6cc38ac0 VF |
14 | #define MAXSMALLOC 120*1024*1024UL |
15 | #define LARGESMALLOC 128*1024U | |
fbc2792b JA |
16 | |
17 | struct elem { | |
18 | unsigned int magic1; | |
19 | struct flist_head list; | |
20 | unsigned int magic2; | |
6cc38ac0 | 21 | unsigned int size; |
fbc2792b JA |
22 | }; |
23 | ||
ca0a0b52 | 24 | static FLIST_HEAD(list); |
fbc2792b JA |
25 | |
26 | static int do_rand_allocs(void) | |
27 | { | |
28 | unsigned int size, nr, rounds = 0; | |
29 | unsigned long total; | |
30 | struct elem *e; | |
6cc38ac0 | 31 | bool error; |
fbc2792b JA |
32 | |
33 | while (rounds++ < LOOPS) { | |
34 | #ifdef STEST_SEED | |
35 | srand(MAGIC1); | |
36 | #endif | |
6cc38ac0 | 37 | error = false; |
fbc2792b | 38 | nr = total = 0; |
6cc38ac0 | 39 | while (total < MAXSMALLOC) { |
fbc2792b JA |
40 | size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0))); |
41 | e = smalloc(size); | |
42 | if (!e) { | |
43 | printf("fail at %lu, size %u\n", total, size); | |
44 | break; | |
45 | } | |
46 | e->magic1 = MAGIC1; | |
47 | e->magic2 = MAGIC2; | |
6cc38ac0 | 48 | e->size = size; |
fbc2792b JA |
49 | total += size; |
50 | flist_add_tail(&e->list, &list); | |
51 | nr++; | |
52 | } | |
53 | ||
54 | printf("Got items: %u\n", nr); | |
55 | ||
56 | while (!flist_empty(&list)) { | |
57 | e = flist_entry(list.next, struct elem, list); | |
58 | assert(e->magic1 == MAGIC1); | |
59 | assert(e->magic2 == MAGIC2); | |
6cc38ac0 | 60 | total -= e->size; |
fbc2792b JA |
61 | flist_del(&e->list); |
62 | sfree(e); | |
6cc38ac0 VF |
63 | |
64 | if (!error) { | |
65 | e = smalloc(LARGESMALLOC); | |
66 | if (!e) { | |
67 | error = true; | |
68 | printf("failure allocating %u bytes at %lu allocated during sfree phase\n", | |
69 | LARGESMALLOC, total); | |
70 | } | |
71 | else | |
72 | sfree(e); | |
73 | } | |
fbc2792b JA |
74 | } |
75 | } | |
76 | ||
77 | return 0; | |
78 | } | |
79 | ||
fbc2792b JA |
80 | int main(int argc, char *argv[]) |
81 | { | |
71471cb1 | 82 | arch_init(argv); |
fbc2792b | 83 | sinit(); |
9077ee4a | 84 | debug_init(); |
fbc2792b JA |
85 | |
86 | do_rand_allocs(); | |
6cc38ac0 | 87 | smalloc_debug(0); /* free and total blocks should match */ |
fbc2792b | 88 | |
fbc2792b JA |
89 | scleanup(); |
90 | return 0; | |
91 | } |