1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 #include <config.h>
13
14 #include <isc/mem.h>
15 #include <isc/util.h>
16
17 isc_mem_t *mctx;
18
19 int
main(int argc,char * argv[])20 main(int argc, char *argv[]) {
21 void *items1[50];
22 void *items2[50];
23 void *tmp;
24 isc_mempool_t *mp1, *mp2;
25 unsigned int i, j;
26 isc_mutex_t lock;
27
28 UNUSED(argc);
29 UNUSED(argv);
30
31 isc_mem_debugging = ISC_MEM_DEBUGRECORD;
32
33 RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
34
35 mctx = NULL;
36 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
37
38 mp1 = NULL;
39 RUNTIME_CHECK(isc_mempool_create(mctx, 24, &mp1) == ISC_R_SUCCESS);
40
41 mp2 = NULL;
42 RUNTIME_CHECK(isc_mempool_create(mctx, 31, &mp2) == ISC_R_SUCCESS);
43
44 isc_mempool_associatelock(mp1, &lock);
45 isc_mempool_associatelock(mp2, &lock);
46
47 isc_mem_stats(mctx, stderr);
48
49 isc_mempool_setfreemax(mp1, 10);
50 isc_mempool_setfillcount(mp1, 10);
51 isc_mempool_setmaxalloc(mp1, 30);
52
53 /*
54 * Allocate 30 items from the pool. This is our max.
55 */
56 for (i = 0; i < 30; i++) {
57 items1[i] = isc_mempool_get(mp1);
58 RUNTIME_CHECK(items1[i] != NULL);
59 }
60
61 /*
62 * Try to allocate one more. This should fail.
63 */
64 tmp = isc_mempool_get(mp1);
65 RUNTIME_CHECK(tmp == NULL);
66
67 /*
68 * Free the first 11 items. Verify that there are 10 free items on
69 * the free list (which is our max).
70 */
71
72 for (i = 0; i < 11; i++) {
73 isc_mempool_put(mp1, items1[i]);
74 items1[i] = NULL;
75 }
76
77 RUNTIME_CHECK(isc_mempool_getfreecount(mp1) == 10);
78 RUNTIME_CHECK(isc_mempool_getallocated(mp1) == 19);
79
80 isc_mem_stats(mctx, stderr);
81
82 /*
83 * Now, beat up on mp2 for a while. Allocate 50 items, then free
84 * them, then allocate 50 more, etc.
85 */
86 isc_mempool_setfreemax(mp2, 25);
87 isc_mempool_setfillcount(mp2, 25);
88 for (j = 0; j < 5000; j++) {
89 for (i = 0; i < 50; i++) {
90 items2[i] = isc_mempool_get(mp2);
91 RUNTIME_CHECK(items2[i] != NULL);
92 }
93 for (i = 0; i < 50; i++) {
94 isc_mempool_put(mp2, items2[i]);
95 items2[i] = NULL;
96 }
97 }
98
99 /*
100 * Free all the other items and blow away this pool.
101 */
102 for (i = 11; i < 30; i++) {
103 isc_mempool_put(mp1, items1[i]);
104 items1[i] = NULL;
105 }
106
107 isc_mempool_destroy(&mp1);
108
109 isc_mem_stats(mctx, stderr);
110
111 isc_mempool_destroy(&mp2);
112
113 isc_mem_stats(mctx, stderr);
114
115 isc_mem_destroy(&mctx);
116
117 DESTROYLOCK(&lock);
118
119 return (0);
120 }
121