| 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 | |
| 15 | struct elem { |
| 16 | unsigned int magic1; |
| 17 | struct flist_head list; |
| 18 | unsigned int magic2; |
| 19 | }; |
| 20 | |
| 21 | FLIST_HEAD(list); |
| 22 | |
| 23 | static int do_rand_allocs(void) |
| 24 | { |
| 25 | unsigned int size, nr, rounds = 0; |
| 26 | unsigned long total; |
| 27 | struct elem *e; |
| 28 | |
| 29 | while (rounds++ < LOOPS) { |
| 30 | #ifdef STEST_SEED |
| 31 | srand(MAGIC1); |
| 32 | #endif |
| 33 | nr = total = 0; |
| 34 | while (total < 120*1024*1024UL) { |
| 35 | size = 8 * sizeof(struct elem) + (int) (999.0 * (rand() / (RAND_MAX + 1.0))); |
| 36 | e = smalloc(size); |
| 37 | if (!e) { |
| 38 | printf("fail at %lu, size %u\n", total, size); |
| 39 | break; |
| 40 | } |
| 41 | e->magic1 = MAGIC1; |
| 42 | e->magic2 = MAGIC2; |
| 43 | total += size; |
| 44 | flist_add_tail(&e->list, &list); |
| 45 | nr++; |
| 46 | } |
| 47 | |
| 48 | printf("Got items: %u\n", nr); |
| 49 | |
| 50 | while (!flist_empty(&list)) { |
| 51 | e = flist_entry(list.next, struct elem, list); |
| 52 | assert(e->magic1 == MAGIC1); |
| 53 | assert(e->magic2 == MAGIC2); |
| 54 | flist_del(&e->list); |
| 55 | sfree(e); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int main(int argc, char *argv[]) |
| 63 | { |
| 64 | arch_init(argv); |
| 65 | sinit(); |
| 66 | debug_init(); |
| 67 | |
| 68 | do_rand_allocs(); |
| 69 | |
| 70 | scleanup(); |
| 71 | return 0; |
| 72 | } |