paul | 4dcadf7 | 2005-04-13 03:31:35 +0000 | [diff] [blame] | 1 | #include <zebra.h> |
| 2 | #include <memory.h> |
| 3 | |
| 4 | /* Memory torture tests |
| 5 | * |
| 6 | * Tests below are generic but comments are focused on interaction with |
| 7 | * Paul's proposed memory 'quick' cache, which may never be included in |
| 8 | * CVS |
| 9 | */ |
| 10 | |
| 11 | struct thread_master *master; |
| 12 | |
| 13 | #if 0 /* set to 1 to use system alloc directly */ |
| 14 | #undef XMALLOC |
| 15 | #undef XCALLOC |
| 16 | #undef XREALLOC |
| 17 | #undef XFREE |
| 18 | #define XMALLOC(T,S) malloc((S)) |
| 19 | #define XCALLOC(T,S) calloc(1, (S)) |
| 20 | #define XREALLOC(T,P,S) realloc((P),(S)) |
| 21 | #define XFREE(T,P) free((P)) |
| 22 | #endif |
| 23 | |
| 24 | #define TIMES 10 |
| 25 | |
| 26 | int |
| 27 | main(int argc, char **argv) |
| 28 | { |
| 29 | void *a[10]; |
| 30 | int i; |
| 31 | |
| 32 | printf ("malloc x, malloc x, free, malloc x, free free\n\n"); |
| 33 | /* simple case, test cache */ |
| 34 | for (i = 0; i < TIMES; i++) |
| 35 | { |
| 36 | a[0] = XMALLOC (MTYPE_VTY, 1024); |
| 37 | memset (a[0], 1, 1024); |
| 38 | a[1] = XMALLOC (MTYPE_VTY, 1024); |
| 39 | memset (a[1], 1, 1024); |
| 40 | XFREE(MTYPE_VTY, a[0]); /* should go to cache */ |
| 41 | a[0] = XMALLOC (MTYPE_VTY, 1024); /* should be satisfied from cache */ |
| 42 | XFREE(MTYPE_VTY, a[0]); |
| 43 | XFREE(MTYPE_VTY, a[1]); |
| 44 | } |
| 45 | |
| 46 | printf ("malloc x, malloc y, free x, malloc y, free free\n\n"); |
| 47 | /* cache should go invalid, valid, invalid, etc.. */ |
| 48 | for (i = 0; i < TIMES; i++) |
| 49 | { |
| 50 | a[0] = XMALLOC (MTYPE_VTY, 512); |
| 51 | memset (a[0], 1, 512); |
| 52 | a[1] = XMALLOC (MTYPE_VTY, 1024); /* invalidate cache */ |
| 53 | memset (a[1], 1, 1024); |
| 54 | XFREE(MTYPE_VTY, a[0]); |
| 55 | a[0] = XMALLOC (MTYPE_VTY, 1024); |
| 56 | XFREE(MTYPE_VTY, a[0]); |
| 57 | XFREE(MTYPE_VTY, a[1]); |
| 58 | /* cache should become valid again on next request */ |
| 59 | } |
| 60 | |
| 61 | printf ("calloc\n\n"); |
| 62 | /* test calloc */ |
| 63 | for (i = 0; i < TIMES; i++) |
| 64 | { |
| 65 | a[0] = XCALLOC (MTYPE_VTY, 1024); |
| 66 | memset (a[0], 1, 1024); |
| 67 | a[1] = XCALLOC (MTYPE_VTY, 512); /* invalidate cache */ |
| 68 | memset (a[1], 1, 512); |
| 69 | XFREE(MTYPE_VTY, a[1]); |
| 70 | XFREE(MTYPE_VTY, a[0]); |
| 71 | /* alloc == 0, cache can become valid again on next request */ |
| 72 | } |
| 73 | |
| 74 | printf ("calloc and realloc\n\n"); |
| 75 | /* check calloc + realloc */ |
| 76 | for (i = 0; i < TIMES; i++) |
| 77 | { |
| 78 | printf ("calloc a0 1024\n"); |
| 79 | a[0] = XCALLOC (MTYPE_VTY, 1024); |
| 80 | memset (a[0], 1, 1024/2); |
| 81 | |
| 82 | printf ("calloc 1 1024\n"); |
| 83 | a[1] = XCALLOC (MTYPE_VTY, 1024); |
| 84 | memset (a[1], 1, 1024/2); |
| 85 | |
| 86 | printf ("realloc 0 1024\n"); |
| 87 | a[3] = XREALLOC (MTYPE_VTY, a[0], 2048); /* invalidate cache */ |
| 88 | if (a[3] != NULL) |
| 89 | a[0] = a[3]; |
| 90 | memset (a[0], 1, 1024); |
| 91 | |
| 92 | printf ("calloc 2 512\n"); |
| 93 | a[2] = XCALLOC (MTYPE_VTY, 512); |
| 94 | memset (a[2], 1, 512); |
| 95 | |
| 96 | printf ("free 1 0 2\n"); |
| 97 | XFREE(MTYPE_VTY, a[1]); |
| 98 | XFREE(MTYPE_VTY, a[0]); |
| 99 | XFREE(MTYPE_VTY, a[2]); |
| 100 | /* alloc == 0, cache valid next request */ |
| 101 | } |
| 102 | return 0; |
| 103 | } |