1 /* $NetBSD: mempool_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2007 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: mempool_test.c,v 1.17 2007/06/19 23:46:59 tbox Exp */
30 main(int argc
, char *argv
[]) {
34 isc_mempool_t
*mp1
, *mp2
;
41 isc_mem_debugging
= ISC_MEM_DEBUGRECORD
;
43 RUNTIME_CHECK(isc_mutex_init(&lock
) == ISC_R_SUCCESS
);
46 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx
) == ISC_R_SUCCESS
);
49 RUNTIME_CHECK(isc_mempool_create(mctx
, 24, &mp1
) == ISC_R_SUCCESS
);
52 RUNTIME_CHECK(isc_mempool_create(mctx
, 31, &mp2
) == ISC_R_SUCCESS
);
54 isc_mempool_associatelock(mp1
, &lock
);
55 isc_mempool_associatelock(mp2
, &lock
);
57 isc_mem_stats(mctx
, stderr
);
59 isc_mempool_setfreemax(mp1
, 10);
60 isc_mempool_setfillcount(mp1
, 10);
61 isc_mempool_setmaxalloc(mp1
, 30);
64 * Allocate 30 items from the pool. This is our max.
66 for (i
= 0; i
< 30; i
++) {
67 items1
[i
] = isc_mempool_get(mp1
);
68 RUNTIME_CHECK(items1
[i
] != NULL
);
72 * Try to allocate one more. This should fail.
74 tmp
= isc_mempool_get(mp1
);
75 RUNTIME_CHECK(tmp
== NULL
);
78 * Free the first 11 items. Verify that there are 10 free items on
79 * the free list (which is our max).
82 for (i
= 0; i
< 11; i
++) {
83 isc_mempool_put(mp1
, items1
[i
]);
87 RUNTIME_CHECK(isc_mempool_getfreecount(mp1
) == 10);
88 RUNTIME_CHECK(isc_mempool_getallocated(mp1
) == 19);
90 isc_mem_stats(mctx
, stderr
);
93 * Now, beat up on mp2 for a while. Allocate 50 items, then free
94 * them, then allocate 50 more, etc.
96 isc_mempool_setfreemax(mp2
, 25);
97 isc_mempool_setfillcount(mp2
, 25);
98 for (j
= 0; j
< 5000; j
++) {
99 for (i
= 0; i
< 50; i
++) {
100 items2
[i
] = isc_mempool_get(mp2
);
101 RUNTIME_CHECK(items2
[i
] != NULL
);
103 for (i
= 0; i
< 50; i
++) {
104 isc_mempool_put(mp2
, items2
[i
]);
110 * Free all the other items and blow away this pool.
112 for (i
= 11; i
< 30; i
++) {
113 isc_mempool_put(mp1
, items1
[i
]);
117 isc_mempool_destroy(&mp1
);
119 isc_mem_stats(mctx
, stderr
);
121 isc_mempool_destroy(&mp2
);
123 isc_mem_stats(mctx
, stderr
);
125 isc_mem_destroy(&mctx
);