]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_memory.c
Merge branch 'stable/3.0'
[mirror_frr.git] / tests / lib / 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 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
17 */
18
19 #include <zebra.h>
20 #include <memory.h>
21
22 DEFINE_MGROUP(TEST_MEMORY, "memory test")
23 DEFINE_MTYPE_STATIC(TEST_MEMORY, TEST, "generic test mtype")
24
25 /* Memory torture tests
26 *
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
29 * CVS
30 */
31
32 struct thread_master *master;
33
34 #if 0 /* set to 1 to use system alloc directly */
35 #undef XMALLOC
36 #undef XCALLOC
37 #undef XREALLOC
38 #undef XFREE
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))
43 #endif
44
45 #define TIMES 10
46
47 int
48 main(int argc, char **argv)
49 {
50 void *a[10];
51 int i;
52
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++)
56 {
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]);
65 }
66
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++)
70 {
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 */
80 }
81
82 printf ("calloc\n\n");
83 /* test calloc */
84 for (i = 0; i < TIMES; i++)
85 {
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 */
93 }
94
95 printf ("calloc and realloc\n\n");
96 /* check calloc + realloc */
97 for (i = 0; i < TIMES; i++)
98 {
99 printf ("calloc a0 1024\n");
100 a[0] = XCALLOC (MTYPE_TEST, 1024);
101 memset (a[0], 1, 1024/2);
102
103 printf ("calloc 1 1024\n");
104 a[1] = XCALLOC (MTYPE_TEST, 1024);
105 memset (a[1], 1, 1024/2);
106
107 printf ("realloc 0 1024\n");
108 a[3] = XREALLOC (MTYPE_TEST, a[0], 2048); /* invalidate cache */
109 if (a[3] != NULL)
110 a[0] = a[3];
111 memset (a[0], 1, 1024);
112
113 printf ("calloc 2 512\n");
114 a[2] = XCALLOC (MTYPE_TEST, 512);
115 memset (a[2], 1, 512);
116
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 */
122 }
123 return 0;
124 }