Apache Portable Runtime
apr_pools.h
Go to the documentation of this file.
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #ifndef APR_POOLS_H
18 #define APR_POOLS_H
19 
20 /**
21  * @file apr_pools.h
22  * @brief APR memory allocation
23  *
24  * Resource allocation routines...
25  *
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()).
29  *
30  * Instead, we maintain pools, and allocate items (both memory and I/O
31  * handlers) from the pools --- currently there are two, one for
32  * per-transaction info, and one for config info. When a transaction is
33  * over, we can delete everything in the per-transaction apr_pool_t without
34  * fear, and without thinking too hard about it either.
35  *
36  * Note that most operations on pools are not thread-safe: a single pool
37  * should only be accessed by a single thread at any given time. The one
38  * exception to this rule is creating a subpool of a given pool: one or more
39  * threads can safely create subpools at the same time that another thread
40  * accesses the parent pool.
41  */
42 
43 #include "apr.h"
44 #include "apr_errno.h"
45 #include "apr_general.h" /* for APR_STRINGIFY */
46 #define APR_WANT_MEMFUNC /**< for no good reason? */
47 #include "apr_want.h"
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /**
54  * @defgroup apr_pools Memory Pool Functions
55  * @ingroup APR
56  * @{
57  */
58 
59 /** The fundamental pool type */
60 typedef struct apr_pool_t apr_pool_t;
61 
62 
63 /**
64  * Declaration helper macro to construct apr_foo_pool_get()s.
65  *
66  * This standardized macro is used by opaque (APR) data types to return
67  * the apr_pool_t that is associated with the data type.
68  *
69  * APR_POOL_DECLARE_ACCESSOR() is used in a header file to declare the
70  * accessor function. A typical usage and result would be:
71  * <pre>
72  * APR_POOL_DECLARE_ACCESSOR(file);
73  * becomes:
74  * APR_DECLARE(apr_pool_t *) apr_file_pool_get(const apr_file_t *thefile);
75  * </pre>
76  * @remark Doxygen unwraps this macro (via doxygen.conf) to provide
77  * actual help for each specific occurrence of apr_foo_pool_get.
78  * @remark the linkage is specified for APR. It would be possible to expand
79  * the macros to support other linkages.
80  */
81 #define APR_POOL_DECLARE_ACCESSOR(type) \
82  APR_DECLARE(apr_pool_t *) apr_##type##_pool_get \
83  (const apr_##type##_t *the##type)
84 
85 /**
86  * Implementation helper macro to provide apr_foo_pool_get()s.
87  *
88  * In the implementation, the APR_POOL_IMPLEMENT_ACCESSOR() is used to
89  * actually define the function. It assumes the field is named "pool".
90  */
91 #define APR_POOL_IMPLEMENT_ACCESSOR(type) \
92  APR_DECLARE(apr_pool_t *) apr_##type##_pool_get \
93  (const apr_##type##_t *the##type) \
94  { return the##type->pool; }
95 
96 
97 /**
98  * Pool debug levels
99  *
100  * <pre>
101  * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
102  * ---------------------------------
103  * | | | | | | | | x | General debug code enabled (useful in
104  * combination with --with-efence).
105  *
106  * | | | | | | | x | | Verbose output on stderr (report
107  * CREATE, CLEAR, DESTROY).
108  *
109  * | | | | x | | | | | Verbose output on stderr (report
110  * PALLOC, PCALLOC).
111  *
112  * | | | | | | x | | | Lifetime checking. On each use of a
113  * pool, check its lifetime. If the pool
114  * is out of scope, abort().
115  * In combination with the verbose flag
116  * above, it will output LIFE in such an
117  * event prior to aborting.
118  *
119  * | | | | | x | | | | Pool owner checking. On each use of a
120  * pool, check if the current thread is the
121  * pool's owner. If not, abort(). In
122  * combination with the verbose flag above,
123  * it will output OWNER in such an event
124  * prior to aborting. Use the debug
125  * function apr_pool_owner_set() to switch
126  * a pool's ownership.
127  *
128  * When no debug level was specified, assume general debug mode.
129  * If level 0 was specified, debugging is switched off.
130  * </pre>
131  */
132 #if defined(APR_POOL_DEBUG)
133 /* If APR_POOL_DEBUG is blank, we get 1; if it is a number, we get -1. */
134 #if (APR_POOL_DEBUG - APR_POOL_DEBUG -1 == 1)
135 #undef APR_POOL_DEBUG
136 #define APR_POOL_DEBUG 1
137 #endif
138 #else
139 #define APR_POOL_DEBUG 0
140 #endif
141 
142 /** the place in the code where the particular function was called */
143 #define APR_POOL__FILE_LINE__ __FILE__ ":" APR_STRINGIFY(__LINE__)
144 
145 
146 
147 /** A function that is called when allocation fails. */
148 typedef int (*apr_abortfunc_t)(int retcode);
149 
150 /*
151  * APR memory structure manipulators (pools, tables, and arrays).
152  */
153 
154 /*
155  * Initialization
156  */
157 
158 /**
159  * Setup all of the internal structures required to use pools
160  * @remark Programs do NOT need to call this directly. APR will call this
161  * automatically from apr_initialize.
162  * @internal
163  */
165 
166 /**
167  * Tear down all of the internal structures required to use pools
168  * @remark Programs do NOT need to call this directly. APR will call this
169  * automatically from apr_terminate.
170  * @internal
171  */
172 APR_DECLARE(void) apr_pool_terminate(void);
173 
174 
175 /*
176  * Pool creation/destruction
177  */
178 
179 #include "apr_allocator.h"
180 
181 /**
182  * Create a new pool.
183  * @param newpool The pool we have just created.
184  * @param parent The parent pool. If this is NULL, the new pool is a root
185  * pool. If it is non-NULL, the new pool will inherit all
186  * of its parent pool's attributes, except the apr_pool_t will
187  * be a sub-pool.
188  * @param abort_fn A function to use if the pool cannot allocate more memory.
189  * @param allocator The allocator to use with the new pool. If NULL the
190  * allocator of the parent pool will be used.
191  * @remark This function is thread-safe, in the sense that multiple threads
192  * can safely create subpools of the same parent pool concurrently.
193  * Similarly, a subpool can be created by one thread at the same
194  * time that another thread accesses the parent pool.
195  */
197  apr_pool_t *parent,
198  apr_abortfunc_t abort_fn,
199  apr_allocator_t *allocator)
200  __attribute__((nonnull(1)));
201 
202 /**
203  * Create a new unmanaged pool.
204  * @param newpool The pool we have just created.
205  * @param abort_fn A function to use if the pool cannot allocate more memory.
206  * @param allocator The allocator to use with the new pool. If NULL a
207  * new allocator will be created with the new pool as owner.
208  * @remark An unmanaged pool is a special pool without a parent; it will
209  * NOT be destroyed upon apr_terminate. It must be explicitly
210  * destroyed by calling apr_pool_destroy, to prevent memory leaks.
211  * Use of this function is discouraged, think twice about whether
212  * you really really need it.
213  * @warning Any child cleanups registered against the new pool, or
214  * against sub-pools thereof, will not be executed during an
215  * invocation of apr_proc_create(), so resources created in an
216  * "unmanaged" pool hierarchy will leak to child processes.
217  */
219  apr_abortfunc_t abort_fn,
220  apr_allocator_t *allocator)
221  __attribute__((nonnull(1)));
222 
223 /**
224  * Debug version of apr_pool_create_ex.
225  * @param newpool @see apr_pool_create.
226  * @param parent @see apr_pool_create.
227  * @param abort_fn @see apr_pool_create.
228  * @param allocator @see apr_pool_create.
229  * @param file_line Where the function is called from.
230  * This is usually APR_POOL__FILE_LINE__.
231  * @remark Only available when APR_POOL_DEBUG is defined.
232  * Call this directly if you have your apr_pool_create_ex
233  * calls in a wrapper function and wish to override
234  * the file_line argument to reflect the caller of
235  * your wrapper function. If you do not have
236  * apr_pool_create_ex in a wrapper, trust the macro
237  * and don't call apr_pool_create_ex_debug directly.
238  */
240  apr_pool_t *parent,
241  apr_abortfunc_t abort_fn,
242  apr_allocator_t *allocator,
243  const char *file_line)
244  __attribute__((nonnull(1)));
245 
246 #if APR_POOL_DEBUG
247 #define apr_pool_create_ex(newpool, parent, abort_fn, allocator) \
248  apr_pool_create_ex_debug(newpool, parent, abort_fn, allocator, \
249  APR_POOL__FILE_LINE__)
250 #endif
251 
252  /**
253  * Debug version of apr_pool_create_unmanaged_ex.
254  * @param newpool @see apr_pool_create_unmanaged.
255  * @param abort_fn @see apr_pool_create_unmanaged.
256  * @param allocator @see apr_pool_create_unmanaged.
257  * @param file_line Where the function is called from.
258  * This is usually APR_POOL__FILE_LINE__.
259  * @remark Only available when APR_POOL_DEBUG is defined.
260  * Call this directly if you have your apr_pool_create_unmanaged_ex
261  * calls in a wrapper function and wish to override
262  * the file_line argument to reflect the caller of
263  * your wrapper function. If you do not have
264  * apr_pool_create_unmanaged_ex in a wrapper, trust the macro
265  * and don't call apr_pool_create_unmanaged_ex_debug directly.
266  */
268  apr_abortfunc_t abort_fn,
269  apr_allocator_t *allocator,
270  const char *file_line)
271  __attribute__((nonnull(1)));
272 
273 #if APR_POOL_DEBUG
274 #define apr_pool_create_unmanaged_ex(newpool, abort_fn, allocator) \
275  apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
276  APR_POOL__FILE_LINE__)
277 
278 #endif
279 
280 /**
281  * Create a new pool.
282  * @param newpool The pool we have just created.
283  * @param parent The parent pool. If this is NULL, the new pool is a root
284  * pool. If it is non-NULL, the new pool will inherit all
285  * of its parent pool's attributes, except the apr_pool_t will
286  * be a sub-pool.
287  * @remark This function is thread-safe, in the sense that multiple threads
288  * can safely create subpools of the same parent pool concurrently.
289  * Similarly, a subpool can be created by one thread at the same
290  * time that another thread accesses the parent pool.
291  */
292 #if defined(DOXYGEN)
294  apr_pool_t *parent);
295 #else
296 #if APR_POOL_DEBUG
297 #define apr_pool_create(newpool, parent) \
298  apr_pool_create_ex_debug(newpool, parent, NULL, NULL, \
299  APR_POOL__FILE_LINE__)
300 #else
301 #define apr_pool_create(newpool, parent) \
302  apr_pool_create_ex(newpool, parent, NULL, NULL)
303 #endif
304 #endif
305 
306 /**
307  * Create a new unmanaged pool.
308  * @param newpool The pool we have just created.
309  */
310 #if defined(DOXYGEN)
312 #else
313 #if APR_POOL_DEBUG
314 #define apr_pool_create_unmanaged(newpool) \
315  apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
316  APR_POOL__FILE_LINE__)
317 #else
318 #define apr_pool_create_unmanaged(newpool) \
319  apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
320 #endif
321 #endif
322 
323 /**
324  * Find the pool's allocator
325  * @param pool The pool to get the allocator from.
326  */
328  __attribute__((nonnull(1)));
329 
330 /**
331  * Clear all memory in the pool and run all the cleanups. This also destroys all
332  * subpools.
333  * @param p The pool to clear
334  * @remark This does not actually free the memory, it just allows the pool
335  * to re-use this memory for the next allocation.
336  * @see apr_pool_destroy()
337  */
338 APR_DECLARE(void) apr_pool_clear(apr_pool_t *p) __attribute__((nonnull(1)));
339 
340 /**
341  * Debug version of apr_pool_clear.
342  * @param p See: apr_pool_clear.
343  * @param file_line Where the function is called from.
344  * This is usually APR_POOL__FILE_LINE__.
345  * @remark Only available when APR_POOL_DEBUG is defined.
346  * Call this directly if you have your apr_pool_clear
347  * calls in a wrapper function and wish to override
348  * the file_line argument to reflect the caller of
349  * your wrapper function. If you do not have
350  * apr_pool_clear in a wrapper, trust the macro
351  * and don't call apr_pool_destroy_clear directly.
352  */
354  const char *file_line)
355  __attribute__((nonnull(1)));
356 
357 #if APR_POOL_DEBUG
358 #define apr_pool_clear(p) \
359  apr_pool_clear_debug(p, APR_POOL__FILE_LINE__)
360 #endif
361 
362 /**
363  * Destroy the pool. This takes similar action as apr_pool_clear() and then
364  * frees all the memory.
365  * @param p The pool to destroy
366  * @remark This will actually free the memory
367  */
368 APR_DECLARE(void) apr_pool_destroy(apr_pool_t *p) __attribute__((nonnull(1)));
369 
370 /**
371  * Debug version of apr_pool_destroy.
372  * @param p See: apr_pool_destroy.
373  * @param file_line Where the function is called from.
374  * This is usually APR_POOL__FILE_LINE__.
375  * @remark Only available when APR_POOL_DEBUG is defined.
376  * Call this directly if you have your apr_pool_destroy
377  * calls in a wrapper function and wish to override
378  * the file_line argument to reflect the caller of
379  * your wrapper function. If you do not have
380  * apr_pool_destroy in a wrapper, trust the macro
381  * and don't call apr_pool_destroy_debug directly.
382  */
384  const char *file_line)
385  __attribute__((nonnull(1)));
386 
387 #if APR_POOL_DEBUG
388 #define apr_pool_destroy(p) \
389  apr_pool_destroy_debug(p, APR_POOL__FILE_LINE__)
390 #endif
391 
392 
393 /*
394  * Memory allocation
395  */
396 
397 /**
398  * Allocate a block of memory from a pool
399  * @param p The pool to allocate from
400  * @param size The amount of memory to allocate
401  * @return The allocated memory
402  */
403 APR_DECLARE(void *) apr_palloc(apr_pool_t *p, apr_size_t size)
404 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
405  __attribute__((alloc_size(2)))
406 #endif
407  __attribute__((nonnull(1)));
408 
409 /**
410  * Debug version of apr_palloc
411  * @param p See: apr_palloc
412  * @param size See: apr_palloc
413  * @param file_line Where the function is called from.
414  * This is usually APR_POOL__FILE_LINE__.
415  * @return See: apr_palloc
416  */
417 APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *p, apr_size_t size,
418  const char *file_line)
419 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
420  __attribute__((alloc_size(2)))
421 #endif
422  __attribute__((nonnull(1)));
423 
424 #if APR_POOL_DEBUG
425 #define apr_palloc(p, size) \
426  apr_palloc_debug(p, size, APR_POOL__FILE_LINE__)
427 #endif
428 
429 /**
430  * Allocate a block of memory from a pool and set all of the memory to 0
431  * @param p The pool to allocate from
432  * @param size The amount of memory to allocate
433  * @return The allocated memory
434  */
435 #if defined(DOXYGEN)
436 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *p, apr_size_t size);
437 #elif !APR_POOL_DEBUG
438 #define apr_pcalloc(p, size) memset(apr_palloc(p, size), 0, size)
439 #endif
440 
441 /**
442  * Debug version of apr_pcalloc
443  * @param p See: apr_pcalloc
444  * @param size See: apr_pcalloc
445  * @param file_line Where the function is called from.
446  * This is usually APR_POOL__FILE_LINE__.
447  * @return See: apr_pcalloc
448  */
449 APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *p, apr_size_t size,
450  const char *file_line)
451  __attribute__((nonnull(1)));
452 
453 #if APR_POOL_DEBUG
454 #define apr_pcalloc(p, size) \
455  apr_pcalloc_debug(p, size, APR_POOL__FILE_LINE__)
456 #endif
457 
458 
459 /*
460  * Pool Properties
461  */
462 
463 /**
464  * Set the function to be called when an allocation failure occurs.
465  * @remark If the program wants APR to exit on a memory allocation error,
466  * then this function can be called to set the callback to use (for
467  * performing cleanup and then exiting). If this function is not called,
468  * then APR will return an error and expect the calling program to
469  * deal with the error accordingly.
470  */
472  apr_pool_t *pool)
473  __attribute__((nonnull(2)));
474 
475 /**
476  * Get the abort function associated with the specified pool.
477  * @param pool The pool for retrieving the abort function.
478  * @return The abort function for the given pool.
479  */
481  __attribute__((nonnull(1)));
482 
483 /**
484  * Get the parent pool of the specified pool.
485  * @param pool The pool for retrieving the parent pool.
486  * @return The parent of the given pool.
487  */
489  __attribute__((nonnull(1)));
490 
491 /**
492  * Determine if pool a is an ancestor of pool b.
493  * @param a The pool to search
494  * @param b The pool to search for
495  * @return True if a is an ancestor of b, NULL is considered an ancestor
496  * of all pools.
497  * @remark if compiled with APR_POOL_DEBUG, this function will also
498  * return true if A is a pool which has been guaranteed by the caller
499  * (using apr_pool_join) to have a lifetime at least as long as some
500  * ancestor of pool B.
501  */
503 
504 /**
505  * Tag a pool (give it a name)
506  * @param pool The pool to tag
507  * @param tag The tag
508  */
509 APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag)
510  __attribute__((nonnull(1)));
511 
512 
513 /*
514  * User data management
515  */
516 
517 /**
518  * Set the data associated with the current pool
519  * @param data The user data associated with the pool.
520  * @param key The key to use for association
521  * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
522  * @param pool The current pool
523  * @warning The data to be attached to the pool should have a life span
524  * at least as long as the pool it is being attached to.
525  *
526  * Users of APR must take EXTREME care when choosing a key to
527  * use for their data. It is possible to accidentally overwrite
528  * data by choosing a key that another part of the program is using.
529  * Therefore it is advised that steps are taken to ensure that unique
530  * keys are used for all of the userdata objects in a particular pool
531  * (the same key in two different pools or a pool and one of its
532  * subpools is okay) at all times. Careful namespace prefixing of
533  * key names is a typical way to help ensure this uniqueness.
534  *
535  */
537  const char *key,
538  apr_status_t (*cleanup)(void *),
539  apr_pool_t *pool)
540  __attribute__((nonnull(2,4)));
541 
542 /**
543  * Set the data associated with the current pool
544  * @param data The user data associated with the pool.
545  * @param key The key to use for association
546  * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
547  * @param pool The current pool
548  * @note same as apr_pool_userdata_set(), except that this version doesn't
549  * make a copy of the key (this function is useful, for example, when
550  * the key is a string literal)
551  * @warning This should NOT be used if the key could change addresses by
552  * any means between the apr_pool_userdata_setn() call and a
553  * subsequent apr_pool_userdata_get() on that key, such as if a
554  * static string is used as a userdata key in a DSO and the DSO could
555  * be unloaded and reloaded between the _setn() and the _get(). You
556  * MUST use apr_pool_userdata_set() in such cases.
557  * @warning More generally, the key and the data to be attached to the
558  * pool should have a life span at least as long as the pool itself.
559  *
560  */
562  const void *data, const char *key,
563  apr_status_t (*cleanup)(void *),
564  apr_pool_t *pool)
565  __attribute__((nonnull(2,4)));
566 
567 /**
568  * Return the data associated with the current pool.
569  * @param data The user data associated with the pool.
570  * @param key The key for the data to retrieve
571  * @param pool The current pool.
572  */
573 APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
574  apr_pool_t *pool)
575  __attribute__((nonnull(1,2,3)));
576 
577 
578 /**
579  * @defgroup PoolCleanup Pool Cleanup Functions
580  *
581  * Cleanups are performed in the reverse order they were registered. That is:
582  * Last In, First Out. A cleanup function can safely allocate memory from
583  * the pool that is being cleaned up. It can also safely register additional
584  * cleanups which will be run LIFO, directly after the current cleanup
585  * terminates. Cleanups have to take caution in calling functions that
586  * create subpools. Subpools, created during cleanup will NOT automatically
587  * be cleaned up. In other words, cleanups are to clean up after themselves.
588  *
589  * @{
590  */
591 
592 /**
593  * Register a function to be called when a pool is cleared or destroyed
594  * @param p The pool to register the cleanup with
595  * @param data The data to pass to the cleanup function.
596  * @param plain_cleanup The function to call when the pool is cleared
597  * or destroyed
598  * @param child_cleanup The function to call when a child process is about
599  * to exec - this function is called in the child, obviously!
600  */
602  apr_pool_t *p, const void *data,
603  apr_status_t (*plain_cleanup)(void *),
604  apr_status_t (*child_cleanup)(void *))
605  __attribute__((nonnull(3,4)));
606 
607 /**
608  * Register a function to be called when a pool is cleared or destroyed.
609  *
610  * Unlike apr_pool_cleanup_register which registers a cleanup
611  * that is called AFTER all subpools are destroyed, this function registers
612  * a function that will be called before any of the subpools are destroyed.
613  *
614  * @param p The pool to register the cleanup with
615  * @param data The data to pass to the cleanup function.
616  * @param plain_cleanup The function to call when the pool is cleared
617  * or destroyed
618  */
620  apr_pool_t *p, const void *data,
621  apr_status_t (*plain_cleanup)(void *))
622  __attribute__((nonnull(3)));
623 
624 /**
625  * Remove a previously registered cleanup function.
626  *
627  * The cleanup most recently registered with @a p having the same values of
628  * @a data and @a cleanup will be removed.
629  *
630  * @param p The pool to remove the cleanup from
631  * @param data The data of the registered cleanup
632  * @param cleanup The function to remove from cleanup
633  * @remarks For some strange reason only the plain_cleanup is handled by this
634  * function
635  */
636 APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
637  apr_status_t (*cleanup)(void *))
638  __attribute__((nonnull(3)));
639 
640 /**
641  * Replace the child cleanup function of a previously registered cleanup.
642  *
643  * The cleanup most recently registered with @a p having the same values of
644  * @a data and @a plain_cleanup will have the registered child cleanup
645  * function replaced with @a child_cleanup.
646  *
647  * @param p The pool of the registered cleanup
648  * @param data The data of the registered cleanup
649  * @param plain_cleanup The plain cleanup function of the registered cleanup
650  * @param child_cleanup The function to register as the child cleanup
651  */
653  apr_pool_t *p, const void *data,
654  apr_status_t (*plain_cleanup)(void *),
655  apr_status_t (*child_cleanup)(void *))
656  __attribute__((nonnull(3,4)));
657 
658 /**
659  * Run the specified cleanup function immediately and unregister it.
660  *
661  * The cleanup most recently registered with @a p having the same values of
662  * @a data and @a cleanup will be removed and @a cleanup will be called
663  * with @a data as the argument.
664  *
665  * @param p The pool to remove the cleanup from
666  * @param data The data to remove from cleanup
667  * @param cleanup The function to remove from cleanup
668  */
670  apr_status_t (*cleanup)(void *))
671  __attribute__((nonnull(3)));
672 
673 /**
674  * An empty cleanup function.
675  *
676  * Passed to apr_pool_cleanup_register() when no cleanup is required.
677  *
678  * @param data The data to cleanup, will not be used by this function.
679  */
681 
682 /**
683  * Run all registered child cleanups, in preparation for an exec()
684  * call in a forked child -- close files, etc., but *don't* flush I/O
685  * buffers, *don't* wait for subprocesses, and *don't* free any
686  * memory.
687  */
689 
690 /** @} */
691 
692 /**
693  * @defgroup PoolDebug Pool Debugging functions.
694  *
695  * pools have nested lifetimes -- sub_pools are destroyed when the
696  * parent pool is cleared. We allow certain liberties with operations
697  * on things such as tables (and on other structures in a more general
698  * sense) where we allow the caller to insert values into a table which
699  * were not allocated from the table's pool. The table's data will
700  * remain valid as long as all the pools from which its values are
701  * allocated remain valid.
702  *
703  * For example, if B is a sub pool of A, and you build a table T in
704  * pool B, then it's safe to insert data allocated in A or B into T
705  * (because B lives at most as long as A does, and T is destroyed when
706  * B is cleared/destroyed). On the other hand, if S is a table in
707  * pool A, it is safe to insert data allocated in A into S, but it
708  * is *not safe* to insert data allocated from B into S... because
709  * B can be cleared/destroyed before A is (which would leave dangling
710  * pointers in T's data structures).
711  *
712  * In general we say that it is safe to insert data into a table T
713  * if the data is allocated in any ancestor of T's pool. This is the
714  * basis on which the APR_POOL_DEBUG code works -- it tests these ancestor
715  * relationships for all data inserted into tables. APR_POOL_DEBUG also
716  * provides tools (apr_pool_find, and apr_pool_is_ancestor) for other
717  * folks to implement similar restrictions for their own data
718  * structures.
719  *
720  * However, sometimes this ancestor requirement is inconvenient --
721  * sometimes it's necessary to create a sub pool where the sub pool is
722  * guaranteed to have the same lifetime as the parent pool. This is a
723  * guarantee implemented by the *caller*, not by the pool code. That
724  * is, the caller guarantees they won't destroy the sub pool
725  * individually prior to destroying the parent pool.
726  *
727  * In this case the caller must call apr_pool_join() to indicate this
728  * guarantee to the APR_POOL_DEBUG code.
729  *
730  * These functions are only implemented when #APR_POOL_DEBUG is set.
731  *
732  * @{
733  */
734 #if APR_POOL_DEBUG || defined(DOXYGEN)
735 /**
736  * Guarantee that a subpool has the same lifetime as the parent.
737  * @param p The parent pool
738  * @param sub The subpool
739  */
741  __attribute__((nonnull(2)));
742 
743 /**
744  * Guarantee that a pool is only used by the current thread.
745  * This should be used when a pool is created by a different thread than
746  * the thread it is using, or if there is some locking in use to ensure
747  * that only one thread uses the pool at the same time.
748  *
749  * @param pool The pool
750  * @param flags Flags, currently unused
751  */
752 APR_DECLARE(void) apr_pool_owner_set(apr_pool_t *pool, apr_uint32_t flags);
753 
754 /**
755  * Find a pool from something allocated in it.
756  * @param mem The thing allocated in the pool
757  * @return The pool it is allocated in
758  */
759 APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem);
760 
761 /**
762  * Report the number of bytes currently in the pool
763  * @param p The pool to inspect
764  * @param recurse Recurse/include the subpools' sizes
765  * @return The number of bytes
766  */
767 APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *p, int recurse)
768  __attribute__((nonnull(1)));
769 
770 /**
771  * Lock a pool
772  * @param pool The pool to lock
773  * @param flag The flag
774  */
775 APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag);
776 
777 /* @} */
778 
779 #else /* APR_POOL_DEBUG or DOXYGEN */
780 
781 #ifdef apr_pool_join
782 #undef apr_pool_join
783 #endif
784 #define apr_pool_join(a,b)
785 
786 #ifdef apr_pool_owner_set
787 #undef apr_pool_owner_set
788 #endif
789 #define apr_pool_owner_set(a,b)
790 
791 #ifdef apr_pool_lock
792 #undef apr_pool_lock
793 #endif
794 #define apr_pool_lock(pool, lock)
795 
796 #endif /* APR_POOL_DEBUG or DOXYGEN */
797 
798 /** @} */
799 
800 #ifdef __cplusplus
801 }
802 #endif
803 
804 #endif /* !APR_POOLS_H */
void * apr_pcalloc_debug(apr_pool_t *p, apr_size_t size, const char *file_line)
void apr_pool_destroy(apr_pool_t *p)
apr_pool_t * apr_pool_parent_get(apr_pool_t *pool)
void apr_pool_cleanup_for_exec(void)
void apr_pool_owner_set(apr_pool_t *pool, apr_uint32_t flags)
void apr_pool_tag(apr_pool_t *pool, const char *tag)
apr_status_t apr_pool_userdata_set(const void *data, const char *key, apr_status_t(*cleanup)(void *), apr_pool_t *pool)
void apr_pool_terminate(void)
apr_status_t apr_pool_create_ex_debug(apr_pool_t **newpool, apr_pool_t *parent, apr_abortfunc_t abort_fn, apr_allocator_t *allocator, const char *file_line)
void apr_pool_lock(apr_pool_t *pool, int flag)
void * apr_pcalloc(apr_pool_t *p, apr_size_t size)
struct apr_allocator_t apr_allocator_t
Definition: apr_allocator.h:41
APR Standard Headers Support.
apr_size_t apr_pool_num_bytes(apr_pool_t *p, int recurse)
APR Miscellaneous library routines.
int(* apr_abortfunc_t)(int retcode)
Definition: apr_pools.h:148
apr_status_t apr_pool_cleanup_run(apr_pool_t *p, void *data, apr_status_t(*cleanup)(void *))
void apr_pool_cleanup_kill(apr_pool_t *p, const void *data, apr_status_t(*cleanup)(void *))
void apr_pool_pre_cleanup_register(apr_pool_t *p, const void *data, apr_status_t(*plain_cleanup)(void *))
void * apr_palloc(apr_pool_t *p, apr_size_t size)
apr_status_t apr_pool_create_ex(apr_pool_t **newpool, apr_pool_t *parent, apr_abortfunc_t abort_fn, apr_allocator_t *allocator)
APR Error Codes.
#define APR_DECLARE(type)
Definition: apr.h:500
APR Platform Definitions.
void apr_pool_clear(apr_pool_t *p)
apr_status_t apr_pool_userdata_setn(const void *data, const char *key, apr_status_t(*cleanup)(void *), apr_pool_t *pool)
void apr_pool_destroy_debug(apr_pool_t *p, const char *file_line)
void apr_pool_abort_set(apr_abortfunc_t abortfunc, apr_pool_t *pool)
void apr_pool_clear_debug(apr_pool_t *p, const char *file_line)
apr_status_t apr_pool_create_unmanaged_ex(apr_pool_t **newpool, apr_abortfunc_t abort_fn, apr_allocator_t *allocator)
void apr_pool_child_cleanup_set(apr_pool_t *p, const void *data, apr_status_t(*plain_cleanup)(void *), apr_status_t(*child_cleanup)(void *))
apr_pool_t * apr_pool_find(const void *mem)
apr_status_t apr_pool_create_unmanaged(apr_pool_t **newpool)
apr_allocator_t * apr_pool_allocator_get(apr_pool_t *pool)
apr_status_t apr_pool_cleanup_null(void *data)
#define APR_DECLARE_NONSTD(type)
Definition: apr.h:513
struct apr_pool_t apr_pool_t
Definition: apr_pools.h:60
void apr_pool_cleanup_register(apr_pool_t *p, const void *data, apr_status_t(*plain_cleanup)(void *), apr_status_t(*child_cleanup)(void *))
int apr_status_t
Definition: apr_errno.h:44
apr_status_t apr_pool_userdata_get(void **data, const char *key, apr_pool_t *pool)
apr_status_t apr_pool_initialize(void)
void apr_pool_join(apr_pool_t *p, apr_pool_t *sub)
apr_abortfunc_t apr_pool_abort_get(apr_pool_t *pool)
int apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b)
void * apr_palloc_debug(apr_pool_t *p, apr_size_t size, const char *file_line)
apr_status_t apr_pool_create(apr_pool_t **newpool, apr_pool_t *parent)
apr_status_t apr_pool_create_unmanaged_ex_debug(apr_pool_t **newpool, apr_abortfunc_t abort_fn, apr_allocator_t *allocator, const char *file_line)