2 * This file is part of Quagga.
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
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.
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
23 /* Memory torture tests
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
30 struct thread_master *master;
32 #if 0 /* set to 1 to use system alloc directly */
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))
46 main(int argc, char **argv)
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++)
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]);
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++)
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 */
80 printf ("calloc\n\n");
82 for (i = 0; i < TIMES; i++)
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 */
93 printf ("calloc and realloc\n\n");
94 /* check calloc + realloc */
95 for (i = 0; i < TIMES; i++)
97 printf ("calloc a0 1024\n");
98 a[0] = XCALLOC (MTYPE_VTY, 1024);
99 memset (a[0], 1, 1024/2);
101 printf ("calloc 1 1024\n");
102 a[1] = XCALLOC (MTYPE_VTY, 1024);
103 memset (a[1], 1, 1024/2);
105 printf ("realloc 0 1024\n");
106 a[3] = XREALLOC (MTYPE_VTY, a[0], 2048); /* invalidate cache */
109 memset (a[0], 1, 1024);
111 printf ("calloc 2 512\n");
112 a[2] = XCALLOC (MTYPE_VTY, 512);
113 memset (a[2], 1, 512);
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 */