mem.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-2013, 2015  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1997-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 #ifndef ISC_MEM_H
00019 #define ISC_MEM_H 1
00020 
00021 /*! \file isc/mem.h */
00022 
00023 #include <stdio.h>
00024 
00025 #include <isc/json.h>
00026 #include <isc/lang.h>
00027 #include <isc/mutex.h>
00028 #include <isc/platform.h>
00029 #include <isc/types.h>
00030 #include <isc/xml.h>
00031 
00032 ISC_LANG_BEGINDECLS
00033 
00034 #define ISC_MEM_LOWATER 0
00035 #define ISC_MEM_HIWATER 1
00036 typedef void (*isc_mem_water_t)(void *, int);
00037 
00038 typedef void * (*isc_memalloc_t)(void *, size_t);
00039 typedef void (*isc_memfree_t)(void *, void *);
00040 
00041 /*%
00042  * Define ISC_MEM_TRACKLINES=1 to turn on detailed tracing of memory
00043  * allocation and freeing by file and line number.
00044  */
00045 #ifndef ISC_MEM_TRACKLINES
00046 #define ISC_MEM_TRACKLINES 1
00047 #endif
00048 
00049 /*%
00050  * Define ISC_MEM_CHECKOVERRUN=1 to turn on checks for using memory outside
00051  * the requested space.  This will increase the size of each allocation.
00052  *
00053  * If we are performing a Coverity static analysis then ISC_MEM_CHECKOVERRUN
00054  * can hide bugs that would otherwise discovered so force to zero.
00055  */
00056 #ifdef __COVERITY__
00057 #undef ISC_MEM_CHECKOVERRUN
00058 #define ISC_MEM_CHECKOVERRUN 0
00059 #endif
00060 #ifndef ISC_MEM_CHECKOVERRUN
00061 #define ISC_MEM_CHECKOVERRUN 1
00062 #endif
00063 
00064 /*%
00065  * Define ISC_MEM_FILL=1 to fill each block of memory returned to the system
00066  * with the byte string '0xbe'.  This helps track down uninitialized pointers
00067  * and the like.  On freeing memory, the space is filled with '0xde' for
00068  * the same reasons.
00069  *
00070  * If we are performing a Coverity static analysis then ISC_MEM_FILL
00071  * can hide bugs that would otherwise discovered so force to zero.
00072  */
00073 #ifdef __COVERITY__
00074 #undef ISC_MEM_FILL
00075 #define ISC_MEM_FILL 0
00076 #endif
00077 #ifndef ISC_MEM_FILL
00078 #define ISC_MEM_FILL 1
00079 #endif
00080 
00081 /*%
00082  * Define ISC_MEMPOOL_NAMES=1 to make memory pools store a symbolic
00083  * name so that the leaking pool can be more readily identified in
00084  * case of a memory leak.
00085  */
00086 #ifndef ISC_MEMPOOL_NAMES
00087 #define ISC_MEMPOOL_NAMES 1
00088 #endif
00089 
00090 LIBISC_EXTERNAL_DATA extern unsigned int isc_mem_debugging;
00091 LIBISC_EXTERNAL_DATA extern unsigned int isc_mem_defaultflags;
00092 
00093 /*@{*/
00094 #define ISC_MEM_DEBUGTRACE              0x00000001U
00095 #define ISC_MEM_DEBUGRECORD             0x00000002U
00096 #define ISC_MEM_DEBUGUSAGE              0x00000004U
00097 #define ISC_MEM_DEBUGSIZE               0x00000008U
00098 #define ISC_MEM_DEBUGCTX                0x00000010U
00099 #define ISC_MEM_DEBUGALL                0x0000001FU
00100 /*!<
00101  * The variable isc_mem_debugging holds a set of flags for
00102  * turning certain memory debugging options on or off at
00103  * runtime.  It is initialized to the value ISC_MEM_DEGBUGGING,
00104  * which is 0 by default but may be overridden at compile time.
00105  * The following flags can be specified:
00106  *
00107  * \li #ISC_MEM_DEBUGTRACE
00108  *      Log each allocation and free to isc_lctx.
00109  *
00110  * \li #ISC_MEM_DEBUGRECORD
00111  *      Remember each allocation, and match them up on free.
00112  *      Crash if a free doesn't match an allocation.
00113  *
00114  * \li #ISC_MEM_DEBUGUSAGE
00115  *      If a hi_water mark is set, print the maximum inuse memory
00116  *      every time it is raised once it exceeds the hi_water mark.
00117  *
00118  * \li #ISC_MEM_DEBUGSIZE
00119  *      Check the size argument being passed to isc_mem_put() matches
00120  *      that passed to isc_mem_get().
00121  *
00122  * \li #ISC_MEM_DEBUGCTX
00123  *      Check the mctx argument being passed to isc_mem_put() matches
00124  *      that passed to isc_mem_get().
00125  */
00126 /*@}*/
00127 
00128 #if ISC_MEM_TRACKLINES
00129 #define _ISC_MEM_FILELINE       , __FILE__, __LINE__
00130 #define _ISC_MEM_FLARG          , const char *, unsigned int
00131 #else
00132 #define _ISC_MEM_FILELINE
00133 #define _ISC_MEM_FLARG
00134 #endif
00135 
00136 /*!
00137  * Define ISC_MEM_USE_INTERNAL_MALLOC=1 to use the internal malloc()
00138  * implementation in preference to the system one.  The internal malloc()
00139  * is very space-efficient, and quite fast on uniprocessor systems.  It
00140  * performs poorly on multiprocessor machines.
00141  * JT: we can overcome the performance issue on multiprocessor machines
00142  * by carefully separating memory contexts.
00143  */
00144 
00145 #ifndef ISC_MEM_USE_INTERNAL_MALLOC
00146 #define ISC_MEM_USE_INTERNAL_MALLOC 1
00147 #endif
00148 
00149 /*
00150  * Flags for isc_mem_create2()calls.
00151  */
00152 #define ISC_MEMFLAG_NOLOCK      0x00000001       /* no lock is necessary */
00153 #define ISC_MEMFLAG_INTERNAL    0x00000002       /* use internal malloc */
00154 #if ISC_MEM_USE_INTERNAL_MALLOC
00155 #define ISC_MEMFLAG_DEFAULT     ISC_MEMFLAG_INTERNAL
00156 #else
00157 #define ISC_MEMFLAG_DEFAULT     0
00158 #endif
00159 
00160 
00161 /*%<
00162  * We use either isc___mem (three underscores) or isc__mem (two) depending on
00163  * whether it's for BIND9's internal purpose (with -DBIND9) or generic export
00164  * library.
00165  */
00166 #define ISCMEMFUNC(sfx) isc__mem_ ## sfx
00167 #define ISCMEMPOOLFUNC(sfx) isc__mempool_ ## sfx
00168 
00169 #define isc_mem_get(c, s)       ISCMEMFUNC(get)((c), (s) _ISC_MEM_FILELINE)
00170 #define isc_mem_allocate(c, s)  ISCMEMFUNC(allocate)((c), (s) _ISC_MEM_FILELINE)
00171 #define isc_mem_reallocate(c, p, s) ISCMEMFUNC(reallocate)((c), (p), (s) _ISC_MEM_FILELINE)
00172 #define isc_mem_strdup(c, p)    ISCMEMFUNC(strdup)((c), (p) _ISC_MEM_FILELINE)
00173 #define isc_mempool_get(c)      ISCMEMPOOLFUNC(get)((c) _ISC_MEM_FILELINE)
00174 
00175 /*%
00176  * isc_mem_putanddetach() is a convenience function for use where you
00177  * have a structure with an attached memory context.
00178  *
00179  * Given:
00180  *
00181  * \code
00182  * struct {
00183  *      ...
00184  *      isc_mem_t *mctx;
00185  *      ...
00186  * } *ptr;
00187  *
00188  * isc_mem_t *mctx;
00189  *
00190  * isc_mem_putanddetach(&ptr->mctx, ptr, sizeof(*ptr));
00191  * \endcode
00192  *
00193  * is the equivalent of:
00194  *
00195  * \code
00196  * mctx = NULL;
00197  * isc_mem_attach(ptr->mctx, &mctx);
00198  * isc_mem_detach(&ptr->mctx);
00199  * isc_mem_put(mctx, ptr, sizeof(*ptr));
00200  * isc_mem_detach(&mctx);
00201  * \endcode
00202  */
00203 
00204 /*% memory and memory pool methods */
00205 typedef struct isc_memmethods {
00206         void (*attach)(isc_mem_t *source, isc_mem_t **targetp);
00207         void (*detach)(isc_mem_t **mctxp);
00208         void (*destroy)(isc_mem_t **mctxp);
00209         void *(*memget)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
00210         void (*memput)(isc_mem_t *mctx, void *ptr, size_t size _ISC_MEM_FLARG);
00211         void (*memputanddetach)(isc_mem_t **mctxp, void *ptr,
00212                                 size_t size _ISC_MEM_FLARG);
00213         void *(*memallocate)(isc_mem_t *mctx, size_t size _ISC_MEM_FLARG);
00214         void *(*memreallocate)(isc_mem_t *mctx, void *ptr,
00215                                size_t size _ISC_MEM_FLARG);
00216         char *(*memstrdup)(isc_mem_t *mctx, const char *s _ISC_MEM_FLARG);
00217         void (*memfree)(isc_mem_t *mctx, void *ptr _ISC_MEM_FLARG);
00218         void (*setdestroycheck)(isc_mem_t *mctx, isc_boolean_t flag);
00219         void (*setwater)(isc_mem_t *ctx, isc_mem_water_t water,
00220                          void *water_arg, size_t hiwater, size_t lowater);
00221         void (*waterack)(isc_mem_t *ctx, int flag);
00222         size_t (*inuse)(isc_mem_t *mctx);
00223         size_t (*maxinuse)(isc_mem_t *mctx);
00224         size_t (*total)(isc_mem_t *mctx);
00225         isc_boolean_t (*isovermem)(isc_mem_t *mctx);
00226         isc_result_t (*mpcreate)(isc_mem_t *mctx, size_t size,
00227                                  isc_mempool_t **mpctxp);
00228 } isc_memmethods_t;
00229 
00230 typedef struct isc_mempoolmethods {
00231         void (*destroy)(isc_mempool_t **mpctxp);
00232         void *(*get)(isc_mempool_t *mpctx _ISC_MEM_FLARG);
00233         void (*put)(isc_mempool_t *mpctx, void *mem _ISC_MEM_FLARG);
00234         unsigned int (*getallocated)(isc_mempool_t *mpctx);
00235         void (*setmaxalloc)(isc_mempool_t *mpctx, unsigned int limit);
00236         void (*setfreemax)(isc_mempool_t *mpctx, unsigned int limit);
00237         void (*setname)(isc_mempool_t *mpctx, const char *name);
00238         void (*associatelock)(isc_mempool_t *mpctx, isc_mutex_t *lock);
00239         void (*setfillcount)(isc_mempool_t *mpctx, unsigned int limit);
00240 } isc_mempoolmethods_t;
00241 
00242 /*%
00243  * This structure is actually just the common prefix of a memory context
00244  * implementation's version of an isc_mem_t.
00245  * \brief
00246  * Direct use of this structure by clients is forbidden.  mctx implementations
00247  * may change the structure.  'magic' must be ISCAPI_MCTX_MAGIC for any of the
00248  * isc_mem_ routines to work.  mctx implementations must maintain all mctx
00249  * invariants.
00250  */
00251 struct isc_mem {
00252         unsigned int            impmagic;
00253         unsigned int            magic;
00254         isc_memmethods_t        *methods;
00255 };
00256 
00257 #define ISCAPI_MCTX_MAGIC       ISC_MAGIC('A','m','c','x')
00258 #define ISCAPI_MCTX_VALID(m)    ((m) != NULL && \
00259                                  (m)->magic == ISCAPI_MCTX_MAGIC)
00260 
00261 /*%
00262  * This is the common prefix of a memory pool context.  The same note as
00263  * that for the mem structure applies.
00264  */
00265 struct isc_mempool {
00266         unsigned int            impmagic;
00267         unsigned int            magic;
00268         isc_mempoolmethods_t    *methods;
00269 };
00270 
00271 #define ISCAPI_MPOOL_MAGIC      ISC_MAGIC('A','m','p','l')
00272 #define ISCAPI_MPOOL_VALID(mp)  ((mp) != NULL && \
00273                                  (mp)->magic == ISCAPI_MPOOL_MAGIC)
00274 
00275 #define isc_mem_put(c, p, s) \
00276         do { \
00277                 ISCMEMFUNC(put)((c), (p), (s) _ISC_MEM_FILELINE);       \
00278                 (p) = NULL; \
00279         } while (0)
00280 #define isc_mem_putanddetach(c, p, s) \
00281         do { \
00282                 ISCMEMFUNC(putanddetach)((c), (p), (s) _ISC_MEM_FILELINE); \
00283                 (p) = NULL; \
00284         } while (0)
00285 #define isc_mem_free(c, p) \
00286         do { \
00287                 ISCMEMFUNC(free)((c), (p) _ISC_MEM_FILELINE);   \
00288                 (p) = NULL; \
00289         } while (0)
00290 #define isc_mempool_put(c, p) \
00291         do { \
00292                 ISCMEMPOOLFUNC(put)((c), (p) _ISC_MEM_FILELINE);        \
00293                 (p) = NULL; \
00294         } while (0)
00295 
00296 /*@{*/
00297 isc_result_t
00298 isc_mem_create(size_t max_size, size_t target_size,
00299                isc_mem_t **mctxp);
00300 
00301 isc_result_t
00302 isc_mem_create2(size_t max_size, size_t target_size,
00303                 isc_mem_t **mctxp, unsigned int flags);
00304 
00305 isc_result_t
00306 isc_mem_createx(size_t max_size, size_t target_size,
00307                 isc_memalloc_t memalloc, isc_memfree_t memfree,
00308                 void *arg, isc_mem_t **mctxp);
00309 
00310 isc_result_t
00311 isc_mem_createx2(size_t max_size, size_t target_size,
00312                  isc_memalloc_t memalloc, isc_memfree_t memfree,
00313                  void *arg, isc_mem_t **mctxp, unsigned int flags);
00314 
00315 /*!<
00316  * \brief Create a memory context.
00317  *
00318  * 'max_size' and 'target_size' are tuning parameters.  When
00319  * ISC_MEMFLAG_INTERNAL is set, allocations smaller than 'max_size'
00320  * will be satisfied by getting blocks of size 'target_size' from the
00321  * system allocator and breaking them up into pieces; larger allocations
00322  * will use the system allocator directly. If 'max_size' and/or
00323  * 'target_size' are zero, default values will be * used.  When
00324  * ISC_MEMFLAG_INTERNAL is not set, 'target_size' is ignored.
00325  *
00326  * 'max_size' is also used to size the statistics arrays and the array
00327  * used to record active memory when ISC_MEM_DEBUGRECORD is set.  Setting
00328  * 'max_size' too low can have detrimental effects on performance.
00329  *
00330  * A memory context created using isc_mem_createx() will obtain
00331  * memory from the system by calling 'memalloc' and 'memfree',
00332  * passing them the argument 'arg'.  A memory context created
00333  * using isc_mem_create() will use the standard library malloc()
00334  * and free().
00335  *
00336  * If ISC_MEMFLAG_NOLOCK is set in 'flags', the corresponding memory context
00337  * will be accessed without locking.  The user who creates the context must
00338  * ensure there be no race.  Since this can be a source of bug, it is generally
00339  * inadvisable to use this flag unless the user is very sure about the race
00340  * condition and the access to the object is highly performance sensitive.
00341  *
00342  * Requires:
00343  * mctxp != NULL && *mctxp == NULL */
00344 /*@}*/
00345 
00346 /*@{*/
00347 void
00348 isc_mem_attach(isc_mem_t *, isc_mem_t **);
00349 void
00350 isc_mem_detach(isc_mem_t **);
00351 /*!<
00352  * \brief Attach to / detach from a memory context.
00353  *
00354  * This is intended for applications that use multiple memory contexts
00355  * in such a way that it is not obvious when the last allocations from
00356  * a given context has been freed and destroying the context is safe.
00357  *
00358  * Most applications do not need to call these functions as they can
00359  * simply create a single memory context at the beginning of main()
00360  * and destroy it at the end of main(), thereby guaranteeing that it
00361  * is not destroyed while there are outstanding allocations.
00362  */
00363 /*@}*/
00364 
00365 void
00366 isc_mem_destroy(isc_mem_t **);
00367 /*%<
00368  * Destroy a memory context.
00369  */
00370 
00371 isc_result_t
00372 isc_mem_ondestroy(isc_mem_t *ctx,
00373                   isc_task_t *task,
00374                   isc_event_t **event);
00375 /*%<
00376  * Request to be notified with an event when a memory context has
00377  * been successfully destroyed.
00378  */
00379 
00380 void
00381 isc_mem_stats(isc_mem_t *mctx, FILE *out);
00382 /*%<
00383  * Print memory usage statistics for 'mctx' on the stream 'out'.
00384  */
00385 
00386 void
00387 isc_mem_setdestroycheck(isc_mem_t *mctx,
00388                         isc_boolean_t on);
00389 /*%<
00390  * If 'on' is ISC_TRUE, 'mctx' will check for memory leaks when
00391  * destroyed and abort the program if any are present.
00392  */
00393 
00394 /*@{*/
00395 void
00396 isc_mem_setquota(isc_mem_t *, size_t);
00397 size_t
00398 isc_mem_getquota(isc_mem_t *);
00399 /*%<
00400  * Set/get the memory quota of 'mctx'.  This is a hard limit
00401  * on the amount of memory that may be allocated from mctx;
00402  * if it is exceeded, allocations will fail.
00403  */
00404 /*@}*/
00405 
00406 size_t
00407 isc_mem_inuse(isc_mem_t *mctx);
00408 /*%<
00409  * Get an estimate of the amount of memory in use in 'mctx', in bytes.
00410  * This includes quantization overhead, but does not include memory
00411  * allocated from the system but not yet used.
00412  */
00413 
00414 size_t
00415 isc_mem_maxinuse(isc_mem_t *mctx);
00416 /*%<
00417  * Get an estimate of the largest amount of memory that has been in
00418  * use in 'mctx' at any time.
00419  */
00420 
00421 size_t
00422 isc_mem_total(isc_mem_t *mctx);
00423 /*%<
00424  * Get the total amount of memory in 'mctx', in bytes, including memory
00425  * not yet used.
00426  */
00427 
00428 isc_boolean_t
00429 isc_mem_isovermem(isc_mem_t *mctx);
00430 /*%<
00431  * Return true iff the memory context is in "over memory" state, i.e.,
00432  * a hiwater mark has been set and the used amount of memory has exceeds
00433  * the mark.
00434  */
00435 
00436 void
00437 isc_mem_setwater(isc_mem_t *mctx, isc_mem_water_t water, void *water_arg,
00438                  size_t hiwater, size_t lowater);
00439 /*%<
00440  * Set high and low water marks for this memory context.
00441  *
00442  * When the memory usage of 'mctx' exceeds 'hiwater',
00443  * '(water)(water_arg, #ISC_MEM_HIWATER)' will be called.  'water' needs to
00444  * call isc_mem_waterack() with #ISC_MEM_HIWATER to acknowledge the state
00445  * change.  'water' may be called multiple times.
00446  *
00447  * When the usage drops below 'lowater', 'water' will again be called, this
00448  * time with #ISC_MEM_LOWATER.  'water' need to calls isc_mem_waterack() with
00449  * #ISC_MEM_LOWATER to acknowledge the change.
00450  *
00451  *      static void
00452  *      water(void *arg, int mark) {
00453  *              struct foo *foo = arg;
00454  *
00455  *              LOCK(&foo->marklock);
00456  *              if (foo->mark != mark) {
00457  *                      foo->mark = mark;
00458  *                      ....
00459  *                      isc_mem_waterack(foo->mctx, mark);
00460  *              }
00461  *              UNLOCK(&foo->marklock);
00462  *      }
00463  *
00464  * If 'water' is NULL then 'water_arg', 'hi_water' and 'lo_water' are
00465  * ignored and the state is reset.
00466  *
00467  * Requires:
00468  *
00469  *      'water' is not NULL.
00470  *      hi_water >= lo_water
00471  */
00472 
00473 void
00474 isc_mem_waterack(isc_mem_t *ctx, int mark);
00475 /*%<
00476  * Called to acknowledge changes in signaled by calls to 'water'.
00477  */
00478 
00479 void
00480 isc_mem_printactive(isc_mem_t *mctx, FILE *file);
00481 /*%<
00482  * Print to 'file' all active memory in 'mctx'.
00483  *
00484  * Requires ISC_MEM_DEBUGRECORD to have been set.
00485  */
00486 
00487 void
00488 isc_mem_printallactive(FILE *file);
00489 /*%<
00490  * Print to 'file' all active memory in all contexts.
00491  *
00492  * Requires ISC_MEM_DEBUGRECORD to have been set.
00493  */
00494 
00495 void
00496 isc_mem_checkdestroyed(FILE *file);
00497 /*%<
00498  * Check that all memory contexts have been destroyed.
00499  * Prints out those that have not been.
00500  * Fatally fails if there are still active contexts.
00501  */
00502 
00503 unsigned int
00504 isc_mem_references(isc_mem_t *ctx);
00505 /*%<
00506  * Return the current reference count.
00507  */
00508 
00509 void
00510 isc_mem_setname(isc_mem_t *ctx, const char *name, void *tag);
00511 /*%<
00512  * Name 'ctx'.
00513  *
00514  * Notes:
00515  *
00516  *\li   Only the first 15 characters of 'name' will be copied.
00517  *
00518  *\li   'tag' is for debugging purposes only.
00519  *
00520  * Requires:
00521  *
00522  *\li   'ctx' is a valid ctx.
00523  */
00524 
00525 const char *
00526 isc_mem_getname(isc_mem_t *ctx);
00527 /*%<
00528  * Get the name of 'ctx', as previously set using isc_mem_setname().
00529  *
00530  * Requires:
00531  *\li   'ctx' is a valid ctx.
00532  *
00533  * Returns:
00534  *\li   A non-NULL pointer to a null-terminated string.
00535  *      If the ctx has not been named, the string is
00536  *      empty.
00537  */
00538 
00539 void *
00540 isc_mem_gettag(isc_mem_t *ctx);
00541 /*%<
00542  * Get the tag value for  'task', as previously set using isc_mem_setname().
00543  *
00544  * Requires:
00545  *\li   'ctx' is a valid ctx.
00546  *
00547  * Notes:
00548  *\li   This function is for debugging purposes only.
00549  *
00550  * Requires:
00551  *\li   'ctx' is a valid task.
00552  */
00553 
00554 #ifdef HAVE_LIBXML2
00555 int
00556 isc_mem_renderxml(xmlTextWriterPtr writer);
00557 /*%<
00558  * Render all contexts' statistics and status in XML for writer.
00559  */
00560 #endif /* HAVE_LIBXML2 */
00561 
00562 #ifdef HAVE_JSON
00563 isc_result_t
00564 isc_mem_renderjson(json_object *memobj);
00565 /*%<
00566  * Render all contexts' statistics and status in JSON.
00567  */
00568 #endif /* HAVE_JSON */
00569 
00570 
00571 /*
00572  * Memory pools
00573  */
00574 
00575 isc_result_t
00576 isc_mempool_create(isc_mem_t *mctx, size_t size, isc_mempool_t **mpctxp);
00577 /*%<
00578  * Create a memory pool.
00579  *
00580  * Requires:
00581  *\li   mctx is a valid memory context.
00582  *\li   size > 0
00583  *\li   mpctxp != NULL and *mpctxp == NULL
00584  *
00585  * Defaults:
00586  *\li   maxalloc = UINT_MAX
00587  *\li   freemax = 1
00588  *\li   fillcount = 1
00589  *
00590  * Returns:
00591  *\li   #ISC_R_NOMEMORY         -- not enough memory to create pool
00592  *\li   #ISC_R_SUCCESS          -- all is well.
00593  */
00594 
00595 void
00596 isc_mempool_destroy(isc_mempool_t **mpctxp);
00597 /*%<
00598  * Destroy a memory pool.
00599  *
00600  * Requires:
00601  *\li   mpctxp != NULL && *mpctxp is a valid pool.
00602  *\li   The pool has no un"put" allocations outstanding
00603  */
00604 
00605 void
00606 isc_mempool_setname(isc_mempool_t *mpctx, const char *name);
00607 /*%<
00608  * Associate a name with a memory pool.  At most 15 characters may be used.
00609  *
00610  * Requires:
00611  *\li   mpctx is a valid pool.
00612  *\li   name != NULL;
00613  */
00614 
00615 void
00616 isc_mempool_associatelock(isc_mempool_t *mpctx, isc_mutex_t *lock);
00617 /*%<
00618  * Associate a lock with this memory pool.
00619  *
00620  * This lock is used when getting or putting items using this memory pool,
00621  * and it is also used to set or get internal state via the isc_mempool_get*()
00622  * and isc_mempool_set*() set of functions.
00623  *
00624  * Multiple pools can each share a single lock.  For instance, if "manager"
00625  * type object contained pools for various sizes of events, and each of
00626  * these pools used a common lock.  Note that this lock must NEVER be used
00627  * by other than mempool routines once it is given to a pool, since that can
00628  * easily cause double locking.
00629  *
00630  * Requires:
00631  *
00632  *\li   mpctpx is a valid pool.
00633  *
00634  *\li   lock != NULL.
00635  *
00636  *\li   No previous lock is assigned to this pool.
00637  *
00638  *\li   The lock is initialized before calling this function via the normal
00639  *      means of doing that.
00640  */
00641 
00642 /*
00643  * The following functions get/set various parameters.  Note that due to
00644  * the unlocked nature of pools these are potentially random values unless
00645  * the imposed externally provided locking protocols are followed.
00646  *
00647  * Also note that the quota limits will not always take immediate effect.
00648  * For instance, setting "maxalloc" to a number smaller than the currently
00649  * allocated count is permitted.  New allocations will be refused until
00650  * the count drops below this threshold.
00651  *
00652  * All functions require (in addition to other requirements):
00653  *      mpctx is a valid memory pool
00654  */
00655 
00656 unsigned int
00657 isc_mempool_getfreemax(isc_mempool_t *mpctx);
00658 /*%<
00659  * Returns the maximum allowed size of the free list.
00660  */
00661 
00662 void
00663 isc_mempool_setfreemax(isc_mempool_t *mpctx, unsigned int limit);
00664 /*%<
00665  * Sets the maximum allowed size of the free list.
00666  */
00667 
00668 unsigned int
00669 isc_mempool_getfreecount(isc_mempool_t *mpctx);
00670 /*%<
00671  * Returns current size of the free list.
00672  */
00673 
00674 unsigned int
00675 isc_mempool_getmaxalloc(isc_mempool_t *mpctx);
00676 /*!<
00677  * Returns the maximum allowed number of allocations.
00678  */
00679 
00680 void
00681 isc_mempool_setmaxalloc(isc_mempool_t *mpctx, unsigned int limit);
00682 /*%<
00683  * Sets the maximum allowed number of allocations.
00684  *
00685  * Additional requirements:
00686  *\li   limit > 0
00687  */
00688 
00689 unsigned int
00690 isc_mempool_getallocated(isc_mempool_t *mpctx);
00691 /*%<
00692  * Returns the number of items allocated from this pool.
00693  */
00694 
00695 unsigned int
00696 isc_mempool_getfillcount(isc_mempool_t *mpctx);
00697 /*%<
00698  * Returns the number of items allocated as a block from the parent memory
00699  * context when the free list is empty.
00700  */
00701 
00702 void
00703 isc_mempool_setfillcount(isc_mempool_t *mpctx, unsigned int limit);
00704 /*%<
00705  * Sets the fillcount.
00706  *
00707  * Additional requirements:
00708  *\li   limit > 0
00709  */
00710 
00711 
00712 /*
00713  * Pseudo-private functions for use via macros.  Do not call directly.
00714  */
00715 void *
00716 ISCMEMFUNC(get)(isc_mem_t *, size_t _ISC_MEM_FLARG);
00717 void
00718 ISCMEMFUNC(putanddetach)(isc_mem_t **, void *, size_t _ISC_MEM_FLARG);
00719 void
00720 ISCMEMFUNC(put)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
00721 void *
00722 ISCMEMFUNC(allocate)(isc_mem_t *, size_t _ISC_MEM_FLARG);
00723 void *
00724 ISCMEMFUNC(reallocate)(isc_mem_t *, void *, size_t _ISC_MEM_FLARG);
00725 void
00726 ISCMEMFUNC(free)(isc_mem_t *, void * _ISC_MEM_FLARG);
00727 char *
00728 ISCMEMFUNC(strdup)(isc_mem_t *, const char *_ISC_MEM_FLARG);
00729 void *
00730 ISCMEMPOOLFUNC(get)(isc_mempool_t * _ISC_MEM_FLARG);
00731 void
00732 ISCMEMPOOLFUNC(put)(isc_mempool_t *, void * _ISC_MEM_FLARG);
00733 
00734 /*%<
00735  * See isc_mem_create2() above.
00736  */
00737 typedef isc_result_t
00738 (*isc_memcreatefunc_t)(size_t init_max_size, size_t target_size,
00739                        isc_mem_t **ctxp, unsigned int flags);
00740 
00741 isc_result_t
00742 isc_mem_register(isc_memcreatefunc_t createfunc);
00743 /*%<
00744  * Register a new memory management implementation and add it to the list of
00745  * supported implementations.  This function must be called when a different
00746  * memory management library is used than the one contained in the ISC library.
00747  */
00748 
00749 isc_result_t
00750 isc__mem_register(void);
00751 /*%<
00752  * A short cut function that specifies the memory management module in the ISC
00753  * library for isc_mem_register().  An application that uses the ISC library
00754  * usually do not have to care about this function: it would call
00755  * isc_lib_register(), which internally calls this function.
00756  */
00757 
00758 ISC_LANG_ENDDECLS
00759 
00760 #endif /* ISC_MEM_H */

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha