00001 /* 00002 * Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC") 00003 * 00004 * Permission to use, copy, modify, and/or distribute this software for any 00005 * purpose with or without fee is hereby granted, provided that the above 00006 * copyright notice and this permission notice appear in all copies. 00007 * 00008 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 00009 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 00010 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 00011 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 00012 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 00013 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00014 * PERFORMANCE OF THIS SOFTWARE. 00015 */ 00016 00017 #ifndef ISC_OBJPOOL_H 00018 #define ISC_OBJPOOL_H 1 00019 00020 /***** 00021 ***** Module Info 00022 *****/ 00023 00024 /*! \file isc/pool.h 00025 * \brief An object pool is a mechanism for sharing a small pool of 00026 * fungible objects among a large number of objects that depend on them. 00027 * 00028 * This is useful, for example, when it causes performance problems for 00029 * large number of zones to share a single memory context or task object, 00030 * but it would create a different set of problems for them each to have an 00031 * independent task or memory context. 00032 */ 00033 00034 00035 /*** 00036 *** Imports. 00037 ***/ 00038 00039 #include <isc/lang.h> 00040 #include <isc/mem.h> 00041 #include <isc/types.h> 00042 00043 ISC_LANG_BEGINDECLS 00044 00045 /***** 00046 ***** Types. 00047 *****/ 00048 00049 typedef void 00050 (*isc_pooldeallocator_t)(void **object); 00051 00052 typedef isc_result_t 00053 (*isc_poolinitializer_t)(void **target, void *arg); 00054 00055 typedef struct isc_pool isc_pool_t; 00056 00057 /***** 00058 ***** Functions. 00059 *****/ 00060 00061 isc_result_t 00062 isc_pool_create(isc_mem_t *mctx, unsigned int count, 00063 isc_pooldeallocator_t free, 00064 isc_poolinitializer_t init, void *initarg, 00065 isc_pool_t **poolp); 00066 /*%< 00067 * Create a pool of "count" object pointers. If 'free' is not NULL, 00068 * it points to a function that will detach the objects. 'init' 00069 * points to a function that will initialize the arguments, and 00070 * 'arg' to an argument to be passed into that function (for example, 00071 * a relevant manager or context object). 00072 * 00073 * Requires: 00074 * 00075 *\li 'mctx' is a valid memory context. 00076 * 00077 *\li init != NULL 00078 * 00079 *\li poolp != NULL && *poolp == NULL 00080 * 00081 * Ensures: 00082 * 00083 *\li On success, '*poolp' points to the new object pool. 00084 * 00085 * Returns: 00086 * 00087 *\li #ISC_R_SUCCESS 00088 *\li #ISC_R_NOMEMORY 00089 *\li #ISC_R_UNEXPECTED 00090 */ 00091 00092 void * 00093 isc_pool_get(isc_pool_t *pool); 00094 /*%< 00095 * Returns a pointer to an object from the pool. Currently the object 00096 * is chosen from the pool at random. (This may be changed in the future 00097 * to something that guaratees balance.) 00098 */ 00099 00100 int 00101 isc_pool_count(isc_pool_t *pool); 00102 /*%< 00103 * Returns the number of objcts in the pool 'pool'. 00104 */ 00105 00106 isc_result_t 00107 isc_pool_expand(isc_pool_t **sourcep, unsigned int count, isc_pool_t **targetp); 00108 00109 /*%< 00110 * If 'size' is larger than the number of objects in the pool pointed to by 00111 * 'sourcep', then a new pool of size 'count' is allocated, the existing 00112 * objects are copied into it, additional ones created to bring the 00113 * total number up to 'count', and the resulting pool is attached to 00114 * 'targetp'. 00115 * 00116 * If 'count' is less than or equal to the number of objects in 'source', then 00117 * 'sourcep' is attached to 'targetp' without any other action being taken. 00118 * 00119 * In either case, 'sourcep' is detached. 00120 * 00121 * Requires: 00122 * 00123 * \li 'sourcep' is not NULL and '*source' is not NULL 00124 * \li 'targetp' is not NULL and '*source' is NULL 00125 * 00126 * Ensures: 00127 * 00128 * \li On success, '*targetp' points to a valid task pool. 00129 * \li On success, '*sourcep' points to NULL. 00130 * 00131 * Returns: 00132 * 00133 * \li #ISC_R_SUCCESS 00134 * \li #ISC_R_NOMEMORY 00135 */ 00136 00137 void 00138 isc_pool_destroy(isc_pool_t **poolp); 00139 /*%< 00140 * Destroy a task pool. The tasks in the pool are detached but not 00141 * shut down. 00142 * 00143 * Requires: 00144 * \li '*poolp' is a valid task pool. 00145 */ 00146 00147 ISC_LANG_ENDDECLS 00148 00149 #endif /* ISC_OBJPOOL_H */