etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / mem / t_mem.c
blobc51ed9d1e860dac9453d31a984d85fbbe9e08749
1 /* $NetBSD: t_mem.c,v 1.5 2014/12/10 04:37:53 christos Exp $ */
3 /*
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 */
22 #include <config.h>
24 #include <isc/mem.h>
26 #include <tests/t_api.h>
29 * Adapted from the original mempool_test.c program.
31 isc_mem_t *mctx;
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
40 static int
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;
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);
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);
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);
80 if (T_debug)
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");
97 ++nfails;
102 * Try to allocate one more. This should fail.
104 tmp = isc_mempool_get(mp1);
105 if (tmp != NULL) {
106 t_info("isc_mempool_get unexpectedly succeeded\n");
107 ++nfails;
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]);
117 items1[i] = NULL;
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;
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;
134 if (T_debug)
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);
153 ++nfails;
156 for (i = 0; i < 50; i++) {
157 isc_mempool_put(mp2, items2[i]);
158 items2[i] = NULL;
160 if (j % 50000 == 0)
161 t_info("...\n");
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]);
169 items1[i] = NULL;
172 isc_mempool_destroy(&mp1);
174 if (T_debug)
175 isc_mem_stats(mctx, stderr);
177 isc_mempool_destroy(&mp2);
179 if (T_debug)
180 isc_mem_stats(mctx, stderr);
182 isc_mem_destroy(&mctx);
184 return(0);
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
191 t1(void) {
192 int rval;
193 int result;
195 t_assert("mem", 1, T_REQUIRED, "%s", a1);
197 rval = memtest();
199 if (rval == 0)
200 result = T_PASS;
201 else
202 result = T_FAIL;
203 t_result(result);
206 testspec_t T_testlist[] = {
207 { (PFV) t1, "basic memory subsystem" },
208 { (PFV) 0, NULL }
211 #ifdef WIN32
213 main(int argc, char **argv) {
214 t_settests(T_testlist);
215 return (t_main(argc, argv));
217 #endif