Paul Jakma | 010ebbb | 2014-09-16 11:53:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of Quagga. |
| 3 | * |
| 4 | * Quagga is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the |
| 6 | * Free Software Foundation; either version 2, or (at your option) any |
| 7 | * later version. |
| 8 | * |
| 9 | * Quagga is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with Quagga; see the file COPYING. If not, write to the Free |
| 16 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 17 | * 02111-1307, USA. |
| 18 | */ |
| 19 | |
paul | 4dcadf7 | 2005-04-13 03:31:35 +0000 | [diff] [blame] | 20 | #include <zebra.h> |
| 21 | #include <memory.h> |
| 22 | |
| 23 | /* Memory torture tests |
| 24 | * |
| 25 | * Tests below are generic but comments are focused on interaction with |
| 26 | * Paul's proposed memory 'quick' cache, which may never be included in |
| 27 | * CVS |
| 28 | */ |
| 29 | |
| 30 | struct thread_master *master; |
| 31 | |
| 32 | #if 0 /* set to 1 to use system alloc directly */ |
| 33 | #undef XMALLOC |
| 34 | #undef XCALLOC |
| 35 | #undef XREALLOC |
| 36 | #undef XFREE |
| 37 | #define XMALLOC(T,S) malloc((S)) |
| 38 | #define XCALLOC(T,S) calloc(1, (S)) |
| 39 | #define XREALLOC(T,P,S) realloc((P),(S)) |
| 40 | #define XFREE(T,P) free((P)) |
| 41 | #endif |
| 42 | |
| 43 | #define TIMES 10 |
| 44 | |
| 45 | int |
| 46 | main(int argc, char **argv) |
| 47 | { |
| 48 | void *a[10]; |
| 49 | int i; |
| 50 | |
| 51 | printf ("malloc x, malloc x, free, malloc x, free free\n\n"); |
| 52 | /* simple case, test cache */ |
| 53 | for (i = 0; i < TIMES; i++) |
| 54 | { |
| 55 | a[0] = XMALLOC (MTYPE_VTY, 1024); |
| 56 | memset (a[0], 1, 1024); |
| 57 | a[1] = XMALLOC (MTYPE_VTY, 1024); |
| 58 | memset (a[1], 1, 1024); |
| 59 | XFREE(MTYPE_VTY, a[0]); /* should go to cache */ |
| 60 | a[0] = XMALLOC (MTYPE_VTY, 1024); /* should be satisfied from cache */ |
| 61 | XFREE(MTYPE_VTY, a[0]); |
| 62 | XFREE(MTYPE_VTY, a[1]); |
| 63 | } |
| 64 | |
| 65 | printf ("malloc x, malloc y, free x, malloc y, free free\n\n"); |
| 66 | /* cache should go invalid, valid, invalid, etc.. */ |
| 67 | for (i = 0; i < TIMES; i++) |
| 68 | { |
| 69 | a[0] = XMALLOC (MTYPE_VTY, 512); |
| 70 | memset (a[0], 1, 512); |
| 71 | a[1] = XMALLOC (MTYPE_VTY, 1024); /* invalidate cache */ |
| 72 | memset (a[1], 1, 1024); |
| 73 | XFREE(MTYPE_VTY, a[0]); |
| 74 | a[0] = XMALLOC (MTYPE_VTY, 1024); |
| 75 | XFREE(MTYPE_VTY, a[0]); |
| 76 | XFREE(MTYPE_VTY, a[1]); |
| 77 | /* cache should become valid again on next request */ |
| 78 | } |
| 79 | |
| 80 | printf ("calloc\n\n"); |
| 81 | /* test calloc */ |
| 82 | for (i = 0; i < TIMES; i++) |
| 83 | { |
| 84 | a[0] = XCALLOC (MTYPE_VTY, 1024); |
| 85 | memset (a[0], 1, 1024); |
| 86 | a[1] = XCALLOC (MTYPE_VTY, 512); /* invalidate cache */ |
| 87 | memset (a[1], 1, 512); |
| 88 | XFREE(MTYPE_VTY, a[1]); |
| 89 | XFREE(MTYPE_VTY, a[0]); |
| 90 | /* alloc == 0, cache can become valid again on next request */ |
| 91 | } |
| 92 | |
| 93 | printf ("calloc and realloc\n\n"); |
| 94 | /* check calloc + realloc */ |
| 95 | for (i = 0; i < TIMES; i++) |
| 96 | { |
| 97 | printf ("calloc a0 1024\n"); |
| 98 | a[0] = XCALLOC (MTYPE_VTY, 1024); |
| 99 | memset (a[0], 1, 1024/2); |
| 100 | |
| 101 | printf ("calloc 1 1024\n"); |
| 102 | a[1] = XCALLOC (MTYPE_VTY, 1024); |
| 103 | memset (a[1], 1, 1024/2); |
| 104 | |
| 105 | printf ("realloc 0 1024\n"); |
| 106 | a[3] = XREALLOC (MTYPE_VTY, a[0], 2048); /* invalidate cache */ |
| 107 | if (a[3] != NULL) |
| 108 | a[0] = a[3]; |
| 109 | memset (a[0], 1, 1024); |
| 110 | |
| 111 | printf ("calloc 2 512\n"); |
| 112 | a[2] = XCALLOC (MTYPE_VTY, 512); |
| 113 | memset (a[2], 1, 512); |
| 114 | |
| 115 | printf ("free 1 0 2\n"); |
| 116 | XFREE(MTYPE_VTY, a[1]); |
| 117 | XFREE(MTYPE_VTY, a[0]); |
| 118 | XFREE(MTYPE_VTY, a[2]); |
| 119 | /* alloc == 0, cache valid next request */ |
| 120 | } |
| 121 | return 0; |
| 122 | } |