1 /* $NetBSD: t_mem.c,v 1.5 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2007, 2009, 2013 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2001 Internet Software Consortium.
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.
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.
20 /* Id: t_mem.c,v 1.15 2009/01/22 23:47:54 tbox Exp */
26 #include <tests/t_api.h>
29 * Adapted from the original mempool_test.c program.
33 #define MP1_FREEMAX 10
34 #define MP1_FILLCNT 10
35 #define MP1_MAXALLOC 30
37 #define MP2_FREEMAX 25
38 #define MP2_FILLCNT 25
46 isc_mempool_t
*mp1
, *mp2
;
47 isc_result_t isc_result
;
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
));
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
));
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
));
81 isc_mem_stats(mctx
, stderr
);
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
);
91 * Allocate MP1_MAXALLOC items from the pool. This is our max.
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");
102 * Try to allocate one more. This should fail.
104 tmp
= isc_mempool_get(mp1
);
106 t_info("isc_mempool_get unexpectedly succeeded\n");
111 * Free the first 11 items. Verify that there are 10 free items on
112 * the free list (which is our max).
115 for (i
= 0; i
< 11; i
++) {
116 isc_mempool_put(mp1
, items1
[i
]);
120 rval
= isc_mempool_getfreecount(mp1
);
122 t_info("isc_mempool_getfreecount returned %d, expected %d\n",
127 rval
= isc_mempool_getallocated(mp1
);
129 t_info("isc_mempool_getallocated returned %d, expected %d\n",
130 rval
, MP1_MAXALLOC
- 11);
135 isc_mem_stats(mctx
, stderr
);
138 * Now, beat up on mp2 for a while. Allocate 50 items, then free
139 * them, then allocate 50 more, etc.
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);
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
);
156 for (i
= 0; i
< 50; i
++) {
157 isc_mempool_put(mp2
, items2
[i
]);
165 * Free all the other items and blow away this pool.
167 for (i
= 11; i
< MP1_MAXALLOC
; i
++) {
168 isc_mempool_put(mp1
, items1
[i
]);
172 isc_mempool_destroy(&mp1
);
175 isc_mem_stats(mctx
, stderr
);
177 isc_mempool_destroy(&mp2
);
180 isc_mem_stats(mctx
, stderr
);
182 isc_mem_destroy(&mctx
);
187 static const char *a1
=
188 "the memory module supports the creation of memory contexts "
189 "and the management of memory pools.";
195 t_assert("mem", 1, T_REQUIRED
, "%s", a1
);
206 testspec_t T_testlist
[] = {
207 { (PFV
) t1
, "basic memory subsystem" },
213 main(int argc
, char **argv
) {
214 t_settests(T_testlist
);
215 return (t_main(argc
, argv
));