]> git.proxmox.com Git - mirror_frr.git/blame - tests/lib/test_memory.c
Merge pull request #4877 from mjstapp/dplane_neighs
[mirror_frr.git] / tests / lib / test_memory.c
CommitLineData
d62a17ae 1/*
46f4a4d2
PJ
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 *
896014f4
DL
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
46f4a4d2
PJ
17 */
18
4dcadf7e 19#include <zebra.h>
20#include <memory.h>
21
4a1ab8e4
DL
22DEFINE_MGROUP(TEST_MEMORY, "memory test")
23DEFINE_MTYPE_STATIC(TEST_MEMORY, TEST, "generic test mtype")
24
4dcadf7e 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
32struct 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
d62a17ae 47int main(int argc, char **argv)
4dcadf7e 48{
d62a17ae 49 void *a[10];
50 int i;
4dcadf7e 51
d62a17ae 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 }
4dcadf7e 65
d62a17ae 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;
4dcadf7e 120}