2 * Copyright (C) 2004, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: t_mem.c,v 1.13.332.2 2009/01/22 23:47:05 tbox Exp $ */
24 #include <tests/t_api.h>
27 * Adapted from the original mempool_test.c program.
31 #define MP1_FREEMAX 10
32 #define MP1_FILLCNT 10
33 #define MP1_MAXALLOC 30
35 #define MP2_FREEMAX 25
36 #define MP2_FILLCNT 25
44 isc_mempool_t *mp1, *mp2;
45 isc_result_t isc_result;
52 isc_result = isc_mem_create(0, 0, &mctx);
53 if (isc_result != ISC_R_SUCCESS) {
54 t_info("isc_mem_create failed %s\n",
55 isc_result_totext(isc_result));
61 isc_result = isc_mempool_create(mctx, 24, &mp1);
62 if (isc_result != ISC_R_SUCCESS) {
63 t_info("isc_mempool_create failed %s\n",
64 isc_result_totext(isc_result));
70 isc_result = isc_mempool_create(mctx, 31, &mp2);
71 if (isc_result != ISC_R_SUCCESS) {
72 t_info("isc_mempool_create failed %s\n",
73 isc_result_totext(isc_result));
79 isc_mem_stats(mctx, stderr);
81 t_info("setting freemax to %d\n", MP1_FREEMAX);
82 isc_mempool_setfreemax(mp1, MP1_FREEMAX);
83 t_info("setting fillcount to %d\n", MP1_FILLCNT);
84 isc_mempool_setfillcount(mp1, MP1_FILLCNT);
85 t_info("setting maxalloc to %d\n", MP1_MAXALLOC);
86 isc_mempool_setmaxalloc(mp1, MP1_MAXALLOC);
89 * Allocate MP1_MAXALLOC items from the pool. This is our max.
91 for (i = 0; i < MP1_MAXALLOC; i++) {
92 items1[i] = isc_mempool_get(mp1);
93 if (items1[i] == NULL) {
94 t_info("isc_mempool_get unexpectedly failed\n");
100 * Try to allocate one more. This should fail.
102 tmp = isc_mempool_get(mp1);
104 t_info("isc_mempool_get unexpectedly succeeded\n");
109 * Free the first 11 items. Verify that there are 10 free items on
110 * the free list (which is our max).
113 for (i = 0; i < 11; i++) {
114 isc_mempool_put(mp1, items1[i]);
118 rval = isc_mempool_getfreecount(mp1);
120 t_info("isc_mempool_getfreecount returned %d, expected %d\n",
125 rval = isc_mempool_getallocated(mp1);
127 t_info("isc_mempool_getallocated returned %d, expected %d\n",
128 rval, MP1_MAXALLOC - 11);
133 isc_mem_stats(mctx, stderr);
136 * Now, beat up on mp2 for a while. Allocate 50 items, then free
137 * them, then allocate 50 more, etc.
140 t_info("setting freemax to %d\n", MP2_FREEMAX);
141 isc_mempool_setfreemax(mp2, 25);
142 t_info("setting fillcount to %d\n", MP2_FILLCNT);
143 isc_mempool_setfillcount(mp2, 25);
145 t_info("exercising the memory pool\n");
146 for (j = 0; j < 500000; j++) {
147 for (i = 0; i < 50; i++) {
148 items2[i] = isc_mempool_get(mp2);
149 if (items2[i] == NULL) {
150 t_info("items2[%d] is unexpectedly null\n", i);
154 for (i = 0; i < 50; i++) {
155 isc_mempool_put(mp2, items2[i]);
163 * Free all the other items and blow away this pool.
165 for (i = 11; i < MP1_MAXALLOC; i++) {
166 isc_mempool_put(mp1, items1[i]);
170 isc_mempool_destroy(&mp1);
173 isc_mem_stats(mctx, stderr);
175 isc_mempool_destroy(&mp2);
178 isc_mem_stats(mctx, stderr);
180 isc_mem_destroy(&mctx);
185 static const char *a1 =
186 "the memory module supports the creation of memory contexts "
187 "and the management of memory pools.";
193 t_assert("mem", 1, T_REQUIRED, "%s", a1);
204 testspec_t T_testlist[] = {
205 { t1, "basic memory subsystem" },