]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_memory.c
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 along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 DEFINE_MGROUP(TEST_MEMORY
, "memory test")
23 DEFINE_MTYPE_STATIC(TEST_MEMORY
, TEST
, "generic test mtype")
25 /* Memory torture tests
27 * Tests below are generic but comments are focused on interaction with
28 * Paul's proposed memory 'quick' cache, which may never be included in
32 struct thread_master
*master
;
34 #if 0 /* set to 1 to use system alloc directly */
39 #define XMALLOC(T,S) malloc((S))
40 #define XCALLOC(T,S) calloc(1, (S))
41 #define XREALLOC(T,P,S) realloc((P),(S))
42 #define XFREE(T,P) free((P))
48 main(int argc
, char **argv
)
53 printf ("malloc x, malloc x, free, malloc x, free free\n\n");
54 /* simple case, test cache */
55 for (i
= 0; i
< TIMES
; i
++)
57 a
[0] = XMALLOC (MTYPE_TEST
, 1024);
58 memset (a
[0], 1, 1024);
59 a
[1] = XMALLOC (MTYPE_TEST
, 1024);
60 memset (a
[1], 1, 1024);
61 XFREE(MTYPE_TEST
, a
[0]); /* should go to cache */
62 a
[0] = XMALLOC (MTYPE_TEST
, 1024); /* should be satisfied from cache */
63 XFREE(MTYPE_TEST
, a
[0]);
64 XFREE(MTYPE_TEST
, a
[1]);
67 printf ("malloc x, malloc y, free x, malloc y, free free\n\n");
68 /* cache should go invalid, valid, invalid, etc.. */
69 for (i
= 0; i
< TIMES
; i
++)
71 a
[0] = XMALLOC (MTYPE_TEST
, 512);
72 memset (a
[0], 1, 512);
73 a
[1] = XMALLOC (MTYPE_TEST
, 1024); /* invalidate cache */
74 memset (a
[1], 1, 1024);
75 XFREE(MTYPE_TEST
, a
[0]);
76 a
[0] = XMALLOC (MTYPE_TEST
, 1024);
77 XFREE(MTYPE_TEST
, a
[0]);
78 XFREE(MTYPE_TEST
, a
[1]);
79 /* cache should become valid again on next request */
82 printf ("calloc\n\n");
84 for (i
= 0; i
< TIMES
; i
++)
86 a
[0] = XCALLOC (MTYPE_TEST
, 1024);
87 memset (a
[0], 1, 1024);
88 a
[1] = XCALLOC (MTYPE_TEST
, 512); /* invalidate cache */
89 memset (a
[1], 1, 512);
90 XFREE(MTYPE_TEST
, a
[1]);
91 XFREE(MTYPE_TEST
, a
[0]);
92 /* alloc == 0, cache can become valid again on next request */
95 printf ("calloc and realloc\n\n");
96 /* check calloc + realloc */
97 for (i
= 0; i
< TIMES
; i
++)
99 printf ("calloc a0 1024\n");
100 a
[0] = XCALLOC (MTYPE_TEST
, 1024);
101 memset (a
[0], 1, 1024/2);
103 printf ("calloc 1 1024\n");
104 a
[1] = XCALLOC (MTYPE_TEST
, 1024);
105 memset (a
[1], 1, 1024/2);
107 printf ("realloc 0 1024\n");
108 a
[3] = XREALLOC (MTYPE_TEST
, a
[0], 2048); /* invalidate cache */
111 memset (a
[0], 1, 1024);
113 printf ("calloc 2 512\n");
114 a
[2] = XCALLOC (MTYPE_TEST
, 512);
115 memset (a
[2], 1, 512);
117 printf ("free 1 0 2\n");
118 XFREE(MTYPE_TEST
, a
[1]);
119 XFREE(MTYPE_TEST
, a
[0]);
120 XFREE(MTYPE_TEST
, a
[2]);
121 /* alloc == 0, cache valid next request */