1 /* $NetBSD: mem.h,v 1.13 2014/12/10 04:38:00 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2004-2013 Internet Systems Consortium, Inc. ("ISC") 5 * Copyright (C) 1997-2001 Internet Software Consortium. 6 * 7 * Permission to use, copy, modify, and/or distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* Id */ 21 22 #ifndef ISC_MEM_H 23 #define ISC_MEM_H 1 24 25 /*! \file isc/mem.h */ 26 27 #include <stdio.h> 28 29 #include <isc/json.h> 30 #include <isc/lang.h> 31 #include <isc/mutex.h> 32 #include <isc/platform.h> 33 #include <isc/types.h> 34 #include <isc/xml.h> 35 36 ISC_LANG_BEGINDECLS 37 38 #define ISC_MEM_LOWATER 0 39 #define ISC_MEM_HIWATER 1 40 typedef void (*isc_mem_water_t)(void *, int); 41 42 typedef void * (*isc_memalloc_t)(void *, size_t); 43 typedef void (*isc_memfree_t)(void *, void *); 44 45 /*% 46 * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory 47 * allocation and freeing by file and line number. 48 */ 49 #ifndef ISC_MEM_TRACKLINES 50 #define ISC_MEM_TRACKLINES 1 51 #endif 52 53 /*% 54 * Define ISC_MEM_CHECKOVERRUN=1 to turn on checks for using memory outside 55 * the requested space. This will increase the size of each allocation. 56 */ 57 #ifndef ISC_MEM_CHECKOVERRUN 58 #define ISC_MEM_CHECKOVERRUN 1 59 #endif 60 61 /*% 62 * Define ISC_MEM_FILL=1 to fill each block of memory returned to the system 63 * with the byte string '0xbe'. This helps track down uninitialized pointers 64 * and the like. On freeing memory, the space is filled with '0xde' for 65 * the same reasons. 66 */ 67 #ifndef ISC_MEM_FILL 68 #define ISC_MEM_FILL 1 69 #endif 70 71 /*% 72 * Define ISC_MEMPOOL_NAMES=1 to make memory pools store a symbolic 73 * name so that the leaking pool can be more readily identified in 74 * case of a memory leak. 75 */ 76 #ifndef ISC_MEMPOOL_NAMES 77 #define ISC_MEMPOOL_NAMES 1 78 #endif 79 80 LIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging; 81 /*@{*/ 82 #define ISC_MEM_DEBUGTRACE 0x00000001U 83 #define ISC_MEM_DEBUGRECORD 0x00000002U 84 #define ISC_MEM_DEBUGUSAGE 0x00000004U 85 #define ISC_MEM_DEBUGSIZE 0x00000008U 86 #define ISC_MEM_DEBUGCTX 0x00000010U 87 #define ISC_MEM_DEBUGALL 0x0000001FU 88 /*!< 89 * The variable isc_mem_debugging holds a set of flags for 90 * turning certain memory debugging options on or off at 91 * runtime. It is initialized to the value ISC_MEM_DEGBUGGING, 92 * which is 0 by default but may be overridden at compile time. 93 * The following flags can be specified: 94 * 95 * \li #ISC_MEM_DEBUGTRACE 96 * Log each allocation and free to isc_lctx. 97 * 98 * \li #ISC_MEM_DEBUGRECORD 99 * Remember each allocation, and match them up on free. 100 * Crash if a free doesn't match an allocation. 101 * 102 * \li #ISC_MEM_DEBUGUSAGE 103 * If a hi_water mark is set, print the maximum inuse memory 104 * every time it is raised once it exceeds the hi_water mark. 105 * 106 * \li #ISC_MEM_DEBUGSIZE 107 * Check the size argument being passed to isc_mem_put() matches 108 * that passed to isc_mem_get(). 109 * 110 * \li #ISC_MEM_DEBUGCTX 111 * Check the mctx argument being passed to isc_mem_put() matches 112 * that passed to isc_mem_get(). 113 */ 114 /*@}*/ 115 116 #if ISC_MEM_TRACKLINES 117 #define _ISC_MEM_FILELINE , __FILE__, __LINE__ 118 #define _ISC_MEM_FLARG , const char *, unsigned int 119 #else 120 #define _ISC_MEM_FILELINE 121 #define _ISC_MEM_FLARG 122 #endif 123 124 /*! 125 * Define ISC_MEM_USE_INTERNAL_MALLOC=1 to use the internal malloc() 126 * implementation in preference to the system one. The internal malloc() 127 * is very space-efficient, and quite fast on uniprocessor systems. It 128 * performs poorly on multiprocessor machines. 129 * JT: we can overcome the performance issue on multiprocessor machines 130 * by carefully separating memory contexts. 131 */ 132 133 #ifndef ISC_MEM_USE_INTERNAL_MALLOC 134 #define ISC_MEM_USE_INTERNAL_MALLOC 1 135 #endif 136 137 /* 138 * Flags for isc_mem_create2()calls. 139 */ 140 #define ISC_MEMFLAG_NOLOCK 0x00000001 /* no lock is necessary */ 141 #define ISC_MEMFLAG_INTERNAL 0x00000002 /* use internal malloc */ 142 #if ISC_MEM_USE_INTERNAL_MALLOC 143 #define ISC_MEMFLAG_DEFAULT ISC_MEMFLAG_INTERNAL 144 #else 145 #define ISC_MEMFLAG_DEFAULT 0 146 #endif 147 148 149 /*%< 150 * We use either isc___mem (three underscores) or isc__mem (two) depending on 151 * whether it's for BIND9's internal purpose (with -DBIND9) or generic export 152 * library. 153 */ 154 #define ISCMEMFUNC(sfx) isc__mem_ ## sfx 155 #define ISCMEMPOOLFUNC(sfx) isc__mempool_ ## sfx 156 157 #define isc_mem_get(c, s) ISCMEMFUNC(get)((c), (s) _ISC_MEM_FILELINE) 158 #define isc_mem_allocate(c, s) ISCMEMFUNC(allocate)((c), (s) _ISC_MEM_FILELINE) 159 #define isc_mem_reallocate(c, p, s) ISCMEMFUNC(reallocate)((c), (p), (s) _ISC_MEM_FILELINE) 160 #define isc_mem_strdup(c, p) ISCMEMFUNC(strdup)((c), (p) _ISC_MEM_FILELINE) 161 #define isc_mempool_get(c) ISCMEMPOOLFUNC(get)((c) _ISC_MEM_FILELINE) 162 163 /*% 164 * isc_mem_putanddetach() is a convenience function for use where you 165 * have a structure with an attached memory context. 166 * 167 * Given: 168 * 169 * \code 170 * struct { 171 * ... 172 * isc_mem_t *mctx; 173 * ... 174 * } *ptr; 175 * 176 * isc_mem_t *mctx; 177 * 178 * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr)); 179 * \endcode 180 * 181 * is the equivalent of: 182 * 183 * \code 184 * mctx = NULL; 185 * isc_mem_attach(ptr->mctx, &mctx); 186 * isc_mem_detach(&ptr->mctx); 187 * isc_mem_put(mctx, ptr, sizeof(*ptr)); 188 * isc_mem_detach(&mctx); 189 * \endcode 190 */ 191 192 /*% memory and memory pool methods */ 193 typedef struct isc_memmethods { 194 void (*attach)(isc_mem_t *source, isc_mem_t **targetp); 195 void (*detach)(isc_mem_t **mctxp); 196 void (*destroy)(isc_mem_t **mctxp); 197 void *(*memget)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG); 198 void (*memput)(isc_mem_t *mctx, void *ptr, size_t size _ISC_MEM_FLARG); 199 void (*memputanddetach)(isc_mem_t **mctxp, void *ptr, 200 size_t size _ISC_MEM_FLARG); 201 void *(*memallocate)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG); 202 void *(*memreallocate)(isc_mem_t *mctx, void *ptr, 203 size_t size _ISC_MEM_FLARG); 204 char *(*memstrdup)(isc_mem_t *mctx, const char *s _ISC_MEM_FLARG); 205 void (*memfree)(isc_mem_t *mctx, void *ptr _ISC_MEM_FLARG); 206 void (*setdestroycheck)(isc_mem_t *mctx, isc_boolean_t flag); 207 void (*setwater)(isc_mem_t *ctx, isc_mem_water_t water, 208 void *water_arg, size_t hiwater, size_t lowater); 209 void (*waterack)(isc_mem_t *ctx, int flag); 210 size_t (*inuse)(isc_mem_t *mctx); 211 size_t (*maxinuse)(isc_mem_t *mctx); 212 size_t (*total)(isc_mem_t *mctx); 213 isc_boolean_t (*isovermem)(isc_mem_t *mctx); 214 isc_result_t (*mpcreate)(isc_mem_t *mctx, size_t size, 215 isc_mempool_t **mpctxp); 216 } isc_memmethods_t; 217 218 typedef struct isc_mempoolmethods { 219 void (*destroy)(isc_mempool_t **mpctxp); 220 void *(*get)(isc_mempool_t *mpctx _ISC_MEM_FLARG); 221 void (*put)(isc_mempool_t *mpctx, void *mem _ISC_MEM_FLARG); 222 unsigned int (*getallocated)(isc_mempool_t *mpctx); 223 void (*setmaxalloc)(isc_mempool_t *mpctx, unsigned int limit); 224 void (*setfreemax)(isc_mempool_t *mpctx, unsigned int limit); 225 void (*setname)(isc_mempool_t *mpctx, const char *name); 226 void (*associatelock)(isc_mempool_t *mpctx, isc_mutex_t *lock); 227 void (*setfillcount)(isc_mempool_t *mpctx, unsigned int limit); 228 } isc_mempoolmethods_t; 229 230 /*% 231 * This structure is actually just the common prefix of a memory context 232 * implementation's version of an isc_mem_t. 233 * \brief 234 * Direct use of this structure by clients is forbidden. mctx implementations 235 * may change the structure. 'magic' must be ISCAPI_MCTX_MAGIC for any of the 236 * isc_mem_ routines to work. mctx implementations must maintain all mctx 237 * invariants. 238 */ 239 struct isc_mem { 240 unsigned int impmagic; 241 unsigned int magic; 242 isc_memmethods_t *methods; 243 }; 244 245 #define ISCAPI_MCTX_MAGIC ISC_MAGIC('A','m','c','x') 246 #define ISCAPI_MCTX_VALID(m) ((m) != NULL && \ 247 (m)->magic == ISCAPI_MCTX_MAGIC) 248 249 /*% 250 * This is the common prefix of a memory pool context. The same note as 251 * that for the mem structure applies. 252 */ 253 struct isc_mempool { 254 unsigned int impmagic; 255 unsigned int magic; 256 isc_mempoolmethods_t *methods; 257 }; 258 259 #define ISCAPI_MPOOL_MAGIC ISC_MAGIC('A','m','p','l') 260 #define ISCAPI_MPOOL_VALID(mp) ((mp) != NULL && \ 261 (mp)->magic == ISCAPI_MPOOL_MAGIC) 262 263 #define isc_mem_put(c, p, s) \ 264 do { \ 265 ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE); \ 266 (p) = NULL; \ 267 } while (/*CONSTCOND*/0) 268 #define isc_mem_putanddetach(c, p, s) \ 269 do { \ 270 ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE); \ 271 (p) = NULL; \ 272 } while (/*CONSTCOND*/0) 273 #define isc_mem_free(c, p) \ 274 do { \ 275 ISCMEMFUNC(free)((c), (p) _ISC_MEM_FILELINE); \ 276 (p) = NULL; \ 277 } while (/*CONSTCOND*/0) 278 #define isc_mempool_put(c, p) \ 279 do { \ 280 ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE); \ 281 (p) = NULL; \ 282 } while (/*CONSTCOND*/0) 283 284 /*@{*/ 285 isc_result_t 286 isc_mem_create(size_t max_size, size_t target_size, 287 isc_mem_t **mctxp); 288 289 isc_result_t 290 isc_mem_create2(size_t max_size, size_t target_size, 291 isc_mem_t **mctxp, unsigned int flags); 292 293 isc_result_t 294 isc_mem_createx(size_t max_size, size_t target_size, 295 isc_memalloc_t memalloc, isc_memfree_t memfree, 296 void *arg, isc_mem_t **mctxp); 297 298 isc_result_t 299 isc_mem_createx2(size_t max_size, size_t target_size, 300 isc_memalloc_t memalloc, isc_memfree_t memfree, 301 void *arg, isc_mem_t **mctxp, unsigned int flags); 302 303 /*!< 304 * \brief Create a memory context. 305 * 306 * 'max_size' and 'target_size' are tuning parameters. When 307 * ISC_MEMFLAG_INTERNAL is set, allocations smaller than 'max_size' 308 * will be satisfied by getting blocks of size 'target_size' from the 309 * system allocator and breaking them up into pieces; larger allocations 310 * will use the system allocator directly. If 'max_size' and/or 311 * 'target_size' are zero, default values will be * used. When 312 * ISC_MEMFLAG_INTERNAL is not set, 'target_size' is ignored. 313 * 314 * 'max_size' is also used to size the statistics arrays and the array 315 * used to record active memory when ISC_MEM_DEBUGRECORD is set. Setting 316 * 'max_size' too low can have detrimental effects on performance. 317 * 318 * A memory context created using isc_mem_createx() will obtain 319 * memory from the system by calling 'memalloc' and 'memfree', 320 * passing them the argument 'arg'. A memory context created 321 * using isc_mem_create() will use the standard library malloc() 322 * and free(). 323 * 324 * If ISC_MEMFLAG_NOLOCK is set in 'flags', the corresponding memory context 325 * will be accessed without locking. The user who creates the context must 326 * ensure there be no race. Since this can be a source of bug, it is generally 327 * inadvisable to use this flag unless the user is very sure about the race 328 * condition and the access to the object is highly performance sensitive. 329 * 330 * Requires: 331 * mctxp != NULL && *mctxp == NULL */ 332 /*@}*/ 333 334 /*@{*/ 335 void 336 isc_mem_attach(isc_mem_t *, isc_mem_t **); 337 void 338 isc_mem_detach(isc_mem_t **); 339 /*!< 340 * \brief Attach to / detach from a memory context. 341 * 342 * This is intended for applications that use multiple memory contexts 343 * in such a way that it is not obvious when the last allocations from 344 * a given context has been freed and destroying the context is safe. 345 * 346 * Most applications do not need to call these functions as they can 347 * simply create a single memory context at the beginning of main() 348 * and destroy it at the end of main(), thereby guaranteeing that it 349 * is not destroyed while there are outstanding allocations. 350 */ 351 /*@}*/ 352 353 void 354 isc_mem_destroy(isc_mem_t **); 355 /*%< 356 * Destroy a memory context. 357 */ 358 359 isc_result_t 360 isc_mem_ondestroy(isc_mem_t *ctx, 361 isc_task_t *task, 362 isc_event_t **event); 363 /*%< 364 * Request to be notified with an event when a memory context has 365 * been successfully destroyed. 366 */ 367 368 void 369 isc_mem_stats(isc_mem_t *mctx, FILE *out); 370 /*%< 371 * Print memory usage statistics for 'mctx' on the stream 'out'. 372 */ 373 374 void 375 isc_mem_setdestroycheck(isc_mem_t *mctx, 376 isc_boolean_t on); 377 /*%< 378 * If 'on' is ISC_TRUE, 'mctx' will check for memory leaks when 379 * destroyed and abort the program if any are present. 380 */ 381 382 /*@{*/ 383 void 384 isc_mem_setquota(isc_mem_t *, size_t); 385 size_t 386 isc_mem_getquota(isc_mem_t *); 387 /*%< 388 * Set/get the memory quota of 'mctx'. This is a hard limit 389 * on the amount of memory that may be allocated from mctx; 390 * if it is exceeded, allocations will fail. 391 */ 392 /*@}*/ 393 394 size_t 395 isc_mem_inuse(isc_mem_t *mctx); 396 /*%< 397 * Get an estimate of the amount of memory in use in 'mctx', in bytes. 398 * This includes quantization overhead, but does not include memory 399 * allocated from the system but not yet used. 400 */ 401 402 size_t 403 isc_mem_maxinuse(isc_mem_t *mctx); 404 /*%< 405 * Get an estimate of the largest amount of memory that has been in 406 * use in 'mctx' at any time. 407 */ 408 409 size_t 410 isc_mem_total(isc_mem_t *mctx); 411 /*%< 412 * Get the total amount of memory in 'mctx', in bytes, including memory 413 * not yet used. 414 */ 415 416 isc_boolean_t 417 isc_mem_isovermem(isc_mem_t *mctx); 418 /*%< 419 * Return true iff the memory context is in "over memory" state, i.e., 420 * a hiwater mark has been set and the used amount of memory has exceeds 421 * the mark. 422 */ 423 424 void 425 isc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg, 426 size_t hiwater, size_t lowater); 427 /*%< 428 * Set high and low water marks for this memory context. 429 * 430 * When the memory usage of 'mctx' exceeds 'hiwater', 431 * '(water)(water_arg, #ISC_MEM_HIWATER)' will be called. 'water' needs to 432 * call isc_mem_waterack() with #ISC_MEM_HIWATER to acknowledge the state 433 * change. 'water' may be called multiple times. 434 * 435 * When the usage drops below 'lowater', 'water' will again be called, this 436 * time with #ISC_MEM_LOWATER. 'water' need to calls isc_mem_waterack() with 437 * #ISC_MEM_LOWATER to acknowledge the change. 438 * 439 * static void 440 * water(void *arg, int mark) { 441 * struct foo *foo = arg; 442 * 443 * LOCK(&foo->marklock); 444 * if (foo->mark != mark) { 445 * foo->mark = mark; 446 * .... 447 * isc_mem_waterack(foo->mctx, mark); 448 * } 449 * UNLOCK(&foo->marklock); 450 * } 451 * 452 * If 'water' is NULL then 'water_arg', 'hi_water' and 'lo_water' are 453 * ignored and the state is reset. 454 * 455 * Requires: 456 * 457 * 'water' is not NULL. 458 * hi_water >= lo_water 459 */ 460 461 void 462 isc_mem_waterack(isc_mem_t *ctx, int mark); 463 /*%< 464 * Called to acknowledge changes in signaled by calls to 'water'. 465 */ 466 467 void 468 isc_mem_printactive(isc_mem_t *mctx, FILE *file); 469 /*%< 470 * Print to 'file' all active memory in 'mctx'. 471 * 472 * Requires ISC_MEM_DEBUGRECORD to have been set. 473 */ 474 475 void 476 isc_mem_printallactive(FILE *file); 477 /*%< 478 * Print to 'file' all active memory in all contexts. 479 * 480 * Requires ISC_MEM_DEBUGRECORD to have been set. 481 */ 482 483 void 484 isc_mem_checkdestroyed(FILE *file); 485 /*%< 486 * Check that all memory contexts have been destroyed. 487 * Prints out those that have not been. 488 * Fatally fails if there are still active contexts. 489 */ 490 491 unsigned int 492 isc_mem_references(isc_mem_t *ctx); 493 /*%< 494 * Return the current reference count. 495 */ 496 497 void 498 isc_mem_setname(isc_mem_t *ctx, const char *name, void *tag); 499 /*%< 500 * Name 'ctx'. 501 * 502 * Notes: 503 * 504 *\li Only the first 15 characters of 'name' will be copied. 505 * 506 *\li 'tag' is for debugging purposes only. 507 * 508 * Requires: 509 * 510 *\li 'ctx' is a valid ctx. 511 */ 512 513 const char * 514 isc_mem_getname(isc_mem_t *ctx); 515 /*%< 516 * Get the name of 'ctx', as previously set using isc_mem_setname(). 517 * 518 * Requires: 519 *\li 'ctx' is a valid ctx. 520 * 521 * Returns: 522 *\li A non-NULL pointer to a null-terminated string. 523 * If the ctx has not been named, the string is 524 * empty. 525 */ 526 527 void * 528 isc_mem_gettag(isc_mem_t *ctx); 529 /*%< 530 * Get the tag value for 'task', as previously set using isc_mem_setname(). 531 * 532 * Requires: 533 *\li 'ctx' is a valid ctx. 534 * 535 * Notes: 536 *\li This function is for debugging purposes only. 537 * 538 * Requires: 539 *\li 'ctx' is a valid task. 540 */ 541 542 #ifdef HAVE_LIBXML2 543 int 544 isc_mem_renderxml(xmlTextWriterPtr writer); 545 /*%< 546 * Render all contexts' statistics and status in XML for writer. 547 */ 548 #endif /* HAVE_LIBXML2 */ 549 550 #ifdef HAVE_JSON 551 isc_result_t 552 isc_mem_renderjson(json_object *memobj); 553 /*%< 554 * Render all contexts' statistics and status in JSON. 555 */ 556 #endif /* HAVE_JSON */ 557 558 559 /* 560 * Memory pools 561 */ 562 563 isc_result_t 564 isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp); 565 /*%< 566 * Create a memory pool. 567 * 568 * Requires: 569 *\li mctx is a valid memory context. 570 *\li size > 0 571 *\li mpctxp != NULL and *mpctxp == NULL 572 * 573 * Defaults: 574 *\li maxalloc = UINT_MAX 575 *\li freemax = 1 576 *\li fillcount = 1 577 * 578 * Returns: 579 *\li #ISC_R_NOMEMORY -- not enough memory to create pool 580 *\li #ISC_R_SUCCESS -- all is well. 581 */ 582 583 void 584 isc_mempool_destroy(isc_mempool_t **mpctxp); 585 /*%< 586 * Destroy a memory pool. 587 * 588 * Requires: 589 *\li mpctxp != NULL && *mpctxp is a valid pool. 590 *\li The pool has no un"put" allocations outstanding 591 */ 592 593 void 594 isc_mempool_setname(isc_mempool_t *mpctx, const char *name); 595 /*%< 596 * Associate a name with a memory pool. At most 15 characters may be used. 597 * 598 * Requires: 599 *\li mpctx is a valid pool. 600 *\li name != NULL; 601 */ 602 603 void 604 isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock); 605 /*%< 606 * Associate a lock with this memory pool. 607 * 608 * This lock is used when getting or putting items using this memory pool, 609 * and it is also used to set or get internal state via the isc_mempool_get*() 610 * and isc_mempool_set*() set of functions. 611 * 612 * Multiple pools can each share a single lock. For instance, if "manager" 613 * type object contained pools for various sizes of events, and each of 614 * these pools used a common lock. Note that this lock must NEVER be used 615 * by other than mempool routines once it is given to a pool, since that can 616 * easily cause double locking. 617 * 618 * Requires: 619 * 620 *\li mpctpx is a valid pool. 621 * 622 *\li lock != NULL. 623 * 624 *\li No previous lock is assigned to this pool. 625 * 626 *\li The lock is initialized before calling this function via the normal 627 * means of doing that. 628 */ 629 630 /* 631 * The following functions get/set various parameters. Note that due to 632 * the unlocked nature of pools these are potentially random values unless 633 * the imposed externally provided locking protocols are followed. 634 * 635 * Also note that the quota limits will not always take immediate effect. 636 * For instance, setting "maxalloc" to a number smaller than the currently 637 * allocated count is permitted. New allocations will be refused until 638 * the count drops below this threshold. 639 * 640 * All functions require (in addition to other requirements): 641 * mpctx is a valid memory pool 642 */ 643 644 unsigned int 645 isc_mempool_getfreemax(isc_mempool_t *mpctx); 646 /*%< 647 * Returns the maximum allowed size of the free list. 648 */ 649 650 void 651 isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit); 652 /*%< 653 * Sets the maximum allowed size of the free list. 654 */ 655 656 unsigned int 657 isc_mempool_getfreecount(isc_mempool_t *mpctx); 658 /*%< 659 * Returns current size of the free list. 660 */ 661 662 unsigned int 663 isc_mempool_getmaxalloc(isc_mempool_t *mpctx); 664 /*!< 665 * Returns the maximum allowed number of allocations. 666 */ 667 668 void 669 isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit); 670 /*%< 671 * Sets the maximum allowed number of allocations. 672 * 673 * Additional requirements: 674 *\li limit > 0 675 */ 676 677 unsigned int 678 isc_mempool_getallocated(isc_mempool_t *mpctx); 679 /*%< 680 * Returns the number of items allocated from this pool. 681 */ 682 683 unsigned int 684 isc_mempool_getfillcount(isc_mempool_t *mpctx); 685 /*%< 686 * Returns the number of items allocated as a block from the parent memory 687 * context when the free list is empty. 688 */ 689 690 void 691 isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit); 692 /*%< 693 * Sets the fillcount. 694 * 695 * Additional requirements: 696 *\li limit > 0 697 */ 698 699 700 /* 701 * Pseudo-private functions for use via macros. Do not call directly. 702 */ 703 void * 704 ISCMEMFUNC(get)(isc_mem_t *, size_t _ISC_MEM_FLARG); 705 void 706 ISCMEMFUNC(putanddetach)(isc_mem_t **, void *, size_t _ISC_MEM_FLARG); 707 void 708 ISCMEMFUNC(put)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG); 709 void * 710 ISCMEMFUNC(allocate)(isc_mem_t *, size_t _ISC_MEM_FLARG); 711 void * 712 ISCMEMFUNC(reallocate)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG); 713 void 714 ISCMEMFUNC(free)(isc_mem_t *, void * _ISC_MEM_FLARG); 715 char * 716 ISCMEMFUNC(strdup)(isc_mem_t *, const char *_ISC_MEM_FLARG); 717 void * 718 ISCMEMPOOLFUNC(get)(isc_mempool_t * _ISC_MEM_FLARG); 719 void 720 ISCMEMPOOLFUNC(put)(isc_mempool_t *, void * _ISC_MEM_FLARG); 721 722 /*%< 723 * See isc_mem_create2() above. 724 */ 725 typedef isc_result_t 726 (*isc_memcreatefunc_t)(size_t init_max_size, size_t target_size, 727 isc_mem_t **ctxp, unsigned int flags); 728 729 isc_result_t 730 isc_mem_register(isc_memcreatefunc_t createfunc); 731 /*%< 732 * Register a new memory management implementation and add it to the list of 733 * supported implementations. This function must be called when a different 734 * memory management library is used than the one contained in the ISC library. 735 */ 736 737 isc_result_t 738 isc__mem_register(void); 739 /*%< 740 * A short cut function that specifies the memory management module in the ISC 741 * library for isc_mem_register(). An application that uses the ISC library 742 * usually do not have to care about this function: it would call 743 * isc_lib_register(), which internally calls this function. 744 */ 745 746 ISC_LANG_ENDDECLS 747 748 #endif /* ISC_MEM_H */ 749