]> git.proxmox.com Git - mirror_frr.git/blob - tests/test-memory.c
6849b9dcebefa0db94231994ee4ed71fe4b8e13a
[mirror_frr.git] / tests / test-memory.c
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
20 #include <zebra.h>
21 #include <memory.h>
22
23 DEFINE_MGROUP(TEST_MEMORY, "memory test")
24 DEFINE_MTYPE_STATIC(TEST_MEMORY, TEST, "generic test mtype")
25
26 /* Memory torture tests
27 *
28 * Tests below are generic but comments are focused on interaction with
29 * Paul's proposed memory 'quick' cache, which may never be included in
30 * CVS
31 */
32
33 struct thread_master *master;
34
35 #if 0 /* set to 1 to use system alloc directly */
36 #undef XMALLOC
37 #undef XCALLOC
38 #undef XREALLOC
39 #undef XFREE
40 #define XMALLOC(T,S) malloc((S))
41 #define XCALLOC(T,S) calloc(1, (S))
42 #define XREALLOC(T,P,S) realloc((P),(S))
43 #define XFREE(T,P) free((P))
44 #endif
45
46 #define TIMES 10
47
48 int
49 main(int argc, char **argv)
50 {
51 void *a[10];
52 int i;
53
54 printf ("malloc x, malloc x, free, malloc x, free free\n\n");
55 /* simple case, test cache */
56 for (i = 0; i < TIMES; i++)
57 {
58 a[0] = XMALLOC (MTYPE_TEST, 1024);
59 memset (a[0], 1, 1024);
60 a[1] = XMALLOC (MTYPE_TEST, 1024);
61 memset (a[1], 1, 1024);
62 XFREE(MTYPE_TEST, a[0]); /* should go to cache */
63 a[0] = XMALLOC (MTYPE_TEST, 1024); /* should be satisfied from cache */
64 XFREE(MTYPE_TEST, a[0]);
65 XFREE(MTYPE_TEST, a[1]);
66 }
67
68 printf ("malloc x, malloc y, free x, malloc y, free free\n\n");
69 /* cache should go invalid, valid, invalid, etc.. */
70 for (i = 0; i < TIMES; i++)
71 {
72 a[0] = XMALLOC (MTYPE_TEST, 512);
73 memset (a[0], 1, 512);
74 a[1] = XMALLOC (MTYPE_TEST, 1024); /* invalidate cache */
75 memset (a[1], 1, 1024);
76 XFREE(MTYPE_TEST, a[0]);
77 a[0] = XMALLOC (MTYPE_TEST, 1024);
78 XFREE(MTYPE_TEST, a[0]);
79 XFREE(MTYPE_TEST, a[1]);
80 /* cache should become valid again on next request */
81 }
82
83 printf ("calloc\n\n");
84 /* test calloc */
85 for (i = 0; i < TIMES; i++)
86 {
87 a[0] = XCALLOC (MTYPE_TEST, 1024);
88 memset (a[0], 1, 1024);
89 a[1] = XCALLOC (MTYPE_TEST, 512); /* invalidate cache */
90 memset (a[1], 1, 512);
91 XFREE(MTYPE_TEST, a[1]);
92 XFREE(MTYPE_TEST, a[0]);
93 /* alloc == 0, cache can become valid again on next request */
94 }
95
96 printf ("calloc and realloc\n\n");
97 /* check calloc + realloc */
98 for (i = 0; i < TIMES; i++)
99 {
100 printf ("calloc a0 1024\n");
101 a[0] = XCALLOC (MTYPE_TEST, 1024);
102 memset (a[0], 1, 1024/2);
103
104 printf ("calloc 1 1024\n");
105 a[1] = XCALLOC (MTYPE_TEST, 1024);
106 memset (a[1], 1, 1024/2);
107
108 printf ("realloc 0 1024\n");
109 a[3] = XREALLOC (MTYPE_TEST, a[0], 2048); /* invalidate cache */
110 if (a[3] != NULL)
111 a[0] = a[3];
112 memset (a[0], 1, 1024);
113
114 printf ("calloc 2 512\n");
115 a[2] = XCALLOC (MTYPE_TEST, 512);
116 memset (a[2], 1, 512);
117
118 printf ("free 1 0 2\n");
119 XFREE(MTYPE_TEST, a[1]);
120 XFREE(MTYPE_TEST, a[0]);
121 XFREE(MTYPE_TEST, a[2]);
122 /* alloc == 0, cache valid next request */
123 }
124 return 0;
125 }