1 /* $NetBSD: t_mem.c,v 1.5 2014/12/10 04:37:53 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004, 2007, 2009, 2013 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 /* Id: t_mem.c,v 1.15 2009/01/22 23:47:54 tbox Exp */
21
22 #include <config.h>
23
24 #include <isc/mem.h>
25
26 #include <tests/t_api.h>
27
28 /*
29 * Adapted from the original mempool_test.c program.
30 */
31 isc_mem_t *mctx;
32
33 #define MP1_FREEMAX 10
34 #define MP1_FILLCNT 10
35 #define MP1_MAXALLOC 30
36
37 #define MP2_FREEMAX 25
38 #define MP2_FILLCNT 25
39
40 static int
memtest(void)41 memtest(void) {
42 int nfails;
43 void *items1[50];
44 void *items2[50];
45 void *tmp;
46 isc_mempool_t *mp1, *mp2;
47 isc_result_t isc_result;
48 unsigned int i, j;
49 int rval;
50
51
52 nfails = 0;
53 mctx = NULL;
54 isc_result = isc_mem_create(0, 0, &mctx);
55 if (isc_result != ISC_R_SUCCESS) {
56 t_info("isc_mem_create failed %s\n",
57 isc_result_totext(isc_result));
58 ++nfails;
59 return(nfails);
60 }
61
62 mp1 = NULL;
63 isc_result = isc_mempool_create(mctx, 24, &mp1);
64 if (isc_result != ISC_R_SUCCESS) {
65 t_info("isc_mempool_create failed %s\n",
66 isc_result_totext(isc_result));
67 ++nfails;
68 return(nfails);
69 }
70
71 mp2 = NULL;
72 isc_result = isc_mempool_create(mctx, 31, &mp2);
73 if (isc_result != ISC_R_SUCCESS) {
74 t_info("isc_mempool_create failed %s\n",
75 isc_result_totext(isc_result));
76 ++nfails;
77 return(nfails);
78 }
79
80 if (T_debug)
81 isc_mem_stats(mctx, stderr);
82
83 t_info("setting freemax to %d\n", MP1_FREEMAX);
84 isc_mempool_setfreemax(mp1, MP1_FREEMAX);
85 t_info("setting fillcount to %d\n", MP1_FILLCNT);
86 isc_mempool_setfillcount(mp1, MP1_FILLCNT);
87 t_info("setting maxalloc to %d\n", MP1_MAXALLOC);
88 isc_mempool_setmaxalloc(mp1, MP1_MAXALLOC);
89
90 /*
91 * Allocate MP1_MAXALLOC items from the pool. This is our max.
92 */
93 for (i = 0; i < MP1_MAXALLOC; i++) {
94 items1[i] = isc_mempool_get(mp1);
95 if (items1[i] == NULL) {
96 t_info("isc_mempool_get unexpectedly failed\n");
97 ++nfails;
98 }
99 }
100
101 /*
102 * Try to allocate one more. This should fail.
103 */
104 tmp = isc_mempool_get(mp1);
105 if (tmp != NULL) {
106 t_info("isc_mempool_get unexpectedly succeeded\n");
107 ++nfails;
108 }
109
110 /*
111 * Free the first 11 items. Verify that there are 10 free items on
112 * the free list (which is our max).
113 */
114
115 for (i = 0; i < 11; i++) {
116 isc_mempool_put(mp1, items1[i]);
117 items1[i] = NULL;
118 }
119
120 rval = isc_mempool_getfreecount(mp1);
121 if (rval != 10) {
122 t_info("isc_mempool_getfreecount returned %d, expected %d\n",
123 rval, MP1_FREEMAX);
124 ++nfails;
125 }
126
127 rval = isc_mempool_getallocated(mp1);
128 if (rval != 19) {
129 t_info("isc_mempool_getallocated returned %d, expected %d\n",
130 rval, MP1_MAXALLOC - 11);
131 ++nfails;
132 }
133
134 if (T_debug)
135 isc_mem_stats(mctx, stderr);
136
137 /*
138 * Now, beat up on mp2 for a while. Allocate 50 items, then free
139 * them, then allocate 50 more, etc.
140 */
141
142 t_info("setting freemax to %d\n", MP2_FREEMAX);
143 isc_mempool_setfreemax(mp2, 25);
144 t_info("setting fillcount to %d\n", MP2_FILLCNT);
145 isc_mempool_setfillcount(mp2, 25);
146
147 t_info("exercising the memory pool\n");
148 for (j = 0; j < 500000; j++) {
149 for (i = 0; i < 50; i++) {
150 items2[i] = isc_mempool_get(mp2);
151 if (items2[i] == NULL) {
152 t_info("items2[%d] is unexpectedly null\n", i);
153 ++nfails;
154 }
155 }
156 for (i = 0; i < 50; i++) {
157 isc_mempool_put(mp2, items2[i]);
158 items2[i] = NULL;
159 }
160 if (j % 50000 == 0)
161 t_info("...\n");
162 }
163
164 /*
165 * Free all the other items and blow away this pool.
166 */
167 for (i = 11; i < MP1_MAXALLOC; i++) {
168 isc_mempool_put(mp1, items1[i]);
169 items1[i] = NULL;
170 }
171
172 isc_mempool_destroy(&mp1);
173
174 if (T_debug)
175 isc_mem_stats(mctx, stderr);
176
177 isc_mempool_destroy(&mp2);
178
179 if (T_debug)
180 isc_mem_stats(mctx, stderr);
181
182 isc_mem_destroy(&mctx);
183
184 return(0);
185 }
186
187 static const char *a1 =
188 "the memory module supports the creation of memory contexts "
189 "and the management of memory pools.";
190 static void
t1(void)191 t1(void) {
192 int rval;
193 int result;
194
195 t_assert("mem", 1, T_REQUIRED, "%s", a1);
196
197 rval = memtest();
198
199 if (rval == 0)
200 result = T_PASS;
201 else
202 result = T_FAIL;
203 t_result(result);
204 }
205
206 testspec_t T_testlist[] = {
207 { (PFV) t1, "basic memory subsystem" },
208 { (PFV) 0, NULL }
209 };
210
211 #ifdef WIN32
212 int
main(int argc,char ** argv)213 main(int argc, char **argv) {
214 t_settests(T_testlist);
215 return (t_main(argc, argv));
216 }
217 #endif
218