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 */
23 #include <isc/magic.h>
28 #if ISC_MEM_TRACKLINES
29 #define FLARG_PASS , file, line
30 #define FLARG , const char *file, int line
36 static isc_mutex_t createlock
;
37 static isc_once_t once
= ISC_ONCE_INIT
;
38 static isc_memcreatefunc_t mem_createfunc
= NULL
;
42 RUNTIME_CHECK(isc_mutex_init(&createlock
) == ISC_R_SUCCESS
);
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
);
52 if (mem_createfunc
== NULL
)
53 mem_createfunc
= createfunc
;
55 result
= ISC_R_EXISTS
;
62 isc_mem_create(size_t init_max_size
, size_t target_size
, isc_mem_t
**mctxp
) {
67 REQUIRE(mem_createfunc
!= NULL
);
68 result
= (*mem_createfunc
)(init_max_size
, target_size
, mctxp
,
77 isc_mem_create2(size_t init_max_size
, size_t target_size
, isc_mem_t
**mctxp
,
84 REQUIRE(mem_createfunc
!= NULL
);
85 result
= (*mem_createfunc
)(init_max_size
, target_size
, mctxp
, flags
);
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
);
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
);
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
);
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
));
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
);
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).
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
));
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
));
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
));
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
);
175 isc_mem_setdestroycheck(isc_mem_t
*mctx
, isc_boolean_t flag
) {
176 REQUIRE(ISCAPI_MCTX_VALID(mctx
));
178 mctx
->methods
->setdestroycheck(mctx
, flag
);
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
);
191 isc_mem_waterack(isc_mem_t
*ctx
, int flag
) {
192 REQUIRE(ISCAPI_MCTX_VALID(ctx
));
194 ctx
->methods
->waterack(ctx
, flag
);
198 isc_mem_inuse(isc_mem_t
*mctx
) {
199 REQUIRE(ISCAPI_MCTX_VALID(mctx
));
201 return (mctx
->methods
->inuse(mctx
));
205 isc_mem_setname(isc_mem_t
*mctx
, const char *name
, void *tag
) {
206 REQUIRE(ISCAPI_MCTX_VALID(mctx
));
215 isc_mem_getname(isc_mem_t
*mctx
) {
216 REQUIRE(ISCAPI_MCTX_VALID(mctx
));
222 isc_mem_gettag(isc_mem_t
*mctx
) {
223 REQUIRE(ISCAPI_MCTX_VALID(mctx
));
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
));
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
);
245 isc__mempool_get(isc_mempool_t
*mpctx FLARG
) {
246 REQUIRE(ISCAPI_MPOOL_VALID(mpctx
));
248 return (mpctx
->methods
->get(mpctx FLARG_PASS
));
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
);
259 isc_mempool_getallocated(isc_mempool_t
*mpctx
) {
260 REQUIRE(ISCAPI_MPOOL_VALID(mpctx
));
262 return (mpctx
->methods
->getallocated(mpctx
));
266 isc_mempool_setmaxalloc(isc_mempool_t
*mpctx
, unsigned int limit
) {
267 REQUIRE(ISCAPI_MPOOL_VALID(mpctx
));
269 mpctx
->methods
->setmaxalloc(mpctx
, limit
);
273 isc_mempool_setfreemax(isc_mempool_t
*mpctx
, unsigned int limit
) {
274 REQUIRE(ISCAPI_MPOOL_VALID(mpctx
));
276 mpctx
->methods
->setfreemax(mpctx
, limit
);
280 isc_mempool_setname(isc_mempool_t
*mpctx
, const char *name
) {
281 REQUIRE(ISCAPI_MPOOL_VALID(mpctx
));
283 mpctx
->methods
->setname(mpctx
, name
);
287 isc_mempool_associatelock(isc_mempool_t
*mpctx
, isc_mutex_t
*lock
) {
288 REQUIRE(ISCAPI_MPOOL_VALID(mpctx
));
290 mpctx
->methods
->associatelock(mpctx
, lock
);
294 isc_mempool_setfillcount(isc_mempool_t
*mpctx
, unsigned int limit
) {
295 REQUIRE(ISCAPI_MPOOL_VALID(mpctx
));
297 mpctx
->methods
->setfillcount(mpctx
, limit
);