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