1 /*	$NetBSD: mempool_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1999-2001  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: mempool_test.c,v 1.17 2007/06/19 23:46:59 tbox Exp  */
21 
22 #include <config.h>
23 
24 #include <isc/mem.h>
25 #include <isc/util.h>
26 
27 isc_mem_t *mctx;
28 
29 int
main(int argc,char * argv[])30 main(int argc, char *argv[]) {
31 	void *items1[50];
32 	void *items2[50];
33 	void *tmp;
34 	isc_mempool_t *mp1, *mp2;
35 	unsigned int i, j;
36 	isc_mutex_t lock;
37 
38 	UNUSED(argc);
39 	UNUSED(argv);
40 
41 	isc_mem_debugging = ISC_MEM_DEBUGRECORD;
42 
43 	RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
44 
45 	mctx = NULL;
46 	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
47 
48 	mp1 = NULL;
49 	RUNTIME_CHECK(isc_mempool_create(mctx, 24, &mp1) == ISC_R_SUCCESS);
50 
51 	mp2 = NULL;
52 	RUNTIME_CHECK(isc_mempool_create(mctx, 31, &mp2) == ISC_R_SUCCESS);
53 
54 	isc_mempool_associatelock(mp1, &lock);
55 	isc_mempool_associatelock(mp2, &lock);
56 
57 	isc_mem_stats(mctx, stderr);
58 
59 	isc_mempool_setfreemax(mp1, 10);
60 	isc_mempool_setfillcount(mp1, 10);
61 	isc_mempool_setmaxalloc(mp1, 30);
62 
63 	/*
64 	 * Allocate 30 items from the pool.  This is our max.
65 	 */
66 	for (i = 0; i < 30; i++) {
67 		items1[i] = isc_mempool_get(mp1);
68 		RUNTIME_CHECK(items1[i] != NULL);
69 	}
70 
71 	/*
72 	 * Try to allocate one more.  This should fail.
73 	 */
74 	tmp = isc_mempool_get(mp1);
75 	RUNTIME_CHECK(tmp == NULL);
76 
77 	/*
78 	 * Free the first 11 items.  Verify that there are 10 free items on
79 	 * the free list (which is our max).
80 	 */
81 
82 	for (i = 0; i < 11; i++) {
83 		isc_mempool_put(mp1, items1[i]);
84 		items1[i] = NULL;
85 	}
86 
87 	RUNTIME_CHECK(isc_mempool_getfreecount(mp1) == 10);
88 	RUNTIME_CHECK(isc_mempool_getallocated(mp1) == 19);
89 
90 	isc_mem_stats(mctx, stderr);
91 
92 	/*
93 	 * Now, beat up on mp2 for a while.  Allocate 50 items, then free
94 	 * them, then allocate 50 more, etc.
95 	 */
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);
102 		}
103 		for (i = 0; i < 50; i++) {
104 			isc_mempool_put(mp2, items2[i]);
105 			items2[i] = NULL;
106 		}
107 	}
108 
109 	/*
110 	 * Free all the other items and blow away this pool.
111 	 */
112 	for (i = 11; i < 30; i++) {
113 		isc_mempool_put(mp1, items1[i]);
114 		items1[i] = NULL;
115 	}
116 
117 	isc_mempool_destroy(&mp1);
118 
119 	isc_mem_stats(mctx, stderr);
120 
121 	isc_mempool_destroy(&mp2);
122 
123 	isc_mem_stats(mctx, stderr);
124 
125 	isc_mem_destroy(&mctx);
126 
127 	DESTROYLOCK(&lock);
128 
129 	return (0);
130 }
131