Adjust fileList from perl
[msysgit.git] / include / apr-0 / apr_pools.h
blob2f4353f0ff9d0399d3a350dc75c0c030a0bdaea4
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef APR_POOLS_H
18 #define APR_POOLS_H
20 /**
21 * @file apr_pools.h
22 * @brief APR memory allocation
24 * Resource allocation routines...
26 * designed so that we don't have to keep track of EVERYTHING so that
27 * it can be explicitly freed later (a fundamentally unsound strategy ---
28 * particularly in the presence of die()).
30 * Instead, we maintain pools, and allocate items (both memory and I/O
31 * handlers) from the pools --- currently there are two, one for per
32 * transaction info, and one for config info. When a transaction is over,
33 * we can delete everything in the per-transaction apr_pool_t without fear,
34 * and without thinking too hard about it either.
37 #include "apr.h"
38 #include "apr_errno.h"
39 #include "apr_general.h" /* for APR_STRINGIFY */
40 #define APR_WANT_MEMFUNC /**< for no good reason? */
41 #include "apr_want.h"
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
47 /**
48 * @defgroup apr_pools Memory Pool Functions
49 * @ingroup APR
50 * @{
53 /** The fundamental pool type */
54 typedef struct apr_pool_t apr_pool_t;
57 /**
58 * Declaration helper macro to construct apr_foo_pool_get()s.
60 * This standardized macro is used by opaque (APR) data types to return
61 * the apr_pool_t that is associated with the data type.
63 * APR_POOL_DECLARE_ACCESSOR() is used in a header file to declare the
64 * accessor function. A typical usage and result would be:
65 * <pre>
66 * APR_POOL_DECLARE_ACCESSOR(file);
67 * becomes:
68 * APR_DECLARE(apr_pool_t *) apr_file_pool_get(apr_file_t *ob);
69 * </pre>
70 * @remark Doxygen unwraps this macro (via doxygen.conf) to provide
71 * actual help for each specific occurance of apr_foo_pool_get.
72 * @remark the linkage is specified for APR. It would be possible to expand
73 * the macros to support other linkages.
75 #define APR_POOL_DECLARE_ACCESSOR(type) \
76 APR_DECLARE(apr_pool_t *) apr_##type##_pool_get \
77 (const apr_##type##_t *the##type)
79 /**
80 * Implementation helper macro to provide apr_foo_pool_get()s.
82 * In the implementation, the APR_POOL_IMPLEMENT_ACCESSOR() is used to
83 * actually define the function. It assumes the field is named "pool".
85 #define APR_POOL_IMPLEMENT_ACCESSOR(type) \
86 APR_DECLARE(apr_pool_t *) apr_##type##_pool_get \
87 (const apr_##type##_t *the##type) \
88 { return the##type->pool; }
91 /**
92 * Pool debug levels
94 * <pre>
95 * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
96 * ---------------------------------
97 * | | | | | | | | x | General debug code enabled (usefull in
98 * combination with --with-efence).
100 * | | | | | | | x | | Verbose output on stderr (report
101 * CREATE, CLEAR, DESTROY).
103 * | | | | x | | | | | Verbose output on stderr (report
104 * PALLOC, PCALLOC).
106 * | | | | | | x | | | Lifetime checking. On each use of a
107 * pool, check its lifetime. If the pool
108 * is out of scope, abort().
109 * In combination with the verbose flag
110 * above, it will output LIFE in such an
111 * event prior to aborting.
113 * | | | | | x | | | | Pool owner checking. On each use of a
114 * pool, check if the current thread is the
115 * pools owner. If not, abort(). In
116 * combination with the verbose flag above,
117 * it will output OWNER in such an event
118 * prior to aborting. Use the debug
119 * function apr_pool_owner_set() to switch
120 * a pools ownership.
122 * When no debug level was specified, assume general debug mode.
123 * If level 0 was specified, debugging is switched off
124 * </pre>
126 #if defined(APR_POOL_DEBUG)
127 #if (APR_POOL_DEBUG != 0) && (APR_POOL_DEBUG - 0 == 0)
128 #undef APR_POOL_DEBUG
129 #define APR_POOL_DEBUG 1
130 #endif
131 #else
132 #define APR_POOL_DEBUG 0
133 #endif
135 /** the place in the code where the particular function was called */
136 #define APR_POOL__FILE_LINE__ __FILE__ ":" APR_STRINGIFY(__LINE__)
140 /** A function that is called when allocation fails. */
141 typedef int (*apr_abortfunc_t)(int retcode);
144 * APR memory structure manipulators (pools, tables, and arrays).
148 * Initialization
152 * Setup all of the internal structures required to use pools
153 * @remark Programs do NOT need to call this directly. APR will call this
154 * automatically from apr_initialize.
155 * @internal
157 APR_DECLARE(apr_status_t) apr_pool_initialize(void);
160 * Tear down all of the internal structures required to use pools
161 * @remark Programs do NOT need to call this directly. APR will call this
162 * automatically from apr_terminate.
163 * @internal
165 APR_DECLARE(void) apr_pool_terminate(void);
169 * Pool creation/destruction
172 #include "apr_allocator.h"
175 * Create a new pool.
176 * @param newpool The pool we have just created.
177 * @param parent The parent pool. If this is NULL, the new pool is a root
178 * pool. If it is non-NULL, the new pool will inherit all
179 * of its parent pool's attributes, except the apr_pool_t will
180 * be a sub-pool.
181 * @param abort_fn A function to use if the pool cannot allocate more memory.
182 * @param allocator The allocator to use with the new pool. If NULL the
183 * allocator of the parent pool will be used.
185 APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
186 apr_pool_t *parent,
187 apr_abortfunc_t abort_fn,
188 apr_allocator_t *allocator);
191 * Debug version of apr_pool_create_ex.
192 * @param newpool @see apr_pool_create.
193 * @param parent @see apr_pool_create.
194 * @param abort_fn @see apr_pool_create.
195 * @param allocator @see apr_pool_create.
196 * @param file_line Where the function is called from.
197 * This is usually APR_POOL__FILE_LINE__.
198 * @remark Only available when APR_POOL_DEBUG is defined.
199 * Call this directly if you have you apr_pool_create_ex
200 * calls in a wrapper function and wish to override
201 * the file_line argument to reflect the caller of
202 * your wrapper function. If you do not have
203 * apr_pool_create_ex in a wrapper, trust the macro
204 * and don't call apr_pool_create_ex_debug directly.
206 APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
207 apr_pool_t *parent,
208 apr_abortfunc_t abort_fn,
209 apr_allocator_t *allocator,
210 const char *file_line);
212 #if APR_POOL_DEBUG
213 #define apr_pool_create_ex(newpool, parent, abort_fn, allocator) \
214 apr_pool_create_ex_debug(newpool, parent, abort_fn, allocator, \
215 APR_POOL__FILE_LINE__)
216 #endif
219 * Create a new pool.
220 * @param newpool The pool we have just created.
221 * @param parent The parent pool. If this is NULL, the new pool is a root
222 * pool. If it is non-NULL, the new pool will inherit all
223 * of its parent pool's attributes, except the apr_pool_t will
224 * be a sub-pool.
226 #if defined(DOXYGEN)
227 APR_DECLARE(apr_status_t) apr_pool_create(apr_pool_t **newpool,
228 apr_pool_t *parent);
229 #else
230 #if APR_POOL_DEBUG
231 #define apr_pool_create(newpool, parent) \
232 apr_pool_create_ex_debug(newpool, parent, NULL, NULL, \
233 APR_POOL__FILE_LINE__)
234 #else
235 #define apr_pool_create(newpool, parent) \
236 apr_pool_create_ex(newpool, parent, NULL, NULL)
237 #endif
238 #endif
240 /** @deprecated @see apr_pool_create_ex */
241 #if APR_POOL_DEBUG
242 #define apr_pool_sub_make(newpool, parent, abort_fn) \
243 (void)apr_pool_create_ex_debug(newpool, parent, abort_fn, \
244 NULL, \
245 APR_POOL__FILE_LINE__)
246 #else
247 #define apr_pool_sub_make(newpool, parent, abort_fn) \
248 (void)apr_pool_create_ex(newpool, parent, abort_fn, NULL)
249 #endif
252 * Find the pools allocator
253 * @param pool The pool to get the allocator from.
255 APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool);
258 * Clear all memory in the pool and run all the cleanups. This also destroys all
259 * subpools.
260 * @param p The pool to clear
261 * @remark This does not actually free the memory, it just allows the pool
262 * to re-use this memory for the next allocation.
263 * @see apr_pool_destroy()
265 APR_DECLARE(void) apr_pool_clear(apr_pool_t *p);
268 * Debug version of apr_pool_clear.
269 * @param p See: apr_pool_clear.
270 * @param file_line Where the function is called from.
271 * This is usually APR_POOL__FILE_LINE__.
272 * @remark Only available when APR_POOL_DEBUG is defined.
273 * Call this directly if you have you apr_pool_clear
274 * calls in a wrapper function and wish to override
275 * the file_line argument to reflect the caller of
276 * your wrapper function. If you do not have
277 * apr_pool_clear in a wrapper, trust the macro
278 * and don't call apr_pool_destroy_clear directly.
280 APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *p,
281 const char *file_line);
283 #if APR_POOL_DEBUG
284 #define apr_pool_clear(p) \
285 apr_pool_clear_debug(p, APR_POOL__FILE_LINE__)
286 #endif
289 * Destroy the pool. This takes similar action as apr_pool_clear() and then
290 * frees all the memory.
291 * @param p The pool to destroy
292 * @remark This will actually free the memory
294 APR_DECLARE(void) apr_pool_destroy(apr_pool_t *p);
297 * Debug version of apr_pool_destroy.
298 * @param p See: apr_pool_destroy.
299 * @param file_line Where the function is called from.
300 * This is usually APR_POOL__FILE_LINE__.
301 * @remark Only available when APR_POOL_DEBUG is defined.
302 * Call this directly if you have you apr_pool_destroy
303 * calls in a wrapper function and wish to override
304 * the file_line argument to reflect the caller of
305 * your wrapper function. If you do not have
306 * apr_pool_destroy in a wrapper, trust the macro
307 * and don't call apr_pool_destroy_debug directly.
309 APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *p,
310 const char *file_line);
312 #if APR_POOL_DEBUG
313 #define apr_pool_destroy(p) \
314 apr_pool_destroy_debug(p, APR_POOL__FILE_LINE__)
315 #endif
319 * Memory allocation
323 * Allocate a block of memory from a pool
324 * @param p The pool to allocate from
325 * @param size The amount of memory to allocate
326 * @return The allocated memory
328 APR_DECLARE(void *) apr_palloc(apr_pool_t *p, apr_size_t size);
331 * Debug version of apr_palloc
332 * @param p See: apr_palloc
333 * @param size See: apr_palloc
334 * @param file_line Where the function is called from.
335 * This is usually APR_POOL__FILE_LINE__.
336 * @return See: apr_palloc
338 APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *p, apr_size_t size,
339 const char *file_line);
341 #if APR_POOL_DEBUG
342 #define apr_palloc(p, size) \
343 apr_palloc_debug(p, size, APR_POOL__FILE_LINE__)
344 #endif
347 * Allocate a block of memory from a pool and set all of the memory to 0
348 * @param p The pool to allocate from
349 * @param size The amount of memory to allocate
350 * @return The allocated memory
352 #if defined(DOXYGEN)
353 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *p, apr_size_t size);
354 #elif !APR_POOL_DEBUG
355 #define apr_pcalloc(p, size) memset(apr_palloc(p, size), 0, size)
356 #endif
359 * Debug version of apr_pcalloc
360 * @param p See: apr_pcalloc
361 * @param size See: apr_pcalloc
362 * @param file_line Where the function is called from.
363 * This is usually APR_POOL__FILE_LINE__.
364 * @return See: apr_pcalloc
366 APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *p, apr_size_t size,
367 const char *file_line);
369 #if APR_POOL_DEBUG
370 #define apr_pcalloc(p, size) \
371 apr_pcalloc_debug(p, size, APR_POOL__FILE_LINE__)
372 #endif
376 * Pool Properties
380 * Set the function to be called when an allocation failure occurs.
381 * @remark If the program wants APR to exit on a memory allocation error,
382 * then this function can be called to set the callback to use (for
383 * performing cleanup and then exiting). If this function is not called,
384 * then APR will return an error and expect the calling program to
385 * deal with the error accordingly.
387 APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abortfunc,
388 apr_pool_t *pool);
390 /** @deprecated @see apr_pool_abort_set */
391 APR_DECLARE(void) apr_pool_set_abort(apr_abortfunc_t abortfunc,
392 apr_pool_t *pool);
395 * Get the abort function associated with the specified pool.
396 * @param pool The pool for retrieving the abort function.
397 * @return The abort function for the given pool.
399 APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool);
401 /** @deprecated @see apr_pool_abort_get */
402 APR_DECLARE(apr_abortfunc_t) apr_pool_get_abort(apr_pool_t *pool);
405 * Get the parent pool of the specified pool.
406 * @param pool The pool for retrieving the parent pool.
407 * @return The parent of the given pool.
409 APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool);
411 /** @deprecated @see apr_pool_parent_get */
412 APR_DECLARE(apr_pool_t *) apr_pool_get_parent(apr_pool_t *pool);
415 * Determine if pool a is an ancestor of pool b
416 * @param a The pool to search
417 * @param b The pool to search for
418 * @return True if a is an ancestor of b, NULL is considered an ancestor
419 * of all pools.
421 APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b);
424 * Tag a pool (give it a name)
425 * @param pool The pool to tag
426 * @param tag The tag
428 APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag);
432 * User data management
436 * Set the data associated with the current pool
437 * @param data The user data associated with the pool.
438 * @param key The key to use for association
439 * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
440 * @param pool The current pool
441 * @warning The data to be attached to the pool should have a life span
442 * at least as long as the pool it is being attached to.
444 * Users of APR must take EXTREME care when choosing a key to
445 * use for their data. It is possible to accidentally overwrite
446 * data by choosing a key that another part of the program is using.
447 * Therefore it is advised that steps are taken to ensure that unique
448 * keys are used for all of the userdata objects in a particular pool
449 * (the same key in two different pools or a pool and one of its
450 * subpools is okay) at all times. Careful namespace prefixing of
451 * key names is a typical way to help ensure this uniqueness.
453 APR_DECLARE(apr_status_t) apr_pool_userdata_set(
454 const void *data,
455 const char *key,
456 apr_status_t (*cleanup)(void *),
457 apr_pool_t *pool);
460 * Set the data associated with the current pool
461 * @param data The user data associated with the pool.
462 * @param key The key to use for association
463 * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
464 * @param pool The current pool
465 * @note same as apr_pool_userdata_set(), except that this version doesn't
466 * make a copy of the key (this function is useful, for example, when
467 * the key is a string literal)
468 * @warning This should NOT be used if the key could change addresses by
469 * any means between the apr_pool_userdata_setn() call and a
470 * subsequent apr_pool_userdata_get() on that key, such as if a
471 * static string is used as a userdata key in a DSO and the DSO could
472 * be unloaded and reloaded between the _setn() and the _get(). You
473 * MUST use apr_pool_userdata_set() in such cases.
474 * @warning More generally, the key and the data to be attached to the
475 * pool should have a life span at least as long as the pool itself.
478 APR_DECLARE(apr_status_t) apr_pool_userdata_setn(
479 const void *data,
480 const char *key,
481 apr_status_t (*cleanup)(void *),
482 apr_pool_t *pool);
485 * Return the data associated with the current pool.
486 * @param data The user data associated with the pool.
487 * @param key The key for the data to retrieve
488 * @param pool The current pool.
490 APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
491 apr_pool_t *pool);
495 * Cleanup
497 * Cleanups are performed in the reverse order they were registered. That is:
498 * Last In, First Out.
502 * Register a function to be called when a pool is cleared or destroyed
503 * @param p The pool register the cleanup with
504 * @param data The data to pass to the cleanup function.
505 * @param plain_cleanup The function to call when the pool is cleared
506 * or destroyed
507 * @param child_cleanup The function to call when a child process is being
508 * shutdown - this function is called in the child, obviously!
510 APR_DECLARE(void) apr_pool_cleanup_register(
511 apr_pool_t *p,
512 const void *data,
513 apr_status_t (*plain_cleanup)(void *),
514 apr_status_t (*child_cleanup)(void *));
517 * Remove a previously registered cleanup function
518 * @param p The pool remove the cleanup from
519 * @param data The data to remove from cleanup
520 * @param cleanup The function to remove from cleanup
521 * @remarks For some strange reason only the plain_cleanup is handled by this
522 * function
524 APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
525 apr_status_t (*cleanup)(void *));
528 * Replace the child cleanup of a previously registered cleanup
529 * @param p The pool of the registered cleanup
530 * @param data The data of the registered cleanup
531 * @param plain_cleanup The plain cleanup function of the registered cleanup
532 * @param child_cleanup The function to register as the child cleanup
534 APR_DECLARE(void) apr_pool_child_cleanup_set(
535 apr_pool_t *p,
536 const void *data,
537 apr_status_t (*plain_cleanup)(void *),
538 apr_status_t (*child_cleanup)(void *));
541 * Run the specified cleanup function immediately and unregister it. Use
542 * @a data instead of the data that was registered with the cleanup.
543 * @param p The pool remove the cleanup from
544 * @param data The data to remove from cleanup
545 * @param cleanup The function to remove from cleanup
547 APR_DECLARE(apr_status_t) apr_pool_cleanup_run(
548 apr_pool_t *p,
549 void *data,
550 apr_status_t (*cleanup)(void *));
553 * An empty cleanup function
554 * @param data The data to cleanup
556 APR_DECLARE_NONSTD(apr_status_t) apr_pool_cleanup_null(void *data);
558 /* Preparing for exec() --- close files, etc., but *don't* flush I/O
559 * buffers, *don't* wait for subprocesses, and *don't* free any memory.
562 * Run all of the child_cleanups, so that any unnecessary files are
563 * closed because we are about to exec a new program
565 APR_DECLARE(void) apr_pool_cleanup_for_exec(void);
569 * @defgroup PoolDebug Pool Debugging functions.
571 * pools have nested lifetimes -- sub_pools are destroyed when the
572 * parent pool is cleared. We allow certain liberties with operations
573 * on things such as tables (and on other structures in a more general
574 * sense) where we allow the caller to insert values into a table which
575 * were not allocated from the table's pool. The table's data will
576 * remain valid as long as all the pools from which its values are
577 * allocated remain valid.
579 * For example, if B is a sub pool of A, and you build a table T in
580 * pool B, then it's safe to insert data allocated in A or B into T
581 * (because B lives at most as long as A does, and T is destroyed when
582 * B is cleared/destroyed). On the other hand, if S is a table in
583 * pool A, it is safe to insert data allocated in A into S, but it
584 * is *not safe* to insert data allocated from B into S... because
585 * B can be cleared/destroyed before A is (which would leave dangling
586 * pointers in T's data structures).
588 * In general we say that it is safe to insert data into a table T
589 * if the data is allocated in any ancestor of T's pool. This is the
590 * basis on which the APR_POOL_DEBUG code works -- it tests these ancestor
591 * relationships for all data inserted into tables. APR_POOL_DEBUG also
592 * provides tools (apr_pool_find, and apr_pool_is_ancestor) for other
593 * folks to implement similar restrictions for their own data
594 * structures.
596 * However, sometimes this ancestor requirement is inconvenient --
597 * sometimes we're forced to create a sub pool (such as through
598 * apr_sub_req_lookup_uri), and the sub pool is guaranteed to have
599 * the same lifetime as the parent pool. This is a guarantee implemented
600 * by the *caller*, not by the pool code. That is, the caller guarantees
601 * they won't destroy the sub pool individually prior to destroying the
602 * parent pool.
604 * In this case the caller must call apr_pool_join() to indicate this
605 * guarantee to the APR_POOL_DEBUG code. There are a few examples spread
606 * through the standard modules.
608 * These functions are only implemented when #APR_POOL_DEBUG is set.
610 * @{
612 #if APR_POOL_DEBUG || defined(DOXYGEN)
614 * Guarantee that a subpool has the same lifetime as the parent.
615 * @param p The parent pool
616 * @param sub The subpool
618 APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub);
621 * Find a pool from something allocated in it.
622 * @param mem The thing allocated in the pool
623 * @return The pool it is allocated in
625 APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem);
628 * Report the number of bytes currently in the pool
629 * @param p The pool to inspect
630 * @param recurse Recurse/include the subpools' sizes
631 * @return The number of bytes
633 APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *p, int recurse);
636 * Lock a pool
637 * @param pool The pool to lock
638 * @param flag The flag
640 APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag);
642 /* @} */
644 #else /* APR_POOL_DEBUG or DOXYGEN */
646 #ifdef apr_pool_join
647 #undef apr_pool_join
648 #endif
649 #define apr_pool_join(a,b)
651 #ifdef apr_pool_lock
652 #undef apr_pool_lock
653 #endif
654 #define apr_pool_lock(pool, lock)
656 #endif /* APR_POOL_DEBUG or DOXYGEN */
658 /** @} */
660 #ifdef __cplusplus
662 #endif
664 #endif /* !APR_POOLS_H */