Apache Portable Runtime
Loading...
Searching...
No Matches
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
50extern "C" {
51#endif
52
53/**
54 * @defgroup apr_pools Memory Pool Functions
55 * @ingroup APR
56 * @{
57 */
58
59/** The fundamental pool type */
60typedef 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. */
148typedef 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 */
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 pool.
204 * @deprecated @see apr_pool_create_unmanaged_ex.
205 */
207 apr_abortfunc_t abort_fn,
208 apr_allocator_t *allocator);
209
210/**
211 * Create a new unmanaged pool.
212 * @param newpool The pool we have just created.
213 * @param abort_fn A function to use if the pool cannot allocate more memory.
214 * @param allocator The allocator to use with the new pool. If NULL a
215 * new allocator will be created with the new pool as owner.
216 * @remark An unmanaged pool is a special pool without a parent; it will
217 * NOT be destroyed upon apr_terminate. It must be explicitly
218 * destroyed by calling apr_pool_destroy, to prevent memory leaks.
219 * Use of this function is discouraged, think twice about whether
220 * you really really need it.
221 * @warning Any child cleanups registered against the new pool, or
222 * against sub-pools thereof, will not be executed during an
223 * invocation of apr_proc_create(), so resources created in an
224 * "unmanaged" pool hierarchy will leak to child processes.
225 */
227 apr_abortfunc_t abort_fn,
228 apr_allocator_t *allocator)
229 __attribute__((nonnull(1)));
230
231/**
232 * Debug version of apr_pool_create_ex.
233 * @param newpool @see apr_pool_create.
234 * @param parent @see apr_pool_create.
235 * @param abort_fn @see apr_pool_create.
236 * @param allocator @see apr_pool_create.
237 * @param file_line Where the function is called from.
238 * This is usually APR_POOL__FILE_LINE__.
239 * @remark Only available when APR_POOL_DEBUG is defined.
240 * Call this directly if you have your apr_pool_create_ex
241 * calls in a wrapper function and wish to override
242 * the file_line argument to reflect the caller of
243 * your wrapper function. If you do not have
244 * apr_pool_create_ex in a wrapper, trust the macro
245 * and don't call apr_pool_create_ex_debug directly.
246 */
248 apr_pool_t *parent,
249 apr_abortfunc_t abort_fn,
250 apr_allocator_t *allocator,
251 const char *file_line)
252 __attribute__((nonnull(1)));
253
254#if APR_POOL_DEBUG
255#define apr_pool_create_ex(newpool, parent, abort_fn, allocator) \
256 apr_pool_create_ex_debug(newpool, parent, abort_fn, allocator, \
257 APR_POOL__FILE_LINE__)
258#endif
259
260/**
261 * Debug version of apr_pool_create_core_ex.
262 * @deprecated @see apr_pool_create_unmanaged_ex_debug.
263 */
265 apr_abortfunc_t abort_fn,
266 apr_allocator_t *allocator,
267 const char *file_line);
268
269/**
270 * Debug version of apr_pool_create_unmanaged_ex.
271 * @param newpool @see apr_pool_create_unmanaged.
272 * @param abort_fn @see apr_pool_create_unmanaged.
273 * @param allocator @see apr_pool_create_unmanaged.
274 * @param file_line Where the function is called from.
275 * This is usually APR_POOL__FILE_LINE__.
276 * @remark Only available when APR_POOL_DEBUG is defined.
277 * Call this directly if you have your apr_pool_create_unmanaged_ex
278 * calls in a wrapper function and wish to override
279 * the file_line argument to reflect the caller of
280 * your wrapper function. If you do not have
281 * apr_pool_create_core_ex in a wrapper, trust the macro
282 * and don't call apr_pool_create_core_ex_debug directly.
283 */
285 apr_abortfunc_t abort_fn,
286 apr_allocator_t *allocator,
287 const char *file_line)
288 __attribute__((nonnull(1)));
289
290#if APR_POOL_DEBUG
291#define apr_pool_create_core_ex(newpool, abort_fn, allocator) \
292 apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
293 APR_POOL__FILE_LINE__)
294
295#define apr_pool_create_unmanaged_ex(newpool, abort_fn, allocator) \
296 apr_pool_create_unmanaged_ex_debug(newpool, abort_fn, allocator, \
297 APR_POOL__FILE_LINE__)
298
299#endif
300
301/**
302 * Create a new pool.
303 * @param newpool The pool we have just created.
304 * @param parent The parent pool. If this is NULL, the new pool is a root
305 * pool. If it is non-NULL, the new pool will inherit all
306 * of its parent pool's attributes, except the apr_pool_t will
307 * be a sub-pool.
308 * @remark This function is thread-safe, in the sense that multiple threads
309 * can safely create subpools of the same parent pool concurrently.
310 * Similarly, a subpool can be created by one thread at the same
311 * time that another thread accesses the parent pool.
312 */
313#if defined(DOXYGEN)
315 apr_pool_t *parent);
316#else
317#if APR_POOL_DEBUG
318#define apr_pool_create(newpool, parent) \
319 apr_pool_create_ex_debug(newpool, parent, NULL, NULL, \
320 APR_POOL__FILE_LINE__)
321#else
322#define apr_pool_create(newpool, parent) \
323 apr_pool_create_ex(newpool, parent, NULL, NULL)
324#endif
325#endif
326
327/**
328 * Create a new unmanaged pool.
329 * @param newpool The pool we have just created.
330 */
331#if defined(DOXYGEN)
333APR_DECLARE(apr_status_t) apr_pool_create_unmanaged(apr_pool_t **newpool);
334#else
335#if APR_POOL_DEBUG
336#define apr_pool_create_core(newpool) \
337 apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
338 APR_POOL__FILE_LINE__)
339#define apr_pool_create_unmanaged(newpool) \
340 apr_pool_create_unmanaged_ex_debug(newpool, NULL, NULL, \
341 APR_POOL__FILE_LINE__)
342#else
343#define apr_pool_create_core(newpool) \
344 apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
345#define apr_pool_create_unmanaged(newpool) \
346 apr_pool_create_unmanaged_ex(newpool, NULL, NULL)
347#endif
348#endif
349
350/**
351 * Find the pool's allocator
352 * @param pool The pool to get the allocator from.
353 */
355 __attribute__((nonnull(1)));
356
357/**
358 * Clear all memory in the pool and run all the cleanups. This also destroys all
359 * subpools.
360 * @param p The pool to clear
361 * @remark This does not actually free the memory, it just allows the pool
362 * to re-use this memory for the next allocation.
363 * @see apr_pool_destroy()
364 */