apr_pools.h 29 KB

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