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 | { | |
c6783fc3 | 28 | unsigned int i, size, nr, rounds = 0, ret = 0; |
fbc2792b JA |
29 | unsigned long total; |
30 | struct elem *e; | |
6cc38ac0 | 31 | bool error; |
c6783fc3 | 32 | char *c; |
fbc2792b JA |
33 | |
34 | while (rounds++ < LOOPS) { | |
35 | #ifdef STEST_SEED | |
36 | srand(MAGIC1); | |
37 | #endif | |
6cc38ac0 | 38 | error = false; |
fbc2792b | 39 | nr = total = 0; |
6cc38ac0 | 40 | while (total < MAXSMALLOC) { |
fbc2792b | 41 | size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0))); |
c6783fc3 | 42 | e = scalloc(1, size); |
fbc2792b JA |
43 | if (!e) { |
44 | printf("fail at %lu, size %u\n", total, size); | |
94a30f0a | 45 | ret++; |
fbc2792b JA |
46 | break; |
47 | } | |
c6783fc3 VF |
48 | |
49 | c = (char *)e; | |
50 | for (i = 0; i < size; i++) { | |
51 | if (*(c+i) != 0) { | |
52 | printf("buffer not cleared at %lu, size %u\n", total, size); | |
53 | ret++; | |
54 | break; | |
55 | } | |
56 | } | |
57 | ||
58 | /* stop the while loop if buffer was not cleared */ | |
59 | if (i < size) | |
60 | break; | |
61 | ||
fbc2792b JA |
62 | e->magic1 = MAGIC1; |
63 | e->magic2 = MAGIC2; | |
6cc38ac0 | 64 | e->size = size; |
fbc2792b JA |
65 | total += size; |
66 | flist_add_tail(&e->list, &list); | |
67 | nr++; | |
68 | } | |
69 | ||
70 | printf("Got items: %u\n", nr); | |
71 | ||
72 | while (!flist_empty(&list)) { | |
73 | e = flist_entry(list.next, struct elem, list); | |
74 | assert(e->magic1 == MAGIC1); | |
75 | assert(e->magic2 == MAGIC2); | |
6cc38ac0 | 76 | total -= e->size; |
fbc2792b JA |
77 | flist_del(&e->list); |
78 | sfree(e); | |
6cc38ac0 VF |
79 | |
80 | if (!error) { | |
c6783fc3 | 81 | e = scalloc(1, LARGESMALLOC); |
6cc38ac0 | 82 | if (!e) { |
94a30f0a | 83 | ret++; |
6cc38ac0 VF |
84 | printf("failure allocating %u bytes at %lu allocated during sfree phase\n", |
85 | LARGESMALLOC, total); | |
c6783fc3 | 86 | break; |
6cc38ac0 | 87 | } |
c6783fc3 VF |
88 | |
89 | c = (char *)e; | |
90 | for (i = 0; i < LARGESMALLOC; i++) { | |
91 | if (*(c+i) != 0) { | |
92 | error = true; | |
93 | ret++; | |
94 | printf("large buffer not cleared at %lu, size %u\n", total, size); | |
95 | break; | |
96 | } | |
97 | } | |
98 | ||
99 | sfree(e); | |
6cc38ac0 | 100 | } |
fbc2792b JA |
101 | } |
102 | } | |
103 | ||
94a30f0a | 104 | return ret; |
fbc2792b JA |
105 | } |
106 | ||
fbc2792b JA |
107 | int main(int argc, char *argv[]) |
108 | { | |
94a30f0a VF |
109 | int ret; |
110 | ||
71471cb1 | 111 | arch_init(argv); |
fbc2792b | 112 | sinit(); |
9077ee4a | 113 | debug_init(); |
fbc2792b | 114 | |
94a30f0a VF |
115 | ret = do_rand_allocs(); |
116 | smalloc_debug(0); /* TODO: check that free and total blocks | |
117 | ** match */ | |
fbc2792b | 118 | |
fbc2792b | 119 | scleanup(); |
94a30f0a | 120 | return ret; |
fbc2792b | 121 | } |