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