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