]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_memory.c
Merge pull request #8808 from ton31337/feature/tracepoints_for_bgp_dest_lock_unlock
[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 main(int argc, char **argv)
48 {
49 void *a[10];
50 int i;
51
52 printf("malloc x, malloc x, free, malloc x, free free\n\n");
53 /* simple case, test cache */
54 for (i = 0; i < TIMES; i++) {
55 a[0] = XMALLOC(MTYPE_TEST, 1024);
56 memset(a[0], 1, 1024);
57 a[1] = XMALLOC(MTYPE_TEST, 1024);
58 memset(a[1], 1, 1024);
59 XFREE(MTYPE_TEST, a[0]); /* should go to cache */
60 a[0] = XMALLOC(MTYPE_TEST,
61 1024); /* should be satisfied from cache */
62 XFREE(MTYPE_TEST, a[0]);
63 XFREE(MTYPE_TEST, a[1]);
64 }
65
66 printf("malloc x, malloc y, free x, malloc y, free free\n\n");
67 /* cache should go invalid, valid, invalid, etc.. */
68 for (i = 0; i < TIMES; i++) {
69 a[0] = XMALLOC(MTYPE_TEST, 512);
70 memset(a[0], 1, 512);
71 a[1] = XMALLOC(MTYPE_TEST, 1024); /* invalidate cache */
72 memset(a[1], 1, 1024);
73 XFREE(MTYPE_TEST, a[0]);
74 a[0] = XMALLOC(MTYPE_TEST, 1024);
75 XFREE(MTYPE_TEST, a[0]);
76 XFREE(MTYPE_TEST, 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 a[0] = XCALLOC(MTYPE_TEST, 1024);
84 memset(a[0], 1, 1024);
85 a[1] = XCALLOC(MTYPE_TEST, 512); /* invalidate cache */
86 memset(a[1], 1, 512);
87 XFREE(MTYPE_TEST, a[1]);
88 XFREE(MTYPE_TEST, a[0]);
89 /* alloc == 0, cache can become valid again on next request */
90 }
91
92 printf("calloc and realloc\n\n");
93 /* check calloc + realloc */
94 for (i = 0; i < TIMES; i++) {
95 printf("calloc a0 1024\n");
96 a[0] = XCALLOC(MTYPE_TEST, 1024);
97 memset(a[0], 1, 1024 / 2);
98
99 printf("calloc 1 1024\n");
100 a[1] = XCALLOC(MTYPE_TEST, 1024);
101 memset(a[1], 1, 1024 / 2);
102
103 printf("realloc 0 1024\n");
104 a[3] = XREALLOC(MTYPE_TEST, a[0], 2048); /* invalidate cache */
105 if (a[3] != NULL)
106 a[0] = a[3];
107 memset(a[0], 1, 1024);
108
109 printf("calloc 2 512\n");
110 a[2] = XCALLOC(MTYPE_TEST, 512);
111 memset(a[2], 1, 512);
112
113 printf("free 1 0 2\n");
114 XFREE(MTYPE_TEST, a[1]);
115 XFREE(MTYPE_TEST, a[0]);
116 XFREE(MTYPE_TEST, a[2]);
117 /* alloc == 0, cache valid next request */
118 }
119 return 0;
120 }