apr_pools.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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(const apr_file_t *thefile);
  67. * </pre>
  68. * @remark Doxygen unwraps this macro (via doxygen.conf) to provide
  69. * actual help for each specific occurrence 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. * pool's 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 pool's 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. __attribute__((nonnull(1)));
  178. /**
  179. * Create a new pool.
  180. * @deprecated @see apr_pool_create_unmanaged_ex.
  181. */
  182. APR_DECLARE(apr_status_t) apr_pool_create_core_ex(apr_pool_t **newpool,
  183. apr_abortfunc_t abort_fn,
  184. apr_allocator_t *allocator);
  185. /**
  186. * Create a new unmanaged pool.
  187. * @param newpool The pool we have just created.
  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 a
  190. * new allocator will be created with the new pool as owner.
  191. * @remark An unmanaged pool is a special pool without a parent; it will
  192. * NOT be destroyed upon apr_terminate. It must be explicitly
  193. * destroyed by calling apr_pool_destroy, to prevent memory leaks.
  194. * Use of this function is discouraged, think twice about whether
  195. * you really really need it.
  196. * @warning Any child cleanups registered against the new pool, or
  197. * against sub-pools thereof, will not be executed during an
  198. * invocation of apr_proc_create(), so resources created in an
  199. * "unmanaged" pool hierarchy will leak to child processes.
  200. */
  201. APR_DECLARE(apr_status_t) apr_pool_create_unmanaged_ex(apr_pool_t **newpool,
  202. apr_abortfunc_t abort_fn,
  203. apr_allocator_t *allocator)
  204. __attribute__((nonnull(1)));
  205. /**
  206. * Debug version of apr_pool_create_ex.
  207. * @param newpool @see apr_pool_create.
  208. * @param parent @see apr_pool_create.
  209. * @param abort_fn @see apr_pool_create.
  210. * @param allocator @see apr_pool_create.
  211. * @param file_line Where the function is called from.
  212. * This is usually APR_POOL__FILE_LINE__.
  213. * @remark Only available when APR_POOL_DEBUG is defined.
  214. * Call this directly if you have your apr_pool_create_ex
  215. * calls in a wrapper function and wish to override
  216. * the file_line argument to reflect the caller of
  217. * your wrapper function. If you do not have
  218. * apr_pool_create_ex in a wrapper, trust the macro
  219. * and don't call apr_pool_create_ex_debug directly.
  220. */
  221. APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
  222. apr_pool_t *parent,
  223. apr_abortfunc_t abort_fn,
  224. apr_allocator_t *allocator,
  225. const char *file_line)
  226. __attribute__((nonnull(1)));
  227. #if APR_POOL_DEBUG
  228. #define apr_pool_create_ex(newpool, parent, abort_fn, allocator) \
  229. apr_pool_create_ex_debug(newpool, parent, abort_fn, allocator, \
  230. APR_POOL__FILE_LINE__)
  231. #endif
  232. /**
  233. * Debug version of apr_pool_create_core_ex.
  234. * @deprecated @see apr_pool_create_unmanaged_ex_debug.
  235. */
  236. APR_DECLARE(apr_status_t) apr_pool_create_core_ex_debug(apr_pool_t **newpool,
  237. apr_abortfunc_t abort_fn,
  238. apr_allocator_t *allocator,
  239. const char *file_line);
  240. /**
  241. * Debug version of apr_pool_create_unmanaged_ex.
  242. * @param newpool @see apr_pool_create_unmanaged.
  243. * @param abort_fn @see apr_pool_create_unmanaged.
  244. * @param allocator @see apr_pool_create_unmanaged.
  245. * @param file_line Where the function is called from.
  246. * This is usually APR_POOL__FILE_LINE__.
  247. * @remark Only available when APR_POOL_DEBUG is defined.
  248. * Call this directly if you have your apr_pool_create_unmanaged_ex
  249. * calls in a wrapper function and wish to override
  250. * the file_line argument to reflect the caller of
  251. * your wrapper function. If you do not have
  252. * apr_pool_create_core_ex in a wrapper, trust the macro
  253. * and don't call apr_pool_create_core_ex_debug directly.
  254. */
  255. APR_DECLARE(apr_status_t) apr_pool_create_unmanaged_ex_debug(apr_pool_t **newpool,
  256. apr_abortfunc_t abort_fn,
  257. apr_allocator_t *allocator,
  258. const char *file_line)
  259. __attribute__((nonnull(1)));
  260. #if APR_POOL_DEBUG
  261. #define apr_pool_create_core_ex(newpool, abort_fn, allocator) \
  262. apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
  263. APR_POOL__FILE_LINE__)
  264. #define apr_pool_create_unmanaged_ex(newpool, abort_fn, allocator) \
  265. apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
  266. APR_POOL__FILE_LINE__)
  267. #endif
  268. /**
  269. * Create a new pool.
  270. * @param newpool The pool we have just created.
  271. * @param parent The parent pool. If this is NULL, the new pool is a root
  272. * pool. If it is non-NULL, the new pool will inherit all
  273. * of its parent pool's attributes, except the apr_pool_t will
  274. * be a sub-pool.
  275. * @remark This function is thread-safe, in the sense that multiple threads
  276. * can safely create subpools of the same parent pool concurrently.
  277. * Similarly, a subpool can be created by one thread at the same
  278. * time that another thread accesses the parent pool.
  279. */
  280. #if defined(DOXYGEN)
  281. APR_DECLARE(apr_status_t) apr_pool_create(apr_pool_t **newpool,
  282. apr_pool_t *parent);
  283. #else
  284. #if APR_POOL_DEBUG
  285. #define apr_pool_create(newpool, parent) \
  286. apr_pool_create_ex_debug(newpool, parent, NULL, NULL, \
  287. APR_POOL__FILE_LINE__)
  288. #else
  289. #define apr_pool_create(newpool, parent) \
  290. apr_pool_create_ex(newpool, parent, NULL, NULL)
  291. #endif
  292. #endif
  293. /**
  294. * Create a new unmanaged pool.
  295. * @param newpool The pool we have just created.
  296. */
  297. #if defined(DOXYGEN)
  298. APR_DECLARE(apr_status_t) apr_pool_create_core(apr_pool_t **newpool);
  299. APR_DECLARE(apr_status_t) apr_pool_create_unmanaged(apr_pool_t **newpool);
  300. #else
  301. #if APR_POOL_DEBUG
  302. #define apr_pool_create_core(newpool) \
  303. apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
  304. APR_POOL__FILE_LINE__)
  305. #define apr_pool_create_unmanaged(newpool) \
  306. apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
  307. APR_POOL__FILE_LINE__)
  308. #else
  309. #define apr_pool_create_core(newpool) \
  310. apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
  311. #define apr_pool_create_unmanaged(newpool) \
  312. apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
  313. #endif
  314. #endif
  315. /**
  316. * Find the pool's allocator
  317. * @param pool The pool to get the allocator from.
  318. */
  319. APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool)
  320. __attribute__((nonnull(1)));
  321. /**
  322. * Clear all memory in the pool and run all the cleanups. This also destroys all
  323. * subpools.
  324. * @param p The pool to clear
  325. * @remark This does not actually free the memory, it just allows the pool
  326. * to re-use this memory for the next allocation.
  327. * @see apr_pool_destroy()
  328. */
  329. APR_DECLARE(void) apr_pool_clear(apr_pool_t *p) __attribute__((nonnull(1)));
  330. /**
  331. * Debug version of apr_pool_clear.
  332. * @param p See: apr_pool_clear.
  333. * @param file_line Where the function is called from.
  334. * This is usually APR_POOL__FILE_LINE__.
  335. * @remark Only available when APR_POOL_DEBUG is defined.
  336. * Call this directly if you have your apr_pool_clear
  337. * calls in a wrapper function and wish to override
  338. * the file_line argument to reflect the caller of
  339. * your wrapper function. If you do not have
  340. * apr_pool_clear in a wrapper, trust the macro
  341. * and don't call apr_pool_destroy_clear directly.
  342. */
  343. APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *p,
  344. const char *file_line)
  345. __attribute__((nonnull(1)));
  346. #if APR_POOL_DEBUG
  347. #define apr_pool_clear(p) \
  348. apr_pool_clear_debug(p, APR_POOL__FILE_LINE__)
  349. #endif
  350. /**
  351. * Destroy the pool. This takes similar action as apr_pool_clear() and then
  352. * frees all the memory.
  353. * @param p The pool to destroy
  354. * @remark This will actually free the memory
  355. */
  356. APR_DECLARE(void) apr_pool_destroy(apr_pool_t *p) __attribute__((nonnull(1)));
  357. /**
  358. * Debug version of apr_pool_destroy.
  359. * @param p See: apr_pool_destroy.
  360. * @param file_line Where the function is called from.
  361. * This is usually APR_POOL__FILE_LINE__.
  362. * @remark Only available when APR_POOL_DEBUG is defined.
  363. * Call this directly if you have your apr_pool_destroy
  364. * calls in a wrapper function and wish to override
  365. * the file_line argument to reflect the caller of
  366. * your wrapper function. If you do not have
  367. * apr_pool_destroy in a wrapper, trust the macro
  368. * and don't call apr_pool_destroy_debug directly.
  369. */
  370. APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *p,
  371. const char *file_line)
  372. __attribute__((nonnull(1)));
  373. #if APR_POOL_DEBUG
  374. #define apr_pool_destroy(p) \
  375. apr_pool_destroy_debug(p, APR_POOL__FILE_LINE__)
  376. #endif
  377. /*
  378. * Memory allocation
  379. */
  380. /**
  381. * Allocate a block of memory from a pool
  382. * @param p The pool to allocate from
  383. * @param size The amount of memory to allocate
  384. * @return The allocated memory
  385. */
  386. APR_DECLARE(void *) apr_palloc(apr_pool_t *p, apr_size_t size)
  387. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  388. __attribute__((alloc_size(2)))
  389. #endif
  390. __attribute__((nonnull(1)));
  391. /**
  392. * Debug version of apr_palloc
  393. * @param p See: apr_palloc
  394. * @param size See: apr_palloc
  395. * @param file_line Where the function is called from.
  396. * This is usually APR_POOL__FILE_LINE__.
  397. * @return See: apr_palloc
  398. */
  399. APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *p, apr_size_t size,
  400. const char *file_line)
  401. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
  402. __attribute__((alloc_size(2)))
  403. #endif
  404. __attribute__((nonnull(1)));
  405. #if APR_POOL_DEBUG
  406. #define apr_palloc(p, size) \
  407. apr_palloc_debug(p, size, APR_POOL__FILE_LINE__)
  408. #endif
  409. /**
  410. * Allocate a block of memory from a pool and set all of the memory to 0
  411. * @param p The pool to allocate from
  412. * @param size The amount of memory to allocate
  413. * @return The allocated memory
  414. */
  415. #if defined(DOXYGEN)
  416. APR_DECLARE(void *) apr_pcalloc(apr_pool_t *p, apr_size_t size);
  417. #elif !APR_POOL_DEBUG
  418. #define apr_pcalloc(p, size) memset(apr_palloc(p, size), 0, size)
  419. #endif
  420. /**
  421. * Debug version of apr_pcalloc
  422. * @param p See: apr_pcalloc
  423. * @param size See: apr_pcalloc
  424. * @param file_line Where the function is called from.
  425. * This is usually APR_POOL__FILE_LINE__.
  426. * @return See: apr_pcalloc
  427. */
  428. APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *p, apr_size_t size,
  429. const char *file_line)
  430. __attribute__((nonnull(1)));
  431. #if APR_POOL_DEBUG
  432. #define apr_pcalloc(p, size) \
  433. apr_pcalloc_debug(p, size, APR_POOL__FILE_LINE__)
  434. #endif
  435. /*
  436. * Pool Properties
  437. */
  438. /**
  439. * Set the function to be called when an allocation failure occurs.
  440. * @remark If the program wants APR to exit on a memory allocation error,
  441. * then this function can be called to set the callback to use (for
  442. * performing cleanup and then exiting). If this function is not called,
  443. * then APR will return an error and expect the calling program to
  444. * deal with the error accordingly.
  445. */
  446. APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abortfunc,
  447. apr_pool_t *pool)
  448. __attribute__((nonnull(2)));
  449. /**
  450. * Get the abort function associated with the specified pool.
  451. * @param pool The pool for retrieving the abort function.
  452. * @return The abort function for the given pool.
  453. */
  454. APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool)
  455. __attribute__((nonnull(1)));
  456. /**
  457. * Get the parent pool of the specified pool.
  458. * @param pool The pool for retrieving the parent pool.
  459. * @return The parent of the given pool.
  460. */
  461. APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool)
  462. __attribute__((nonnull(1)));
  463. /**
  464. * Determine if pool a is an ancestor of pool b.
  465. * @param a The pool to search
  466. * @param b The pool to search for
  467. * @return True if a is an ancestor of b, NULL is considered an ancestor
  468. * of all pools.
  469. * @remark if compiled with APR_POOL_DEBUG, this function will also
  470. * return true if A is a pool which has been guaranteed by the caller
  471. * (using apr_pool_join) to have a lifetime at least as long as some
  472. * ancestor of pool B.
  473. */
  474. APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b);
  475. /**
  476. * Tag a pool (give it a name)
  477. * @param pool The pool to tag
  478. * @param tag The tag
  479. */
  480. APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag)
  481. __attribute__((nonnull(1)));
  482. /*
  483. * User data management
  484. */
  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. * @warning The data to be attached to the pool should have a life span
  492. * at least as long as the pool it is being attached to.
  493. *
  494. * Users of APR must take EXTREME care when choosing a key to
  495. * use for their data. It is possible to accidentally overwrite
  496. * data by choosing a key that another part of the program is using.
  497. * Therefore it is advised that steps are taken to ensure that unique
  498. * keys are used for all of the userdata objects in a particular pool
  499. * (the same key in two different pools or a pool and one of its
  500. * subpools is okay) at all times. Careful namespace prefixing of
  501. * key names is a typical way to help ensure this uniqueness.
  502. *
  503. */
  504. APR_DECLARE(apr_status_t) apr_pool_userdata_set(const void *data,
  505. const char *key,
  506. apr_status_t (*cleanup)(void *),
  507. apr_pool_t *pool)
  508. __attribute__((nonnull(2,4)));
  509. /**
  510. * Set the data associated with the current pool
  511. * @param data The user data associated with the pool.
  512. * @param key The key to use for association
  513. * @param cleanup The cleanup program to use to cleanup the data (NULL if none)
  514. * @param pool The current pool
  515. * @note same as apr_pool_userdata_set(), except that this version doesn't
  516. * make a copy of the key (this function is useful, for example, when
  517. * the key is a string literal)
  518. * @warning This should NOT be used if the key could change addresses by
  519. * any means between the apr_pool_userdata_setn() call and a
  520. * subsequent apr_pool_userdata_get() on that key, such as if a
  521. * static string is used as a userdata key in a DSO and the DSO could
  522. * be unloaded and reloaded between the _setn() and the _get(). You
  523. * MUST use apr_pool_userdata_set() in such cases.
  524. * @warning More generally, the key and the data to be attached to the
  525. * pool should have a life span at least as long as the pool itself.
  526. *
  527. */
  528. APR_DECLARE(apr_status_t) apr_pool_userdata_setn(
  529. const void *data, const char *key,
  530. apr_status_t (*cleanup)(void *),
  531. apr_pool_t *pool)
  532. __attribute__((nonnull(2,4)));
  533. /**
  534. * Return the data associated with the current pool.
  535. * @param data The user data associated with the pool.
  536. * @param key The key for the data to retrieve
  537. * @param pool The current pool.
  538. */
  539. APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
  540. apr_pool_t *pool)
  541. __attribute__((nonnull(1,2,3)));
  542. /**
  543. * @defgroup PoolCleanup Pool Cleanup Functions
  544. *
  545. * Cleanups are performed in the reverse order they were registered. That is:
  546. * Last In, First Out. A cleanup function can safely allocate memory from
  547. * the pool that is being cleaned up. It can also safely register additional
  548. * cleanups which will be run LIFO, directly after the current cleanup
  549. * terminates. Cleanups have to take caution in calling functions that
  550. * create subpools. Subpools, created during cleanup will NOT automatically
  551. * be cleaned up. In other words, cleanups are to clean up after themselves.
  552. *
  553. * @{
  554. */
  555. /**
  556. * Register a function to be called when a pool is cleared or destroyed
  557. * @param p The pool to register the cleanup with
  558. * @param data The data to pass to the cleanup function.
  559. * @param plain_cleanup The function to call when the pool is cleared
  560. * or destroyed
  561. * @param child_cleanup The function to call when a child process is about
  562. * to exec - this function is called in the child, obviously!
  563. */
  564. APR_DECLARE(void) apr_pool_cleanup_register(
  565. apr_pool_t *p, const void *data,
  566. apr_status_t (*plain_cleanup)(void *),
  567. apr_status_t (*child_cleanup)(void *))
  568. __attribute__((nonnull(3,4)));
  569. /**
  570. * Register a function to be called when a pool is cleared or destroyed.
  571. *
  572. * Unlike apr_pool_cleanup_register which registers a cleanup
  573. * that is called AFTER all subpools are destroyed, this function registers
  574. * a function that will be called before any of the subpools are destroyed.
  575. *
  576. * @param p The pool to register the cleanup with
  577. * @param data The data to pass to the cleanup function.
  578. * @param plain_cleanup The function to call when the pool is cleared
  579. * or destroyed
  580. */
  581. APR_DECLARE(void) apr_pool_pre_cleanup_register(
  582. apr_pool_t *p, const void *data,
  583. apr_status_t (*plain_cleanup)(void *))
  584. __attribute__((nonnull(3)));
  585. /**
  586. * Remove a previously registered cleanup function.
  587. *
  588. * The cleanup most recently registered with @a p having the same values of
  589. * @a data and @a cleanup will be removed.
  590. *
  591. * @param p The pool to remove the cleanup from
  592. * @param data The data of the registered cleanup
  593. * @param cleanup The function to remove from cleanup
  594. * @remarks For some strange reason only the plain_cleanup is handled by this
  595. * function
  596. */
  597. APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
  598. apr_status_t (*cleanup)(void *))
  599. __attribute__((nonnull(3)));
  600. /**
  601. * Replace the child cleanup function of a previously registered cleanup.
  602. *
  603. * The cleanup most recently registered with @a p having the same values of
  604. * @a data and @a plain_cleanup will have the registered child cleanup
  605. * function replaced with @a child_cleanup.
  606. *
  607. * @param p The pool of the registered cleanup
  608. * @param data The data of the registered cleanup
  609. * @param plain_cleanup The plain cleanup function of the registered cleanup
  610. * @param child_cleanup The function to register as the child cleanup
  611. */
  612. APR_DECLARE(void) apr_pool_child_cleanup_set(
  613. apr_pool_t *p, const void *data,
  614. apr_status_t (*plain_cleanup)(void *),
  615. apr_status_t (*child_cleanup)(void *))
  616. __attribute__((nonnull(3,4)));
  617. /**
  618. * Run the specified cleanup function immediately and unregister it.
  619. *
  620. * The cleanup most recently registered with @a p having the same values of
  621. * @a data and @a cleanup will be removed and @a cleanup will be called
  622. * with @a data as the argument.
  623. *
  624. * @param p The pool to remove the cleanup from
  625. * @param data The data to remove from cleanup
  626. * @param cleanup The function to remove from cleanup
  627. */
  628. APR_DECLARE(apr_status_t) apr_pool_cleanup_run(apr_pool_t *p, void *data,
  629. apr_status_t (*cleanup)(void *))
  630. __attribute__((nonnull(3)));
  631. /**
  632. * An empty cleanup function.
  633. *
  634. * Passed to apr_pool_cleanup_register() when no cleanup is required.
  635. *
  636. * @param data The data to cleanup, will not be used by this function.
  637. */
  638. APR_DECLARE_NONSTD(apr_status_t) apr_pool_cleanup_null(void *data);
  639. /**
  640. * Run all registered child cleanups, in preparation for an exec()
  641. * call in a forked child -- close files, etc., but *don't* flush I/O
  642. * buffers, *don't* wait for subprocesses, and *don't* free any
  643. * memory.
  644. */
  645. APR_DECLARE(void) apr_pool_cleanup_for_exec(void);
  646. /** @} */
  647. /**
  648. * @defgroup PoolDebug Pool Debugging functions.
  649. *
  650. * pools have nested lifetimes -- sub_pools are destroyed when the
  651. * parent pool is cleared. We allow certain liberties with operations
  652. * on things such as tables (and on other structures in a more general
  653. * sense) where we allow the caller to insert values into a table which
  654. * were not allocated from the table's pool. The table's data will
  655. * remain valid as long as all the pools from which its values are
  656. * allocated remain valid.
  657. *
  658. * For example, if B is a sub pool of A, and you build a table T in
  659. * pool B, then it's safe to insert data allocated in A or B into T
  660. * (because B lives at most as long as A does, and T is destroyed when
  661. * B is cleared/destroyed). On the other hand, if S is a table in
  662. * pool A, it is safe to insert data allocated in A into S, but it
  663. * is *not safe* to insert data allocated from B into S... because
  664. * B can be cleared/destroyed before A is (which would leave dangling
  665. * pointers in T's data structures).
  666. *
  667. * In general we say that it is safe to insert data into a table T
  668. * if the data is allocated in any ancestor of T's pool. This is the
  669. * basis on which the APR_POOL_DEBUG code works -- it tests these ancestor
  670. * relationships for all data inserted into tables. APR_POOL_DEBUG also
  671. * provides tools (apr_pool_find, and apr_pool_is_ancestor) for other
  672. * folks to implement similar restrictions for their own data
  673. * structures.
  674. *
  675. * However, sometimes this ancestor requirement is inconvenient --
  676. * sometimes it's necessary to create a sub pool where the sub pool is
  677. * guaranteed to have the same lifetime as the parent pool. This is a
  678. * guarantee implemented by the *caller*, not by the pool code. That
  679. * is, the caller guarantees they won't destroy the sub pool
  680. * individually prior to destroying the parent pool.
  681. *
  682. * In this case the caller must call apr_pool_join() to indicate this
  683. * guarantee to the APR_POOL_DEBUG code.
  684. *
  685. * These functions are only implemented when #APR_POOL_DEBUG is set.
  686. *
  687. * @{
  688. */
  689. #if APR_POOL_DEBUG || defined(DOXYGEN)
  690. /**
  691. * Guarantee that a subpool has the same lifetime as the parent.
  692. * @param p The parent pool
  693. * @param sub The subpool
  694. */
  695. APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub)
  696. __attribute__((nonnull(2)));
  697. /**
  698. * Find a pool from something allocated in it.
  699. * @param mem The thing allocated in the pool
  700. * @return The pool it is allocated in
  701. */
  702. APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem);
  703. /**
  704. * Report the number of bytes currently in the pool
  705. * @param p The pool to inspect
  706. * @param recurse Recurse/include the subpools' sizes
  707. * @return The number of bytes
  708. */
  709. APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *p, int recurse)
  710. __attribute__((nonnull(1)));
  711. /**
  712. * Lock a pool
  713. * @param pool The pool to lock
  714. * @param flag The flag
  715. */
  716. APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag);
  717. /* @} */
  718. #else /* APR_POOL_DEBUG or DOXYGEN */
  719. #ifdef apr_pool_join
  720. #undef apr_pool_join
  721. #endif
  722. #define apr_pool_join(a,b)
  723. #ifdef apr_pool_lock
  724. #undef apr_pool_lock
  725. #endif
  726. #define apr_pool_lock(pool, lock)
  727. #endif /* APR_POOL_DEBUG or DOXYGEN */
  728. /** @} */
  729. #ifdef __cplusplus
  730. }
  731. #endif
  732. #endif /* !APR_POOLS_H */