]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_memory.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
8 DEFINE_MGROUP(TEST_MEMORY
, "memory test");
9 DEFINE_MTYPE_STATIC(TEST_MEMORY
, TEST
, "generic test mtype");
11 /* Memory torture tests
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
18 struct thread_master
*master
;
20 #if 0 /* set to 1 to use system alloc directly */
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))
33 int main(int argc
, char **argv
)
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]);
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);
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 */
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 */
73 XFREE(MTYPE_TEST
, a
[1]);
74 XFREE(MTYPE_TEST
, a
[0]);
75 /* alloc == 0, cache can become valid again on next request */
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);
85 printf("calloc 1 1024\n");
86 a
[1] = XCALLOC(MTYPE_TEST
, 1024);
87 memset(a
[1], 1, 1024 / 2);
89 printf("realloc 0 1024\n");
90 a
[3] = XREALLOC(MTYPE_TEST
, a
[0], 2048); /* invalidate cache */
93 memset(a
[0], 1, 1024);
95 printf("calloc 2 512\n");
96 a
[2] = XCALLOC(MTYPE_TEST
, 512);
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 */