Jens Axboe | fbc2792 | 2011-08-16 09:56:27 +0200 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <assert.h> |
| 4 | |
| 5 | #include "../smalloc.h" |
| 6 | #include "../flist.h" |
| 7 | |
| 8 | FILE *f_err; |
| 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 < 128*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 | static int do_specific_alloc(unsigned long size) |
| 63 | { |
| 64 | void *ptr; |
| 65 | |
| 66 | ptr = smalloc(size); |
| 67 | sfree(ptr); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | int main(int argc, char *argv[]) |
| 72 | { |
| 73 | f_err = stderr; |
| 74 | |
| 75 | sinit(); |
| 76 | |
| 77 | do_rand_allocs(); |
| 78 | |
| 79 | /* smalloc bug, commit 271067a6 */ |
| 80 | do_specific_alloc(671386584); |
| 81 | |
| 82 | scleanup(); |
| 83 | return 0; |
| 84 | } |