]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_memory.c
2 * This file is part of Quagga.
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
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.
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
22 DEFINE_MGROUP(TEST_MEMORY
, "memory test");
23 DEFINE_MTYPE_STATIC(TEST_MEMORY
, TEST
, "generic test mtype");
25 /* Memory torture tests
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
32 struct thread_master
*master
;
34 #if 0 /* set to 1 to use system alloc directly */
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))
47 int main(int argc
, char **argv
)
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]);
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);
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 */
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 */
87 XFREE(MTYPE_TEST
, a
[1]);
88 XFREE(MTYPE_TEST
, a
[0]);
89 /* alloc == 0, cache can become valid again on next request */
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);
99 printf("calloc 1 1024\n");
100 a
[1] = XCALLOC(MTYPE_TEST
, 1024);
101 memset(a
[1], 1, 1024 / 2);
103 printf("realloc 0 1024\n");
104 a
[3] = XREALLOC(MTYPE_TEST
, a
[0], 2048); /* invalidate cache */
107 memset(a
[0], 1, 1024);
109 printf("calloc 2 512\n");
110 a
[2] = XCALLOC(MTYPE_TEST
, 512);
111 memset(a
[2], 1, 512);
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 */