Apache Portable Runtime
apr_pools.h
Go to the documentation of this file.
00001 /* Licensed to the Apache Software Foundation (ASF) under one or more
00002  * contributor license agreements.  See the NOTICE file distributed with
00003  * this work for additional information regarding copyright ownership.
00004  * The ASF licenses this file to You under the Apache License, Version 2.0
00005  * (the "License"); you may not use this file except in compliance with
00006  * the License.  You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef APR_POOLS_H
00018 #define APR_POOLS_H
00019 
00020 /**
00021  * @file apr_pools.h
00022  * @brief APR memory allocation
00023  *
00024  * Resource allocation routines...
00025  *
00026  * designed so that we don't have to keep track of EVERYTHING so that
00027  * it can be explicitly freed later (a fundamentally unsound strategy ---
00028  * particularly in the presence of die()).
00029  *
00030  * Instead, we maintain pools, and allocate items (both memory and I/O
00031  * handlers) from the pools --- currently there are two, one for
00032  * per-transaction info, and one for config info.  When a transaction is
00033  * over, we can delete everything in the per-transaction apr_pool_t without
00034  * fear, and without thinking too hard about it either.
00035  *
00036  * Note that most operations on pools are not thread-safe: a single pool
00037  * should only be accessed by a single thread at any given time. The one
00038  * exception to this rule is creating a subpool of a given pool: one or more
00039  * threads can safely create subpools at the same time that another thread
00040  * accesses the parent pool.
00041  */
00042 
00043 #include "apr.h"
00044 #include "apr_errno.h"
00045 #include "apr_general.h" /* for APR_STRINGIFY */
00046 #define APR_WANT_MEMFUNC /**< for no good reason? */
00047 #include "apr_want.h"
00048 
00049 #ifdef __cplusplus
00050 extern "C" {
00051 #endif
00052 
00053 /**
00054  * @defgroup apr_pools Memory Pool Functions
00055  * @ingroup APR 
00056  * @{
00057  */
00058 
00059 /** The fundamental pool type */
00060 typedef struct apr_pool_t apr_pool_t;
00061 
00062 
00063 /**
00064  * Declaration helper macro to construct apr_foo_pool_get()s.
00065  *
00066  * This standardized macro is used by opaque (APR) data types to return
00067  * the apr_pool_t that is associated with the data type.
00068  *
00069  * APR_POOL_DECLARE_ACCESSOR() is used in a header file to declare the
00070  * accessor function. A typical usage and result would be:
00071  * <pre>
00072  *    APR_POOL_DECLARE_ACCESSOR(file);
00073  * becomes:
00074  *    APR_DECLARE(apr_pool_t *) apr_file_pool_get(apr_file_t *ob);
00075  * </pre>
00076  * @remark Doxygen unwraps this macro (via doxygen.conf) to provide 
00077  * actual help for each specific occurance of apr_foo_pool_get.
00078  * @remark the linkage is specified for APR. It would be possible to expand
00079  *       the macros to support other linkages.
00080  */
00081 #define APR_POOL_DECLARE_ACCESSOR(type) \
00082     APR_DECLARE(apr_pool_t *) apr_##type##_pool_get \
00083         (const apr_##type##_t *the##type)
00084 
00085 /** 
00086  * Implementation helper macro to provide apr_foo_pool_get()s.
00087  *
00088  * In the implementation, the APR_POOL_IMPLEMENT_ACCESSOR() is used to
00089  * actually define the function. It assumes the field is named "pool".
00090  */
00091 #define APR_POOL_IMPLEMENT_ACCESSOR(type) \
00092     APR_DECLARE(apr_pool_t *) apr_##type##_pool_get \
00093             (const apr_##type##_t *the##type) \
00094         { return the##type->pool; }
00095 
00096 
00097 /**
00098  * Pool debug levels
00099  *
00100  * <pre>
00101  * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
00102  * ---------------------------------
00103  * |   |   |   |   |   |   |   | x |  General debug code enabled (useful in
00104  *                                    combination with --with-efence).
00105  *
00106  * |   |   |   |   |   |   | x |   |  Verbose output on stderr (report
00107  *                                    CREATE, CLEAR, DESTROY).
00108  *
00109  * |   |   |   | x |   |   |   |   |  Verbose output on stderr (report
00110  *                                    PALLOC, PCALLOC).
00111  *
00112  * |   |   |   |   |   | x |   |   |  Lifetime checking. On each use of a
00113  *                                    pool, check its lifetime.  If the pool
00114  *                                    is out of scope, abort().
00115  *                                    In combination with the verbose flag
00116  *                                    above, it will output LIFE in such an
00117  *                                    event prior to aborting.
00118  *
00119  * |   |   |   |   | x |   |   |   |  Pool owner checking.  On each use of a
00120  *                                    pool, check if the current thread is the
00121  *                                    pools owner.  If not, abort().  In
00122  *                                    combination with the verbose flag above,
00123  *                                    it will output OWNER in such an event
00124  *                                    prior to aborting.  Use the debug
00125  *                                    function apr_pool_owner_set() to switch
00126  *                                    a pools ownership.
00127  *
00128  * When no debug level was specified, assume general debug mode.
00129  * If level 0 was specified, debugging is switched off
00130  * </pre>
00131  */
00132 #if defined(APR_POOL_DEBUG)
00133 /* If APR_POOL_DEBUG is blank, we get 1; if it is a number, we get -1. */
00134 #if (APR_POOL_DEBUG - APR_POOL_DEBUG -1 == 1)
00135 #undef APR_POOL_DEBUG
00136 #define APR_POOL_DEBUG 1
00137 #endif
00138 #else
00139 #define APR_POOL_DEBUG 0
00140 #endif
00141 
00142 /** the place in the code where the particular function was called */
00143 #define APR_POOL__FILE_LINE__ __FILE__ ":" APR_STRINGIFY(__LINE__)
00144 
00145 
00146 
00147 /** A function that is called when allocation fails. */
00148 typedef int (*apr_abortfunc_t)(int retcode);
00149 
00150 /*
00151  * APR memory structure manipulators (pools, tables, and arrays).
00152  */
00153 
00154 /*
00155  * Initialization
00156  */
00157 
00158 /**
00159  * Setup all of the internal structures required to use pools
00160  * @remark Programs do NOT need to call this directly.  APR will call this
00161  *      automatically from apr_initialize.
00162  * @internal
00163  */
00164 APR_DECLARE(apr_status_t) apr_pool_initialize(void);
00165 
00166 /**
00167  * Tear down all of the internal structures required to use pools
00168  * @remark Programs do NOT need to call this directly.  APR will call this
00169  *      automatically from apr_terminate.
00170  * @internal
00171  */
00172 APR_DECLARE(void) apr_pool_terminate(void);
00173 
00174 
00175 /*
00176  * Pool creation/destruction
00177  */
00178 
00179 #include "apr_allocator.h"
00180 
00181 /**
00182  * Create a new pool.
00183  * @param newpool The pool we have just created.
00184  * @param parent The parent pool.  If this is NULL, the new pool is a root
00185  *        pool.  If it is non-NULL, the new pool will inherit all
00186  *        of its parent pool's attributes, except the apr_pool_t will
00187  *        be a sub-pool.
00188  * @param abort_fn A function to use if the pool cannot allocate more memory.
00189  * @param allocator The allocator to use with the new pool.  If NULL the
00190  *        allocator of the parent pool will be used.
00191  * @remark This function is thread-safe, in the sense that multiple threads
00192  *         can safely create subpools of the same parent pool concurrently.
00193  *         Similarly, a subpool can be created by one thread at the same
00194  *         time that another thread accesses the parent pool.
00195  */
00196 APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
00197                                              apr_pool_t *parent,
00198                                              apr_abortfunc_t abort_fn,
00199                                              apr_allocator_t *allocator);
00200 
00201 /**
00202  * Create a new pool.
00203  * @deprecated @see apr_pool_create_unmanaged_ex.
00204  */
00205 APR_DECLARE(apr_status_t) apr_pool_create_core_ex(apr_pool_t **newpool,
00206                                                   apr_abortfunc_t abort_fn,
00207                                                   apr_allocator_t *allocator);
00208 
00209 /**
00210  * Create a new unmanaged pool.
00211  * @param newpool The pool we have just created.
00212  * @param abort_fn A function to use if the pool cannot allocate more memory.
00213  * @param allocator The allocator to use with the new pool.  If NULL a
00214  *        new allocator will be crated with newpool as owner.
00215  * @remark An unmanaged pool is a special pool without a parent; it will
00216  *         NOT be destroyed upon apr_terminate.  It must be explicitly
00217  *         destroyed by calling apr_pool_destroy, to prevent memory leaks.
00218  *         Use of this function is discouraged, think twice about whether
00219  *         you really really need it.
00220  */
00221 APR_DECLARE(apr_status_t) apr_pool_create_unmanaged_ex(apr_pool_t **newpool,
00222                                                    apr_abortfunc_t abort_fn,
00223                                                    apr_allocator_t *allocator);
00224 
00225 /**
00226  * Debug version of apr_pool_create_ex.
00227  * @param newpool @see apr_pool_create.
00228  * @param parent @see apr_pool_create.
00229  * @param abort_fn @see apr_pool_create.
00230  * @param allocator @see apr_pool_create.
00231  * @param file_line Where the function is called from.
00232  *        This is usually APR_POOL__FILE_LINE__.
00233  * @remark Only available when APR_POOL_DEBUG is defined.
00234  *         Call this directly if you have you apr_pool_create_ex
00235  *         calls in a wrapper function and wish to override
00236  *         the file_line argument to reflect the caller of
00237  *         your wrapper function.  If you do not have
00238  *         apr_pool_create_ex in a wrapper, trust the macro
00239  *         and don't call apr_pool_create_ex_debug directly.
00240  */
00241 APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
00242                                                    apr_pool_t *parent,
00243                                                    apr_abortfunc_t abort_fn,
00244                                                    apr_allocator_t *allocator,
00245                                                    const char *file_line);
00246 
00247 #if APR_POOL_DEBUG
00248 #define apr_pool_create_ex(newpool, parent, abort_fn, allocator)  \
00249     apr_pool_create_ex_debug(newpool, parent, abort_fn, allocator, \
00250                              APR_POOL__FILE_LINE__)
00251 #endif
00252 
00253 /**
00254  * Debug version of apr_pool_create_core_ex.
00255  * @deprecated @see apr_pool_create_unmanaged_ex_debug.
00256  */
00257 APR_DECLARE(apr_status_t) apr_pool_create_core_ex_debug(apr_pool_t **newpool,
00258                                                    apr_abortfunc_t abort_fn,
00259                                                    apr_allocator_t *allocator,
00260                                                    const char *file_line);
00261 
00262 /**
00263  * Debug version of apr_pool_create_unmanaged_ex.
00264  * @param newpool @see apr_pool_create_unmanaged.
00265  * @param abort_fn @see apr_pool_create_unmanaged.
00266  * @param allocator @see apr_pool_create_unmanaged.
00267  * @param file_line Where the function is called from.
00268  *        This is usually APR_POOL__FILE_LINE__.
00269  * @remark Only available when APR_POOL_DEBUG is defined.
00270  *         Call this directly if you have you apr_pool_create_unmanaged_ex
00271  *         calls in a wrapper function and wish to override
00272  *         the file_line argument to reflect the caller of
00273  *         your wrapper function.  If you do not have
00274  *         apr_pool_create_core_ex in a wrapper, trust the macro
00275  *         and don't call apr_pool_create_core_ex_debug directly.
00276  */
00277 APR_DECLARE(apr_status_t) apr_pool_create_unmanaged_ex_debug(apr_pool_t **newpool,
00278                                                    apr_abortfunc_t abort_fn,
00279                                                    apr_allocator_t *allocator,
00280                                                    const char *file_line);
00281 
00282 #if APR_POOL_DEBUG
00283 #define apr_pool_create_core_ex(newpool, abort_fn, allocator)  \
00284     apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
00285                                   APR_POOL__FILE_LINE__)
00286 
00287 #define apr_pool_create_unmanaged_ex(newpool, abort_fn, allocator)  \
00288     apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
00289                                   APR_POOL__FILE_LINE__)
00290 
00291 #endif
00292 
00293 /**
00294  * Create a new pool.
00295  * @param newpool The pool we have just created.
00296  * @param parent The parent pool.  If this is NULL, the new pool is a root
00297  *        pool.  If it is non-NULL, the new pool will inherit all
00298  *        of its parent pool's attributes, except the apr_pool_t will
00299  *        be a sub-pool.
00300  * @remark This function is thread-safe, in the sense that multiple threads
00301  *         can safely create subpools of the same parent pool concurrently.
00302  *         Similarly, a subpool can be created by one thread at the same
00303  *         time that another thread accesses the parent pool.
00304  */
00305 #if defined(DOXYGEN)
00306 APR_DECLARE(apr_status_t) apr_pool_create(apr_pool_t **newpool,
00307                                           apr_pool_t *parent);
00308 #else
00309 #if APR_POOL_DEBUG
00310 #define apr_pool_create(newpool, parent) \
00311     apr_pool_create_ex_debug(newpool, parent, NULL, NULL, \
00312                              APR_POOL__FILE_LINE__)
00313 #else
00314 #define apr_pool_create(newpool, parent) \
00315     apr_pool_create_ex(newpool, parent, NULL, NULL)
00316 #endif
00317 #endif
00318 
00319 /**
00320  * Create a new pool.
00321  * @param newpool The pool we have just created.
00322  */
00323 #if defined(DOXYGEN)
00324 APR_DECLARE(apr_status_t) apr_pool_create_core(apr_pool_t **newpool);
00325 APR_DECLARE(apr_status_t) apr_pool_create_unmanaged(apr_pool_t **newpool);
00326 #else
00327 #if APR_POOL_DEBUG
00328 #define apr_pool_create_core(newpool) \
00329     apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
00330                                   APR_POOL__FILE_LINE__)
00331 #define apr_pool_create_unmanaged(newpool) \
00332     apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
00333                                   APR_POOL__FILE_LINE__)
00334 #else
00335 #define apr_pool_create_core(newpool) \
00336     apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
00337 #define apr_pool_create_unmanaged(newpool) \
00338     apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
00339 #endif
00340 #endif
00341 
00342 /**
00343  * Find the pool's allocator
00344  * @param pool The pool to get the allocator from.
00345  */
00346 APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool);
00347 
00348 /**
00349  * Clear all memory in the pool and run all the cleanups. This also destroys all
00350  * subpools.
00351  * @param p The pool to clear
00352  * @remark This does not actually free the memory, it just allows the pool
00353  *         to re-use this memory for the next allocation.
00354  * @see apr_pool_destroy()
00355  */
00356 APR_DECLARE(void) apr_pool_clear(apr_pool_t *p);
00357 
00358 /**
00359  * Debug version of apr_pool_clear.
00360  * @param p See: apr_pool_clear.
00361  * @param file_line Where the function is called from.
00362  *        This is usually APR_POOL__FILE_LINE__.
00363  * @remark Only available when APR_POOL_DEBUG is defined.
00364  *         Call this directly if you have you apr_pool_clear
00365  *         calls in a wrapper function and wish to override
00366  *         the file_line argument to reflect the caller of
00367  *         your wrapper function.  If you do not have
00368  *         apr_pool_clear in a wrapper, trust the macro
00369  *         and don't call apr_pool_destroy_clear directly.
00370  */
00371 APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *p,
00372                                        const char *file_line);
00373 
00374 #if APR_POOL_DEBUG
00375 #define apr_pool_clear(p) \
00376     apr_pool_clear_debug(p, APR_POOL__FILE_LINE__)
00377 #endif
00378 
00379 /**
00380  * Destroy the pool. This takes similar action as apr_pool_clear() and then
00381  * frees all the memory.
00382  * @param p The pool to destroy
00383  * @remark This will actually free the memory
00384  */
00385 APR_DECLARE(void) apr_pool_destroy(apr_pool_t *p);
00386 
00387 /**
00388  * Debug version of apr_pool_destroy.
00389  * @param p See: apr_pool_destroy.
00390  * @param file_line Where the function is called from.
00391  *        This is usually APR_POOL__FILE_LINE__.
00392  * @remark Only available when APR_POOL_DEBUG is defined.
00393  *         Call this directly if you have you apr_pool_destroy
00394  *         calls in a wrapper function and wish to override
00395  *         the file_line argument to reflect the caller of
00396  *         your wrapper function.  If you do not have
00397  *         apr_pool_destroy in a wrapper, trust the macro
00398  *         and don't call apr_pool_destroy_debug directly.
00399  */
00400 APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *p,
00401                                          const char *file_line);
00402 
00403 #if APR_POOL_DEBUG
00404 #define apr_pool_destroy(p) \
00405     apr_pool_destroy_debug(p, APR_POOL__FILE_LINE__)
00406 #endif
00407 
00408 
00409 /*
00410  * Memory allocation
00411  */
00412 
00413 /**
00414  * Allocate a block of memory from a pool
00415  * @param p The pool to allocate from
00416  * @param size The amount of memory to allocate
00417  * @return The allocated memory
00418  */
00419 APR_DECLARE(void *) apr_palloc(apr_pool_t *p, apr_size_t size);
00420 
00421 /**
00422  * Debug version of apr_palloc
00423  * @param p See: apr_palloc
00424  * @param size See: apr_palloc
00425  * @param file_line Where the function is called from.
00426  *        This is usually APR_POOL__FILE_LINE__.
00427  * @return See: apr_palloc
00428  */
00429 APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *p, apr_size_t size,
00430                                      const char *file_line);
00431 
00432 #if APR_POOL_DEBUG
00433 #define apr_palloc(p, size) \
00434     apr_palloc_debug(p, size, APR_POOL__FILE_LINE__)
00435 #endif
00436 
00437 /**
00438  * Allocate a block of memory from a pool and set all of the memory to 0
00439  * @param p The pool to allocate from
00440  * @param size The amount of memory to allocate
00441  * @return The allocated memory
00442  */
00443 #if defined(DOXYGEN)
00444 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *p, apr_size_t size);
00445 #elif !APR_POOL_DEBUG
00446 #define apr_pcalloc(p, size) memset(apr_palloc(p, size), 0, size)
00447 #endif
00448 
00449 /**
00450  * Debug version of apr_pcalloc
00451  * @param p See: apr_pcalloc
00452  * @param size See: apr_pcalloc
00453  * @param file_line Where the function is called from.
00454  *        This is usually APR_POOL__FILE_LINE__.
00455  * @return See: apr_pcalloc
00456  */
00457 APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *p, apr_size_t size,
00458                                       const char *file_line);
00459 
00460 #if APR_POOL_DEBUG
00461 #define apr_pcalloc(p, size) \
00462     apr_pcalloc_debug(p, size, APR_POOL__FILE_LINE__)
00463 #endif
00464 
00465 
00466 /*
00467  * Pool Properties
00468  */
00469 
00470 /**
00471  * Set the function to be called when an allocation failure occurs.
00472  * @remark If the program wants APR to exit on a memory allocation error,
00473  *      then this function can be called to set the callback to use (for
00474  *      performing cleanup and then exiting). If this function is not called,
00475  *      then APR will return an error and expect the calling program to
00476  *      deal with the error accordingly.
00477  */
00478 APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abortfunc,
00479                                      apr_pool_t *pool);
00480 
00481 /**
00482  * Get the abort function associated with the specified pool.
00483  * @param pool The pool for retrieving the abort function.
00484  * @return The abort function for the given pool.
00485  */
00486 APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool);
00487 
00488 /**
00489  * Get the parent pool of the specified pool.
00490  * @param pool The pool for retrieving the parent pool.
00491  * @return The parent of the given pool.
00492  */
00493 APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool);
00494 
00495 /**
00496  * Determine if pool a is an ancestor of pool b.
00497  * @param a The pool to search
00498  * @param b The pool to search for
00499  * @return True if a is an ancestor of b, NULL is considered an ancestor
00500  *         of all pools.
00501  * @remark if compiled with APR_POOL_DEBUG, this function will also
00502  * return true if A is a pool which has been guaranteed by the caller
00503  * (using apr_pool_join) to have a lifetime at least as long as some
00504  * ancestor of pool B.
00505  */
00506 APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b);
00507 
00508 /**
00509  * Tag a pool (give it a name)
00510  * @param pool The pool to tag
00511  * @param tag  The tag
00512  */
00513 APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag);
00514 
00515 
00516 /*
00517  * User data management
00518  */
00519 
00520 /**
00521  * Set the data associated with the current pool
00522  * @param data The user data associated with the pool.
00523  * @param key The key to use for association
00524  * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
00525  * @param pool The current pool
00526  * @warning The data to be attached to the pool should have a life span
00527  *          at least as long as the pool it is being attached to.
00528  *
00529  *      Users of APR must take EXTREME care when choosing a key to
00530  *      use for their data.  It is possible to accidentally overwrite
00531  *      data by choosing a key that another part of the program is using.
00532  *      Therefore it is advised that steps are taken to ensure that unique
00533  *      keys are used for all of the userdata objects in a particular pool
00534  *      (the same key in two different pools or a pool and one of its
00535  *      subpools is okay) at all times.  Careful namespace prefixing of
00536  *      key names is a typical way to help ensure this uniqueness.
00537  *
00538  */
00539 APR_DECLARE(apr_status_t) apr_pool_userdata_set(
00540     const void *data,
00541     const char *key,
00542     apr_status_t (*cleanup)(void *),
00543     apr_pool_t *pool);
00544 
00545 /**
00546  * Set the data associated with the current pool
00547  * @param data The user data associated with the pool.
00548  * @param key The key to use for association
00549  * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
00550  * @param pool The current pool
00551  * @note same as apr_pool_userdata_set(), except that this version doesn't
00552  *       make a copy of the key (this function is useful, for example, when
00553  *       the key is a string literal)
00554  * @warning This should NOT be used if the key could change addresses by
00555  *       any means between the apr_pool_userdata_setn() call and a
00556  *       subsequent apr_pool_userdata_get() on that key, such as if a
00557  *       static string is used as a userdata key in a DSO and the DSO could
00558  *       be unloaded and reloaded between the _setn() and the _get().  You
00559  *       MUST use apr_pool_userdata_set() in such cases.
00560  * @warning More generally, the key and the data to be attached to the
00561  *       pool should have a life span at least as long as the pool itself.
00562  *
00563  */
00564 APR_DECLARE(apr_status_t) apr_pool_userdata_setn(
00565     const void *data,
00566     const char *key,
00567     apr_status_t (*cleanup)(void *),
00568     apr_pool_t *pool);
00569 
00570 /**
00571  * Return the data associated with the current pool.
00572  * @param data The user data associated with the pool.
00573  * @param key The key for the data to retrieve
00574  * @param pool The current pool.
00575  */
00576 APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
00577                                                 apr_pool_t *pool);
00578 
00579 
00580 /**
00581  * @defgroup PoolCleanup  Pool Cleanup Functions
00582  *
00583  * Cleanups are performed in the reverse order they were registered.  That is:
00584  * Last In, First Out.  A cleanup function can safely allocate memory from
00585  * the pool that is being cleaned up. It can also safely register additional
00586  * cleanups which will be run LIFO, directly after the current cleanup
00587  * terminates.  Cleanups have to take caution in calling functions that
00588  * create subpools. Subpools, created during cleanup will NOT automatically
00589  * be cleaned up.  In other words, cleanups are to clean up after themselves.
00590  *
00591  * @{
00592  */
00593 
00594 /**
00595  * Register a function to be called when a pool is cleared or destroyed
00596  * @param p The pool register the cleanup with
00597  * @param data The data to pass to the cleanup function.
00598  * @param plain_cleanup The function to call when the pool is cleared
00599  *                      or destroyed
00600  * @param child_cleanup The function to call when a child process is about
00601  *                      to exec - this function is called in the child, obviously!
00602  */
00603 APR_DECLARE(void) apr_pool_cleanup_register(
00604     apr_pool_t *p,
00605     const void *data,
00606     apr_status_t (*plain_cleanup)(void *),
00607     apr_status_t (*child_cleanup)(void *));
00608 
00609 /**
00610  * Register a function to be called when a pool is cleared or destroyed.
00611  *
00612  * Unlike apr_pool_cleanup_register which register a cleanup
00613  * that is called AFTER all subpools are destroyed this function register
00614  * a function that will be called before any of the subpool is destoryed.
00615  *
00616  * @param p The pool register the cleanup with
00617  * @param data The data to pass to the cleanup function.
00618  * @param plain_cleanup The function to call when the pool is cleared
00619  *                      or destroyed
00620  */
00621 APR_DECLARE(void) apr_pool_pre_cleanup_register(
00622     apr_pool_t *p,
00623     const void *data,
00624     apr_status_t (*plain_cleanup)(void *));
00625 
00626 /**
00627  * Remove a previously registered cleanup function.
00628  * 
00629  * The cleanup most recently registered with @a p having the same values of
00630  * @a data and @a cleanup will be removed.
00631  *
00632  * @param p The pool to remove the cleanup from
00633  * @param data The data of the registered cleanup
00634  * @param cleanup The function to remove from cleanup
00635  * @remarks For some strange reason only the plain_cleanup is handled by this
00636  *          function
00637  */
00638 APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
00639                                         apr_status_t (*cleanup)(void *));
00640 
00641 /**
00642  * Replace the child cleanup function of a previously registered cleanup.
00643  * 
00644  * The cleanup most recently registered with @a p having the same values of
00645  * @a data and @a plain_cleanup will have the registered child cleanup
00646  * function replaced with @a child_cleanup.
00647  *
00648  * @param p The pool of the registered cleanup
00649  * @param data The data of the registered cleanup
00650  * @param plain_cleanup The plain cleanup function of the registered cleanup
00651  * @param child_cleanup The function to register as the child cleanup
00652  */
00653 APR_DECLARE(void) apr_pool_child_cleanup_set(
00654     apr_pool_t *p,
00655     const void *data,
00656     apr_status_t (*plain_cleanup)(void *),
00657     apr_status_t (*child_cleanup)(void *));
00658 
00659 /**
00660  * Run the specified cleanup function immediately and unregister it.
00661  *
00662  * The cleanup most recently registered with @a p having the same values of
00663  * @a data and @a cleanup will be removed and @a cleanup will be called
00664  * with @a data as the argument.
00665  *
00666  * @param p The pool to remove the cleanup from
00667  * @param data The data to remove from cleanup
00668  * @param cleanup The function to remove from cleanup
00669  */
00670 APR_DECLARE(apr_status_t) apr_pool_cleanup_run(
00671     apr_pool_t *p,
00672     void *data,
00673     apr_status_t (*cleanup)(void *));
00674 
00675 /**
00676  * An empty cleanup function.
00677  * 
00678  * Passed to apr_pool_cleanup_register() when no cleanup is required.
00679  *
00680  * @param data The data to cleanup, will not be used by this function.
00681  */
00682 APR_DECLARE_NONSTD(apr_status_t) apr_pool_cleanup_null(void *data);
00683 
00684 /**
00685  * Run all registered child cleanups, in preparation for an exec()
00686  * call in a forked child -- close files, etc., but *don't* flush I/O
00687  * buffers, *don't* wait for subprocesses, and *don't* free any
00688  * memory.
00689  */
00690 APR_DECLARE(void) apr_pool_cleanup_for_exec(void);
00691 
00692 /** @} */
00693 
00694 /**
00695  * @defgroup PoolDebug Pool Debugging functions.
00696  *
00697  * pools have nested lifetimes -- sub_pools are destroyed when the
00698  * parent pool is cleared.  We allow certain liberties with operations
00699  * on things such as tables (and on other structures in a more general
00700  * sense) where we allow the caller to insert values into a table which
00701  * were not allocated from the table's pool.  The table's data will
00702  * remain valid as long as all the pools from which its values are
00703  * allocated remain valid.
00704  *
00705  * For example, if B is a sub pool of A, and you build a table T in
00706  * pool B, then it's safe to insert data allocated in A or B into T
00707  * (because B lives at most as long as A does, and T is destroyed when
00708  * B is cleared/destroyed).  On the other hand, if S is a table in
00709  * pool A, it is safe to insert data allocated in A into S, but it
00710  * is *not safe* to insert data allocated from B into S... because
00711  * B can be cleared/destroyed before A is (which would leave dangling
00712  * pointers in T's data structures).
00713  *
00714  * In general we say that it is safe to insert data into a table T
00715  * if the data is allocated in any ancestor of T's pool.  This is the
00716  * basis on which the APR_POOL_DEBUG code works -- it tests these ancestor
00717  * relationships for all data inserted into tables.  APR_POOL_DEBUG also
00718  * provides tools (apr_pool_find, and apr_pool_is_ancestor) for other
00719  * folks to implement similar restrictions for their own data
00720  * structures.
00721  *
00722  * However, sometimes this ancestor requirement is inconvenient --
00723  * sometimes it's necessary to create a sub pool where the sub pool is
00724  * guaranteed to have the same lifetime as the parent pool.  This is a
00725  * guarantee implemented by the *caller*, not by the pool code.  That
00726  * is, the caller guarantees they won't destroy the sub pool
00727  * individually prior to destroying the parent pool.
00728  *
00729  * In this case the caller must call apr_pool_join() to indicate this
00730  * guarantee to the APR_POOL_DEBUG code.
00731  *
00732  * These functions are only implemented when #APR_POOL_DEBUG is set.
00733  *
00734  * @{
00735  */
00736 #if APR_POOL_DEBUG || defined(DOXYGEN)
00737 /**
00738  * Guarantee that a subpool has the same lifetime as the parent.
00739  * @param p The parent pool
00740  * @param sub The subpool
00741  */
00742 APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub);
00743 
00744 /**
00745  * Find a pool from something allocated in it.
00746  * @param mem The thing allocated in the pool
00747  * @return The pool it is allocated in
00748  */
00749 APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem);
00750 
00751 /**
00752  * Report the number of bytes currently in the pool
00753  * @param p The pool to inspect
00754  * @param recurse Recurse/include the subpools' sizes
00755  * @return The number of bytes
00756  */
00757 APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *p, int recurse);
00758 
00759 /**
00760  * Lock a pool
00761  * @param pool The pool to lock
00762  * @param flag  The flag
00763  */
00764 APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag);
00765 
00766 /* @} */
00767 
00768 #else /* APR_POOL_DEBUG or DOXYGEN */
00769 
00770 #ifdef apr_pool_join
00771 #undef apr_pool_join
00772 #endif
00773 #define apr_pool_join(a,b)
00774 
00775 #ifdef apr_pool_lock
00776 #undef apr_pool_lock
00777 #endif
00778 #define apr_pool_lock(pool, lock)
00779 
00780 #endif /* APR_POOL_DEBUG or DOXYGEN */
00781 
00782 /** @} */
00783 
00784 #ifdef __cplusplus
00785 }
00786 #endif
00787 
00788 #endif /* !APR_POOLS_H */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines