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                           __attribute__((nonnull(1)));
00201 
00202 /**
00203  * Create a new unmanaged pool.
00204  * @param newpool The pool we have just created.
00205  * @param abort_fn A function to use if the pool cannot allocate more memory.
00206  * @param allocator The allocator to use with the new pool.  If NULL a
00207  *        new allocator will be crated with newpool as owner.
00208  * @remark An unmanaged pool is a special pool without a parent; it will
00209  *         NOT be destroyed upon apr_terminate.  It must be explicitly
00210  *         destroyed by calling apr_pool_destroy, to prevent memory leaks.
00211  *         Use of this function is discouraged, think twice about whether
00212  *         you really really need it.
00213  * @warning Any child cleanups registered against the new pool, or
00214  *         against sub-pools thereof, will not be executed during an
00215  *         invocation of apr_proc_create(), so resources created in an
00216  *         "unmanaged" pool heirarchy will leak to child processes.
00217  */
00218 APR_DECLARE(apr_status_t) apr_pool_create_unmanaged_ex(apr_pool_t **newpool,
00219                                                    apr_abortfunc_t abort_fn,
00220                                                    apr_allocator_t *allocator)
00221                           __attribute__((nonnull(1)));
00222 
00223 /**
00224  * Debug version of apr_pool_create_ex.
00225  * @param newpool @see apr_pool_create.
00226  * @param parent @see apr_pool_create.
00227  * @param abort_fn @see apr_pool_create.
00228  * @param allocator @see apr_pool_create.
00229  * @param file_line Where the function is called from.
00230  *        This is usually APR_POOL__FILE_LINE__.
00231  * @remark Only available when APR_POOL_DEBUG is defined.
00232  *         Call this directly if you have you apr_pool_create_ex
00233  *         calls in a wrapper function and wish to override
00234  *         the file_line argument to reflect the caller of
00235  *         your wrapper function.  If you do not have
00236  *         apr_pool_create_ex in a wrapper, trust the macro
00237  *         and don't call apr_pool_create_ex_debug directly.
00238  */
00239 APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
00240                                                    apr_pool_t *parent,
00241                                                    apr_abortfunc_t abort_fn,
00242                                                    apr_allocator_t *allocator,
00243                                                    const char *file_line)
00244                           __attribute__((nonnull(1)));
00245 
00246 #if APR_POOL_DEBUG
00247 #define apr_pool_create_ex(newpool, parent, abort_fn, allocator)  \
00248     apr_pool_create_ex_debug(newpool, parent, abort_fn, allocator, \
00249                              APR_POOL__FILE_LINE__)
00250 #endif
00251 
00252   /**
00253  * Debug version of apr_pool_create_unmanaged_ex.
00254  * @param newpool @see apr_pool_create_unmanaged.
00255  * @param abort_fn @see apr_pool_create_unmanaged.
00256  * @param allocator @see apr_pool_create_unmanaged.
00257  * @param file_line Where the function is called from.
00258  *        This is usually APR_POOL__FILE_LINE__.
00259  * @remark Only available when APR_POOL_DEBUG is defined.
00260  *         Call this directly if you have you apr_pool_create_unmanaged_ex
00261  *         calls in a wrapper function and wish to override
00262  *         the file_line argument to reflect the caller of
00263  *         your wrapper function.  If you do not have
00264  *         apr_pool_create_unmanaged_ex in a wrapper, trust the macro
00265  *         and don't call apr_pool_create_unmanaged_ex_debug directly.
00266  */
00267 APR_DECLARE(apr_status_t) apr_pool_create_unmanaged_ex_debug(apr_pool_t **newpool,
00268                                                    apr_abortfunc_t abort_fn,
00269                                                    apr_allocator_t *allocator,
00270                                                    const char *file_line)
00271                           __attribute__((nonnull(1)));
00272 
00273 #if APR_POOL_DEBUG
00274 #define apr_pool_create_unmanaged_ex(newpool, abort_fn, allocator)  \
00275     apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
00276                                   APR_POOL__FILE_LINE__)
00277 
00278 #endif
00279 
00280 /**
00281  * Create a new pool.
00282  * @param newpool The pool we have just created.
00283  * @param parent The parent pool.  If this is NULL, the new pool is a root
00284  *        pool.  If it is non-NULL, the new pool will inherit all
00285  *        of its parent pool's attributes, except the apr_pool_t will
00286  *        be a sub-pool.
00287  * @remark This function is thread-safe, in the sense that multiple threads
00288  *         can safely create subpools of the same parent pool concurrently.
00289  *         Similarly, a subpool can be created by one thread at the same
00290  *         time that another thread accesses the parent pool.
00291  */
00292 #if defined(DOXYGEN)
00293 APR_DECLARE(apr_status_t) apr_pool_create(apr_pool_t **newpool,
00294                                           apr_pool_t *parent);
00295 #else
00296 #if APR_POOL_DEBUG
00297 #define apr_pool_create(newpool, parent) \
00298     apr_pool_create_ex_debug(newpool, parent, NULL, NULL, \
00299                              APR_POOL__FILE_LINE__)
00300 #else
00301 #define apr_pool_create(newpool, parent) \
00302     apr_pool_create_ex(newpool, parent, NULL, NULL)
00303 #endif
00304 #endif
00305 
00306 /**
00307  * Create a new pool.
00308  * @param newpool The pool we have just created.
00309  */
00310 #if defined(DOXYGEN)
00311 APR_DECLARE(apr_status_t) apr_pool_create_unmanaged(apr_pool_t **newpool);
00312 #else
00313 #if APR_POOL_DEBUG
00314 #define apr_pool_create_unmanaged(newpool) \
00315     apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
00316                                   APR_POOL__FILE_LINE__)
00317 #else
00318 #define apr_pool_create_unmanaged(newpool) \
00319     apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
00320 #endif
00321 #endif
00322 
00323 /**
00324  * Find the pool's allocator
00325  * @param pool The pool to get the allocator from.
00326  */
00327 APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool)
00328                                __attribute__((nonnull(1)));
00329 
00330 /**
00331  * Clear all memory in the pool and run all the cleanups. This also destroys all
00332  * subpools.
00333  * @param p The pool to clear
00334  * @remark This does not actually free the memory, it just allows the pool
00335  *         to re-use this memory for the next allocation.
00336  * @see apr_pool_destroy()
00337  */
00338 APR_DECLARE(void) apr_pool_clear(apr_pool_t *p) __attribute__((nonnull(1)));
00339 
00340 /**
00341  * Debug version of apr_pool_clear.
00342  * @param p See: apr_pool_clear.
00343  * @param file_line Where the function is called from.
00344  *        This is usually APR_POOL__FILE_LINE__.
00345  * @remark Only available when APR_POOL_DEBUG is defined.
00346  *         Call this directly if you have you apr_pool_clear
00347  *         calls in a wrapper function and wish to override
00348  *         the file_line argument to reflect the caller of
00349  *         your wrapper function.  If you do not have
00350  *         apr_pool_clear in a wrapper, trust the macro
00351  *         and don't call apr_pool_destroy_clear directly.
00352  */
00353 APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *p,
00354                                        const char *file_line)
00355                   __attribute__((nonnull(1)));
00356 
00357 #if APR_POOL_DEBUG
00358 #define apr_pool_clear(p) \
00359     apr_pool_clear_debug(p, APR_POOL__FILE_LINE__)
00360 #endif
00361 
00362 /**
00363  * Destroy the pool. This takes similar action as apr_pool_clear() and then
00364  * frees all the memory.
00365  * @param p The pool to destroy
00366  * @remark This will actually free the memory
00367  */
00368 APR_DECLARE(void) apr_pool_destroy(apr_pool_t *p) __attribute__((nonnull(1)));
00369 
00370 /**
00371  * Debug version of apr_pool_destroy.
00372  * @param p See: apr_pool_destroy.
00373  * @param file_line Where the function is called from.
00374  *        This is usually APR_POOL__FILE_LINE__.
00375  * @remark Only available when APR_POOL_DEBUG is defined.
00376  *         Call this directly if you have you apr_pool_destroy
00377  *         calls in a wrapper function and wish to override
00378  *         the file_line argument to reflect the caller of
00379  *         your wrapper function.  If you do not have
00380  *         apr_pool_destroy in a wrapper, trust the macro
00381  *         and don't call apr_pool_destroy_debug directly.
00382  */
00383 APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *p,
00384                                          const char *file_line)
00385                   __attribute__((nonnull(1)));
00386 
00387 #if APR_POOL_DEBUG
00388 #define apr_pool_destroy(p) \
00389     apr_pool_destroy_debug(p, APR_POOL__FILE_LINE__)
00390 #endif
00391 
00392 
00393 /*
00394  * Memory allocation
00395  */
00396 
00397 /**
00398  * Allocate a block of memory from a pool
00399  * @param p The pool to allocate from
00400  * @param size The amount of memory to allocate
00401  * @return The allocated memory
00402  */
00403 APR_DECLARE(void *) apr_palloc(apr_pool_t *p, apr_size_t size)
00404                     __attribute__((nonnull(1)));
00405 
00406 /**
00407  * Debug version of apr_palloc
00408  * @param p See: apr_palloc
00409  * @param size See: apr_palloc
00410  * @param file_line Where the function is called from.
00411  *        This is usually APR_POOL__FILE_LINE__.
00412  * @return See: apr_palloc
00413  */
00414 APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *p, apr_size_t size,
00415                                      const char *file_line)
00416                     __attribute__((nonnull(1)));
00417 
00418 #if APR_POOL_DEBUG
00419 #define apr_palloc(p, size) \
00420     apr_palloc_debug(p, size, APR_POOL__FILE_LINE__)
00421 #endif
00422 
00423 /**
00424  * Allocate a block of memory from a pool and set all of the memory to 0
00425  * @param p The pool to allocate from
00426  * @param size The amount of memory to allocate
00427  * @return The allocated memory
00428  */
00429 #if defined(DOXYGEN)
00430 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *p, apr_size_t size);
00431 #elif !APR_POOL_DEBUG
00432 #define apr_pcalloc(p, size) memset(apr_palloc(p, size), 0, size)
00433 #endif
00434 
00435 /**
00436  * Debug version of apr_pcalloc
00437  * @param p See: apr_pcalloc
00438  * @param size See: apr_pcalloc
00439  * @param file_line Where the function is called from.
00440  *        This is usually APR_POOL__FILE_LINE__.
00441  * @return See: apr_pcalloc
00442  */
00443 APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *p, apr_size_t size,
00444                                       const char *file_line)
00445                     __attribute__((nonnull(1)));
00446 
00447 #if APR_POOL_DEBUG
00448 #define apr_pcalloc(p, size) \
00449     apr_pcalloc_debug(p, size, APR_POOL__FILE_LINE__)
00450 #endif
00451 
00452 
00453 /*
00454  * Pool Properties
00455  */
00456 
00457 /**
00458  * Set the function to be called when an allocation failure occurs.
00459  * @remark If the program wants APR to exit on a memory allocation error,
00460  *      then this function can be called to set the callback to use (for
00461  *      performing cleanup and then exiting). If this function is not called,
00462  *      then APR will return an error and expect the calling program to
00463  *      deal with the error accordingly.
00464  */
00465 APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abortfunc,
00466                                      apr_pool_t *pool)
00467                   __attribute__((nonnull(2)));
00468 
00469 /**
00470  * Get the abort function associated with the specified pool.
00471  * @param pool The pool for retrieving the abort function.
00472  * @return The abort function for the given pool.
00473  */
00474 APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool)
00475                              __attribute__((nonnull(1)));
00476 
00477 /**
00478  * Get the parent pool of the specified pool.
00479  * @param pool The pool for retrieving the parent pool.
00480  * @return The parent of the given pool.
00481  */
00482 APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool)
00483                           __attribute__((nonnull(1)));
00484 
00485 /**
00486  * Determine if pool a is an ancestor of pool b.
00487  * @param a The pool to search
00488  * @param b The pool to search for
00489  * @return True if a is an ancestor of b, NULL is considered an ancestor
00490  *         of all pools.
00491  * @remark if compiled with APR_POOL_DEBUG, this function will also
00492  * return true if A is a pool which has been guaranteed by the caller
00493  * (using apr_pool_join) to have a lifetime at least as long as some
00494  * ancestor of pool B.
00495  */
00496 APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b);
00497 
00498 /**
00499  * Tag a pool (give it a name)
00500  * @param pool The pool to tag
00501  * @param tag  The tag
00502  */
00503 APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag)
00504                   __attribute__((nonnull(1)));
00505 
00506 
00507 /*
00508  * User data management
00509  */
00510 
00511 /**
00512  * Set the data associated with the current pool
00513  * @param data The user data associated with the pool.
00514  * @param key The key to use for association
00515  * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
00516  * @param pool The current pool
00517  * @warning The data to be attached to the pool should have a life span
00518  *          at least as long as the pool it is being attached to.
00519  *
00520  *      Users of APR must take EXTREME care when choosing a key to
00521  *      use for their data.  It is possible to accidentally overwrite
00522  *      data by choosing a key that another part of the program is using.
00523  *      Therefore it is advised that steps are taken to ensure that unique
00524  *      keys are used for all of the userdata objects in a particular pool
00525  *      (the same key in two different pools or a pool and one of its
00526  *      subpools is okay) at all times.  Careful namespace prefixing of
00527  *      key names is a typical way to help ensure this uniqueness.
00528  *
00529  */
00530 APR_DECLARE(apr_status_t) apr_pool_userdata_set(const void *data,
00531                                                 const char *key,
00532                                                 apr_status_t (*cleanup)(void *),
00533                                                 apr_pool_t *pool)
00534                           __attribute__((nonnull(2,4)));
00535 
00536 /**
00537  * Set the data associated with the current pool
00538  * @param data The user data associated with the pool.
00539  * @param key The key to use for association
00540  * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
00541  * @param pool The current pool
00542  * @note same as apr_pool_userdata_set(), except that this version doesn't
00543  *       make a copy of the key (this function is useful, for example, when
00544  *       the key is a string literal)
00545  * @warning This should NOT be used if the key could change addresses by
00546  *       any means between the apr_pool_userdata_setn() call and a
00547  *       subsequent apr_pool_userdata_get() on that key, such as if a
00548  *       static string is used as a userdata key in a DSO and the DSO could
00549  *       be unloaded and reloaded between the _setn() and the _get().  You
00550  *       MUST use apr_pool_userdata_set() in such cases.
00551  * @warning More generally, the key and the data to be attached to the
00552  *       pool should have a life span at least as long as the pool itself.
00553  *
00554  */
00555 APR_DECLARE(apr_status_t) apr_pool_userdata_setn(
00556                                 const void *data, const char *key,
00557                                 apr_status_t (*cleanup)(void *),
00558                                 apr_pool_t *pool)
00559                           __attribute__((nonnull(2,4)));
00560 
00561 /**
00562  * Return the data associated with the current pool.
00563  * @param data The user data associated with the pool.
00564  * @param key The key for the data to retrieve
00565  * @param pool The current pool.
00566  */
00567 APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
00568                                                 apr_pool_t *pool)
00569                           __attribute__((nonnull(1,2,3)));
00570 
00571 
00572 /**
00573  * @defgroup PoolCleanup  Pool Cleanup Functions
00574  *
00575  * Cleanups are performed in the reverse order they were registered.  That is:
00576  * Last In, First Out.  A cleanup function can safely allocate memory from
00577  * the pool that is being cleaned up. It can also safely register additional
00578  * cleanups which will be run LIFO, directly after the current cleanup
00579  * terminates.  Cleanups have to take caution in calling functions that
00580  * create subpools. Subpools, created during cleanup will NOT automatically
00581  * be cleaned up.  In other words, cleanups are to clean up after themselves.
00582  *
00583  * @{
00584  */
00585 
00586 /**
00587  * Register a function to be called when a pool is cleared or destroyed
00588  * @param p The pool register the cleanup with
00589  * @param data The data to pass to the cleanup function.
00590  * @param plain_cleanup The function to call when the pool is cleared
00591  *                      or destroyed
00592  * @param child_cleanup The function to call when a child process is about
00593  *                      to exec - this function is called in the child, obviously!
00594  */
00595 APR_DECLARE(void) apr_pool_cleanup_register(
00596                             apr_pool_t *p, const void *data,
00597                             apr_status_t (*plain_cleanup)(void *),
00598                             apr_status_t (*child_cleanup)(void *))
00599                   __attribute__((nonnull(3,4)));
00600 
00601 /**
00602  * Register a function to be called when a pool is cleared or destroyed.
00603  *
00604  * Unlike apr_pool_cleanup_register which register a cleanup
00605  * that is called AFTER all subpools are destroyed this function register
00606  * a function that will be called before any of the subpool is destoryed.
00607  *
00608  * @param p The pool register the cleanup with
00609  * @param data The data to pass to the cleanup function.
00610  * @param plain_cleanup The function to call when the pool is cleared
00611  *                      or destroyed
00612  */
00613 APR_DECLARE(void) apr_pool_pre_cleanup_register(
00614                             apr_pool_t *p, const void *data,
00615                             apr_status_t (*plain_cleanup)(void *))
00616                   __attribute__((nonnull(3)));
00617 
00618 /**
00619  * Remove a previously registered cleanup function.
00620  * 
00621  * The cleanup most recently registered with @a p having the same values of
00622  * @a data and @a cleanup will be removed.
00623  *
00624  * @param p The pool to remove the cleanup from
00625  * @param data The data of the registered cleanup
00626  * @param cleanup The function to remove from cleanup
00627  * @remarks For some strange reason only the plain_cleanup is handled by this
00628  *          function
00629  */
00630 APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
00631                                         apr_status_t (*cleanup)(void *))
00632                   __attribute__((nonnull(3)));
00633 
00634 /**
00635  * Replace the child cleanup function of a previously registered cleanup.
00636  * 
00637  * The cleanup most recently registered with @a p having the same values of
00638  * @a data and @a plain_cleanup will have the registered child cleanup
00639  * function replaced with @a child_cleanup.
00640  *
00641  * @param p The pool of the registered cleanup
00642  * @param data The data of the registered cleanup
00643  * @param plain_cleanup The plain cleanup function of the registered cleanup
00644  * @param child_cleanup The function to register as the child cleanup
00645  */
00646 APR_DECLARE(void) apr_pool_child_cleanup_set(
00647                         apr_pool_t *p, const void *data,
00648                         apr_status_t (*plain_cleanup)(void *),
00649                         apr_status_t (*child_cleanup)(void *))
00650                   __attribute__((nonnull(3,4)));
00651 
00652 /**
00653  * Run the specified cleanup function immediately and unregister it.
00654  *
00655  * The cleanup most recently registered with @a p having the same values of
00656  * @a data and @a cleanup will be removed and @a cleanup will be called
00657  * with @a data as the argument.
00658  *
00659  * @param p The pool to remove the cleanup from
00660  * @param data The data to remove from cleanup
00661  * @param cleanup The function to remove from cleanup
00662  */
00663 APR_DECLARE(apr_status_t) apr_pool_cleanup_run(apr_pool_t *p, void *data,
00664                                                apr_status_t (*cleanup)(void *))
00665                           __attribute__((nonnull(3)));
00666 
00667 /**
00668  * An empty cleanup function.
00669  * 
00670  * Passed to apr_pool_cleanup_register() when no cleanup is required.
00671  *
00672  * @param data The data to cleanup, will not be used by this function.
00673  */
00674 APR_DECLARE_NONSTD(apr_status_t) apr_pool_cleanup_null(void *data);
00675 
00676 /**
00677  * Run all registered child cleanups, in preparation for an exec()
00678  * call in a forked child -- close files, etc., but *don't* flush I/O
00679  * buffers, *don't* wait for subprocesses, and *don't* free any
00680  * memory.
00681  */
00682 APR_DECLARE(void) apr_pool_cleanup_for_exec(void);
00683 
00684 /** @} */
00685 
00686 /**
00687  * @defgroup PoolDebug Pool Debugging functions.
00688  *
00689  * pools have nested lifetimes -- sub_pools are destroyed when the
00690  * parent pool is cleared.  We allow certain liberties with operations
00691  * on things such as tables (and on other structures in a more general
00692  * sense) where we allow the caller to insert values into a table which
00693  * were not allocated from the table's pool.  The table's data will
00694  * remain valid as long as all the pools from which its values are
00695  * allocated remain valid.
00696  *
00697  * For example, if B is a sub pool of A, and you build a table T in
00698  * pool B, then it's safe to insert data allocated in A or B into T
00699  * (because B lives at most as long as A does, and T is destroyed when
00700  * B is cleared/destroyed).  On the other hand, if S is a table in
00701  * pool A, it is safe to insert data allocated in A into S, but it
00702  * is *not safe* to insert data allocated from B into S... because
00703  * B can be cleared/destroyed before A is (which would leave dangling
00704  * pointers in T's data structures).
00705  *
00706  * In general we say that it is safe to insert data into a table T
00707  * if the data is allocated in any ancestor of T's pool.  This is the
00708  * basis on which the APR_POOL_DEBUG code works -- it tests these ancestor
00709  * relationships for all data inserted into tables.  APR_POOL_DEBUG also
00710  * provides tools (apr_pool_find, and apr_pool_is_ancestor) for other
00711  * folks to implement similar restrictions for their own data
00712  * structures.
00713  *
00714  * However, sometimes this ancestor requirement is inconvenient --
00715  * sometimes it's necessary to create a sub pool where the sub pool is
00716  * guaranteed to have the same lifetime as the parent pool.  This is a
00717  * guarantee implemented by the *caller*, not by the pool code.  That
00718  * is, the caller guarantees they won't destroy the sub pool
00719  * individually prior to destroying the parent pool.
00720  *
00721  * In this case the caller must call apr_pool_join() to indicate this
00722  * guarantee to the APR_POOL_DEBUG code.
00723  *
00724  * These functions are only implemented when #APR_POOL_DEBUG is set.
00725  *
00726  * @{
00727  */
00728 #if APR_POOL_DEBUG || defined(DOXYGEN)
00729 /**
00730  * Guarantee that a subpool has the same lifetime as the parent.
00731  * @param p The parent pool
00732  * @param sub The subpool
00733  */
00734 APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub)
00735                   __attribute__((nonnull(2)));
00736 
00737 /**
00738  * Find a pool from something allocated in it.
00739  * @param mem The thing allocated in the pool
00740  * @return The pool it is allocated in
00741  */
00742 APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem);
00743 
00744 /**
00745  * Report the number of bytes currently in the pool
00746  * @param p The pool to inspect
00747  * @param recurse Recurse/include the subpools' sizes
00748  * @return The number of bytes
00749  */
00750 APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *p, int recurse)
00751                         __attribute__((nonnull(1)));
00752 
00753 /**
00754  * Lock a pool
00755  * @param pool The pool to lock
00756  * @param flag  The flag
00757  */
00758 APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag);
00759 
00760 /* @} */
00761 
00762 #else /* APR_POOL_DEBUG or DOXYGEN */
00763 
00764 #ifdef apr_pool_join
00765 #undef apr_pool_join
00766 #endif
00767 #define apr_pool_join(a,b)
00768 
00769 #ifdef apr_pool_lock
00770 #undef apr_pool_lock
00771 #endif
00772 #define apr_pool_lock(pool, lock)
00773 
00774 #endif /* APR_POOL_DEBUG or DOXYGEN */
00775 
00776 /** @} */
00777 
00778 #ifdef __cplusplus
00779 }
00780 #endif
00781 
00782 #endif /* !APR_POOLS_H */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines