Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / mem_api.c
blob20339d3eab10f6898f7889722131d859ed16968a
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
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.
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.
19 /* Id: mem_api.c,v 1.5 2009/09/02 23:48:02 tbox Exp */
21 #include <config.h>
23 #include <isc/magic.h>
24 #include <isc/mem.h>
25 #include <isc/once.h>
26 #include <isc/util.h>
28 #if ISC_MEM_TRACKLINES
29 #define FLARG_PASS , file, line
30 #define FLARG , const char *file, int line
31 #else
32 #define FLARG_PASS
33 #define FLARG
34 #endif
36 static isc_mutex_t createlock;
37 static isc_once_t once = ISC_ONCE_INIT;
38 static isc_memcreatefunc_t mem_createfunc = NULL;
40 static void
41 initialize(void) {
42 RUNTIME_CHECK(isc_mutex_init(&createlock) == ISC_R_SUCCESS);
45 isc_result_t
46 isc_mem_register(isc_memcreatefunc_t createfunc) {
47 isc_result_t result = ISC_R_SUCCESS;
49 RUNTIME_CHECK(isc_once_do(&once, initialize) == ISC_R_SUCCESS);
51 LOCK(&createlock);
52 if (mem_createfunc == NULL)
53 mem_createfunc = createfunc;
54 else
55 result = ISC_R_EXISTS;
56 UNLOCK(&createlock);
58 return (result);
61 isc_result_t
62 isc_mem_create(size_t init_max_size, size_t target_size, isc_mem_t **mctxp) {
63 isc_result_t result;
65 LOCK(&createlock);
67 REQUIRE(mem_createfunc != NULL);
68 result = (*mem_createfunc)(init_max_size, target_size, mctxp,
69 ISC_MEMFLAG_DEFAULT);
71 UNLOCK(&createlock);
73 return (result);
76 isc_result_t
77 isc_mem_create2(size_t init_max_size, size_t target_size, isc_mem_t **mctxp,
78 unsigned int flags)
80 isc_result_t result;
82 LOCK(&createlock);
84 REQUIRE(mem_createfunc != NULL);
85 result = (*mem_createfunc)(init_max_size, target_size, mctxp, flags);
87 UNLOCK(&createlock);
89 return (result);
92 void
93 isc_mem_attach(isc_mem_t *source, isc_mem_t **targetp) {
94 REQUIRE(ISCAPI_MCTX_VALID(source));
95 REQUIRE(targetp != NULL && *targetp == NULL);
97 source->methods->attach(source, targetp);
99 ENSURE(*targetp == source);
102 void
103 isc_mem_detach(isc_mem_t **mctxp) {
104 REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
106 (*mctxp)->methods->detach(mctxp);
108 ENSURE(*mctxp == NULL);
111 void
112 isc_mem_destroy(isc_mem_t **mctxp) {
113 REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
115 (*mctxp)->methods->destroy(mctxp);
117 ENSURE(*mctxp == NULL);
120 void *
121 isc__mem_get(isc_mem_t *mctx, size_t size FLARG) {
122 REQUIRE(ISCAPI_MCTX_VALID(mctx));
124 return (mctx->methods->memget(mctx, size FLARG_PASS));
127 void
128 isc__mem_put(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
129 REQUIRE(ISCAPI_MCTX_VALID(mctx));
131 mctx->methods->memput(mctx, ptr, size FLARG_PASS);
134 void
135 isc__mem_putanddetach(isc_mem_t **mctxp, void *ptr, size_t size FLARG) {
136 REQUIRE(mctxp != NULL && ISCAPI_MCTX_VALID(*mctxp));
138 (*mctxp)->methods->memputanddetach(mctxp, ptr, size FLARG_PASS);
141 * XXX: We cannot always ensure *mctxp == NULL here
142 * (see lib/isc/mem.c).
146 void *
147 isc__mem_allocate(isc_mem_t *mctx, size_t size FLARG) {
148 REQUIRE(ISCAPI_MCTX_VALID(mctx));
150 return (mctx->methods->memallocate(mctx, size FLARG_PASS));
153 void *
154 isc__mem_reallocate(isc_mem_t *mctx, void *ptr, size_t size FLARG) {
155 REQUIRE(ISCAPI_MCTX_VALID(mctx));
157 return (mctx->methods->memreallocate(mctx, ptr, size FLARG_PASS));
160 char *
161 isc__mem_strdup(isc_mem_t *mctx, const char *s FLARG) {
162 REQUIRE(ISCAPI_MCTX_VALID(mctx));
164 return (mctx->methods->memstrdup(mctx, s FLARG_PASS));
167 void
168 isc__mem_free(isc_mem_t *mctx, void *ptr FLARG) {
169 REQUIRE(ISCAPI_MCTX_VALID(mctx));
171 mctx->methods->memfree(mctx, ptr FLARG_PASS);
174 void
175 isc_mem_setdestroycheck(isc_mem_t *mctx, isc_boolean_t flag) {
176 REQUIRE(ISCAPI_MCTX_VALID(mctx));
178 mctx->methods->setdestroycheck(mctx, flag);
181 void
182 isc_mem_setwater(isc_mem_t *ctx, isc_mem_water_t water, void *water_arg,
183 size_t hiwater, size_t lowater)
185 REQUIRE(ISCAPI_MCTX_VALID(ctx));
187 ctx->methods->setwater(ctx, water, water_arg, hiwater, lowater);
190 void
191 isc_mem_waterack(isc_mem_t *ctx, int flag) {
192 REQUIRE(ISCAPI_MCTX_VALID(ctx));
194 ctx->methods->waterack(ctx, flag);
197 size_t
198 isc_mem_inuse(isc_mem_t *mctx) {
199 REQUIRE(ISCAPI_MCTX_VALID(mctx));
201 return (mctx->methods->inuse(mctx));
204 void
205 isc_mem_setname(isc_mem_t *mctx, const char *name, void *tag) {
206 REQUIRE(ISCAPI_MCTX_VALID(mctx));
208 UNUSED(name);
209 UNUSED(tag);
211 return;
214 const char *
215 isc_mem_getname(isc_mem_t *mctx) {
216 REQUIRE(ISCAPI_MCTX_VALID(mctx));
218 return ("");
221 void *
222 isc_mem_gettag(isc_mem_t *mctx) {
223 REQUIRE(ISCAPI_MCTX_VALID(mctx));
225 return (NULL);
228 isc_result_t
229 isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp) {
230 REQUIRE(ISCAPI_MCTX_VALID(mctx));
232 return (mctx->methods->mpcreate(mctx, size, mpctxp));
235 void
236 isc_mempool_destroy(isc_mempool_t **mpctxp) {
237 REQUIRE(mpctxp != NULL && ISCAPI_MPOOL_VALID(*mpctxp));
239 (*mpctxp)->methods->destroy(mpctxp);
241 ENSURE(*mpctxp == NULL);
244 void *
245 isc__mempool_get(isc_mempool_t *mpctx FLARG) {
246 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
248 return (mpctx->methods->get(mpctx FLARG_PASS));
251 void
252 isc__mempool_put(isc_mempool_t *mpctx, void *mem FLARG) {
253 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
255 mpctx->methods->put(mpctx, mem FLARG_PASS);
258 unsigned int
259 isc_mempool_getallocated(isc_mempool_t *mpctx) {
260 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
262 return (mpctx->methods->getallocated(mpctx));
265 void
266 isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit) {
267 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
269 mpctx->methods->setmaxalloc(mpctx, limit);
272 void
273 isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit) {
274 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
276 mpctx->methods->setfreemax(mpctx, limit);
279 void
280 isc_mempool_setname(isc_mempool_t *mpctx, const char *name) {
281 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
283 mpctx->methods->setname(mpctx, name);
286 void
287 isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock) {
288 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
290 mpctx->methods->associatelock(mpctx, lock);
293 void
294 isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit) {
295 REQUIRE(ISCAPI_MPOOL_VALID(mpctx));
297 mpctx->methods->setfillcount(mpctx, limit);