xref: /minix/external/bsd/bind/dist/lib/isc/pool.c (revision 00b67f09)
1 /*	$NetBSD: pool.c,v 1.1.1.3 2014/12/10 03:34:43 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2013  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id */
20 
21 /*! \file */
22 
23 #include <config.h>
24 
25 #include <string.h>
26 
27 #include <isc/mem.h>
28 #include <isc/random.h>
29 #include <isc/pool.h>
30 #include <isc/util.h>
31 
32 /***
33  *** Types.
34  ***/
35 
36 struct isc_pool {
37 	isc_mem_t *			mctx;
38 	unsigned int			count;
39 	isc_pooldeallocator_t		free;
40 	isc_poolinitializer_t		init;
41 	void *				initarg;
42 	void **				pool;
43 };
44 
45 /***
46  *** Functions.
47  ***/
48 
49 static isc_result_t
alloc_pool(isc_mem_t * mctx,unsigned int count,isc_pool_t ** poolp)50 alloc_pool(isc_mem_t *mctx, unsigned int count, isc_pool_t **poolp) {
51 	isc_pool_t *pool;
52 
53 	pool = isc_mem_get(mctx, sizeof(*pool));
54 	if (pool == NULL)
55 		return (ISC_R_NOMEMORY);
56 	pool->count = count;
57 	pool->free = NULL;
58 	pool->init = NULL;
59 	pool->initarg = NULL;
60 	pool->mctx = NULL;
61 	isc_mem_attach(mctx, &pool->mctx);
62 	pool->pool = isc_mem_get(mctx, count * sizeof(void *));
63 	if (pool->pool == NULL) {
64 		isc_mem_put(mctx, pool, sizeof(*pool));
65 		return (ISC_R_NOMEMORY);
66 	}
67 	memset(pool->pool, 0, count * sizeof(void *));
68 
69 	*poolp = pool;
70 	return (ISC_R_SUCCESS);
71 }
72 
73 isc_result_t
isc_pool_create(isc_mem_t * mctx,unsigned int count,isc_pooldeallocator_t free,isc_poolinitializer_t init,void * initarg,isc_pool_t ** poolp)74 isc_pool_create(isc_mem_t *mctx, unsigned int count,
75 		   isc_pooldeallocator_t free,
76 		   isc_poolinitializer_t init, void *initarg,
77 		   isc_pool_t **poolp)
78 {
79 	isc_pool_t *pool = NULL;
80 	isc_result_t result;
81 	unsigned int i;
82 
83 	INSIST(count > 0);
84 
85 	/* Allocate the pool structure */
86 	result = alloc_pool(mctx, count, &pool);
87 	if (result != ISC_R_SUCCESS)
88 		return (result);
89 
90 	pool->free = free;
91 	pool->init = init;
92 	pool->initarg = initarg;
93 
94 	/* Populate the pool */
95 	for (i = 0; i < count; i++) {
96 		result = init(&pool->pool[i], initarg);
97 		if (result != ISC_R_SUCCESS) {
98 			isc_pool_destroy(&pool);
99 			return (result);
100 		}
101 	}
102 
103 	*poolp = pool;
104 	return (ISC_R_SUCCESS);
105 }
106 
107 void *
isc_pool_get(isc_pool_t * pool)108 isc_pool_get(isc_pool_t *pool) {
109 	isc_uint32_t i;
110 	isc_random_get(&i);
111 	return (pool->pool[i % pool->count]);
112 }
113 
114 int
isc_pool_count(isc_pool_t * pool)115 isc_pool_count(isc_pool_t *pool) {
116 	REQUIRE(pool != NULL);
117 	return (pool->count);
118 }
119 
120 isc_result_t
isc_pool_expand(isc_pool_t ** sourcep,unsigned int count,isc_pool_t ** targetp)121 isc_pool_expand(isc_pool_t **sourcep, unsigned int count,
122 		   isc_pool_t **targetp)
123 {
124 	isc_result_t result;
125 	isc_pool_t *pool;
126 
127 	REQUIRE(sourcep != NULL && *sourcep != NULL);
128 	REQUIRE(targetp != NULL && *targetp == NULL);
129 
130 	pool = *sourcep;
131 	if (count > pool->count) {
132 		isc_pool_t *newpool = NULL;
133 		unsigned int i;
134 
135 		/* Allocate a new pool structure */
136 		result = alloc_pool(pool->mctx, count, &newpool);
137 		if (result != ISC_R_SUCCESS)
138 			return (result);
139 
140 		newpool->free = pool->free;
141 		newpool->init = pool->init;
142 		newpool->initarg = pool->initarg;
143 
144 		/* Copy over the objects from the old pool */
145 		for (i = 0; i < pool->count; i++) {
146 			newpool->pool[i] = pool->pool[i];
147 			pool->pool[i] = NULL;
148 		}
149 
150 		/* Populate the new entries */
151 		for (i = pool->count; i < count; i++) {
152 			result = pool->init(&newpool->pool[i], pool->initarg);
153 			if (result != ISC_R_SUCCESS) {
154 				isc_pool_destroy(&pool);
155 				return (result);
156 			}
157 		}
158 
159 		isc_pool_destroy(&pool);
160 		pool = newpool;
161 	}
162 
163 	*sourcep = NULL;
164 	*targetp = pool;
165 	return (ISC_R_SUCCESS);
166 }
167 
168 void
isc_pool_destroy(isc_pool_t ** poolp)169 isc_pool_destroy(isc_pool_t **poolp) {
170 	unsigned int i;
171 	isc_pool_t *pool = *poolp;
172 	for (i = 0; i < pool->count; i++) {
173 		if (pool->free != NULL && pool->pool[i] != NULL)
174 			pool->free(&pool->pool[i]);
175 	}
176 	isc_mem_put(pool->mctx, pool->pool, pool->count * sizeof(void *));
177 	isc_mem_putanddetach(&pool->mctx, pool, sizeof(*pool));
178 	*poolp = NULL;
179 }
180