ignore some files
[tridge/bind9.git] / bin / tests / mem / t_mem.c
1 /*
2  * Copyright (C) 2004, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: t_mem.c,v 1.13.332.2 2009/01/22 23:47:05 tbox Exp $ */
19
20 #include <config.h>
21
22 #include <isc/mem.h>
23
24 #include <tests/t_api.h>
25
26 /*
27  * Adapted from the original mempool_test.c program.
28  */
29 isc_mem_t *mctx;
30
31 #define MP1_FREEMAX     10
32 #define MP1_FILLCNT     10
33 #define MP1_MAXALLOC    30
34
35 #define MP2_FREEMAX     25
36 #define MP2_FILLCNT     25
37
38 static int
39 memtest(void) {
40         int             nfails;
41         void            *items1[50];
42         void            *items2[50];
43         void            *tmp;
44         isc_mempool_t   *mp1, *mp2;
45         isc_result_t    isc_result;
46         unsigned int    i, j;
47         int             rval;
48
49
50         nfails = 0;
51         mctx = NULL;
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));
56                 ++nfails;
57                 return(nfails);
58         }
59
60         mp1 = NULL;
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));
65                 ++nfails;
66                 return(nfails);
67         }
68
69         mp2 = NULL;
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));
74                 ++nfails;
75                 return(nfails);
76         }
77
78         if (T_debug)
79                 isc_mem_stats(mctx, stderr);
80
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);
87
88         /*
89          * Allocate MP1_MAXALLOC items from the pool.  This is our max.
90          */
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");
95                         ++nfails;
96                 }
97         }
98
99         /*
100          * Try to allocate one more.  This should fail.
101          */
102         tmp = isc_mempool_get(mp1);
103         if (tmp != NULL) {
104                 t_info("isc_mempool_get unexpectedly succeeded\n");
105                 ++nfails;
106         }
107
108         /*
109          * Free the first 11 items.  Verify that there are 10 free items on
110          * the free list (which is our max).
111          */
112
113         for (i = 0; i < 11; i++) {
114                 isc_mempool_put(mp1, items1[i]);
115                 items1[i] = NULL;
116         }
117
118         rval = isc_mempool_getfreecount(mp1);
119         if (rval != 10) {
120                 t_info("isc_mempool_getfreecount returned %d, expected %d\n",
121                                 rval, MP1_FREEMAX);
122                 ++nfails;
123         }
124
125         rval = isc_mempool_getallocated(mp1);
126         if (rval != 19) {
127                 t_info("isc_mempool_getallocated returned %d, expected %d\n",
128                                 rval, MP1_MAXALLOC - 11);
129                 ++nfails;
130         }
131
132         if (T_debug)
133                 isc_mem_stats(mctx, stderr);
134
135         /*
136          * Now, beat up on mp2 for a while.  Allocate 50 items, then free
137          * them, then allocate 50 more, etc.
138          */
139
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);
144
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);
151                                 ++nfails;
152                         }
153                 }
154                 for (i = 0; i < 50; i++) {
155                         isc_mempool_put(mp2, items2[i]);
156                         items2[i] = NULL;
157                 }
158                 if (j % 50000 == 0)
159                         t_info("...\n");
160         }
161
162         /*
163          * Free all the other items and blow away this pool.
164          */
165         for (i = 11; i < MP1_MAXALLOC; i++) {
166                 isc_mempool_put(mp1, items1[i]);
167                 items1[i] = NULL;
168         }
169
170         isc_mempool_destroy(&mp1);
171
172         if (T_debug)
173                 isc_mem_stats(mctx, stderr);
174
175         isc_mempool_destroy(&mp2);
176
177         if (T_debug)
178                 isc_mem_stats(mctx, stderr);
179
180         isc_mem_destroy(&mctx);
181
182         return(0);
183 }
184
185 static const char *a1 =
186                 "the memory module supports the creation of memory contexts "
187                 "and the management of memory pools.";
188 static void
189 t1(void) {
190         int     rval;
191         int     result;
192
193         t_assert("mem", 1, T_REQUIRED, "%s", a1);
194
195         rval = memtest();
196
197         if (rval == 0)
198                 result = T_PASS;
199         else
200                 result = T_FAIL;
201         t_result(result);
202 }
203
204 testspec_t      T_testlist[] = {
205         {       t1,     "basic memory subsystem"        },
206         {       NULL,   NULL                            }
207 };
208