1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "apr_private.h"
20 #include "apr_atomic.h"
21 #include "apr_portable.h" /* for get_os_proc */
22 #include "apr_strings.h"
23 #include "apr_general.h"
24 #include "apr_pools.h"
25 #include "apr_allocator.h"
27 #include "apr_thread_mutex.h"
30 #define APR_WANT_MEMFUNC
34 #include <stdlib.h> /* for malloc, free and abort */
38 #include <unistd.h> /* for getpid */
46 #define MIN_ALLOC 8192
49 #define BOUNDARY_INDEX 12
50 #define BOUNDARY_SIZE (1 << BOUNDARY_INDEX)
53 * Timing constants for killing subprocesses
54 * There is a total 3-second delay between sending a SIGINT
55 * and sending of the final SIGKILL.
56 * TIMEOUT_INTERVAL should be set to TIMEOUT_USECS / 64
57 * for the exponetial timeout alogrithm.
59 #define TIMEOUT_USECS 3000000
60 #define TIMEOUT_INTERVAL 46875
65 * @note The max_free_index and current_free_index fields are not really
66 * indices, but quantities of BOUNDARY_SIZE big memory blocks.
69 struct apr_allocator_t {
70 /** largest used index into free[], always < MAX_INDEX */
71 apr_uint32_t max_index;
72 /** Total size (in BOUNDARY_SIZE multiples) of unused memory before
73 * blocks are given back. @see apr_allocator_max_free_set().
74 * @note Initialized to APR_ALLOCATOR_MAX_FREE_UNLIMITED,
75 * which means to never give back blocks.
77 apr_uint32_t max_free_index;
79 * Memory size (in BOUNDARY_SIZE multiples) that currently must be freed
80 * before blocks are given back. Range: 0..max_free_index
82 apr_uint32_t current_free_index;
84 apr_thread_mutex_t *mutex;
85 #endif /* APR_HAS_THREADS */
88 * Lists of free nodes. Slot 0 is used for oversized nodes,
89 * and the slots 1..MAX_INDEX-1 contain nodes of sizes
90 * (i+1) * BOUNDARY_SIZE. Example for BOUNDARY_INDEX == 12:
91 * slot 0: nodes larger than 81920
97 apr_memnode_t *free[MAX_INDEX];
100 #define SIZEOF_ALLOCATOR_T APR_ALIGN_DEFAULT(sizeof(apr_allocator_t))
107 APR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator)
109 apr_allocator_t *new_allocator;
113 if ((new_allocator = malloc(SIZEOF_ALLOCATOR_T)) == NULL)
116 memset(new_allocator, 0, SIZEOF_ALLOCATOR_T);
117 new_allocator->max_free_index = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
119 *allocator = new_allocator;
124 APR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator)
127 apr_memnode_t *node, **ref;
129 for (index = 0; index < MAX_INDEX; index++) {
130 ref = &allocator->free[index];
131 while ((node = *ref) != NULL) {
141 APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
142 apr_thread_mutex_t *mutex)
144 allocator->mutex = mutex;
147 APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
148 apr_allocator_t *allocator)
150 return allocator->mutex;
152 #endif /* APR_HAS_THREADS */
154 APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
157 allocator->owner = pool;
160 APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
162 return allocator->owner;
165 APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
168 apr_uint32_t max_free_index;
171 apr_thread_mutex_t *mutex;
173 mutex = apr_allocator_mutex_get(allocator);
175 apr_thread_mutex_lock(mutex);
176 #endif /* APR_HAS_THREADS */
178 max_free_index = APR_ALIGN(size, BOUNDARY_SIZE) >> BOUNDARY_INDEX;
179 allocator->current_free_index += max_free_index;
180 allocator->current_free_index -= allocator->max_free_index;
181 allocator->max_free_index = max_free_index;
182 if (allocator->current_free_index > max_free_index)
183 allocator->current_free_index = max_free_index;
187 apr_thread_mutex_unlock(mutex);
192 apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t in_size)
194 apr_memnode_t *node, **ref;
195 apr_uint32_t i, index, max_index;
198 /* Round up the block size to the next boundary, but always
199 * allocate at least a certain size (MIN_ALLOC).
201 size = APR_ALIGN(in_size + APR_MEMNODE_T_SIZE, BOUNDARY_SIZE);
202 if (size < in_size) {
205 if (size < MIN_ALLOC)
208 /* Find the index for this node size by
209 * dividing its size by the boundary size
211 index = (size >> BOUNDARY_INDEX) - 1;
213 /* First see if there are any nodes in the area we know
214 * our node will fit into.
216 if (index <= allocator->max_index) {
218 if (allocator->mutex)
219 apr_thread_mutex_lock(allocator->mutex);
220 #endif /* APR_HAS_THREADS */
222 /* Walk the free list to see if there are
223 * any nodes on it of the requested size
225 * NOTE: an optimization would be to check
226 * allocator->free[index] first and if no
227 * node is present, directly use
228 * allocator->free[max_index]. This seems
229 * like overkill though and could cause
232 max_index = allocator->max_index;
233 ref = &allocator->free[index];
235 while (*ref == NULL && i < max_index) {
240 if ((node = *ref) != NULL) {
241 /* If we have found a node and it doesn't have any
242 * nodes waiting in line behind it _and_ we are on
243 * the highest available index, find the new highest
246 if ((*ref = node->next) == NULL && i >= max_index) {
251 while (*ref == NULL && max_index > 0);
253 allocator->max_index = max_index;
256 allocator->current_free_index += node->index;
257 if (allocator->current_free_index > allocator->max_free_index)
258 allocator->current_free_index = allocator->max_free_index;
261 if (allocator->mutex)
262 apr_thread_mutex_unlock(allocator->mutex);
263 #endif /* APR_HAS_THREADS */
266 node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
272 if (allocator->mutex)
273 apr_thread_mutex_unlock(allocator->mutex);
274 #endif /* APR_HAS_THREADS */
277 /* If we found nothing, seek the sink (at index 0), if
280 else if (allocator->free[0]) {
282 if (allocator->mutex)
283 apr_thread_mutex_lock(allocator->mutex);
284 #endif /* APR_HAS_THREADS */
286 /* Walk the free list to see if there are
287 * any nodes on it of the requested size
289 ref = &allocator->free[0];
290 while ((node = *ref) != NULL && index > node->index)
296 allocator->current_free_index += node->index;
297 if (allocator->current_free_index > allocator->max_free_index)
298 allocator->current_free_index = allocator->max_free_index;
301 if (allocator->mutex)
302 apr_thread_mutex_unlock(allocator->mutex);
303 #endif /* APR_HAS_THREADS */
306 node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
312 if (allocator->mutex)
313 apr_thread_mutex_unlock(allocator->mutex);
314 #endif /* APR_HAS_THREADS */
317 /* If we haven't got a suitable node, malloc a new one
320 if ((node = malloc(size)) == NULL)
325 node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
326 node->endp = (char *)node + size;
332 void allocator_free(apr_allocator_t *allocator, apr_memnode_t *node)
334 apr_memnode_t *next, *freelist = NULL;
335 apr_uint32_t index, max_index;
336 apr_uint32_t max_free_index, current_free_index;
339 if (allocator->mutex)
340 apr_thread_mutex_lock(allocator->mutex);
341 #endif /* APR_HAS_THREADS */
343 max_index = allocator->max_index;
344 max_free_index = allocator->max_free_index;
345 current_free_index = allocator->current_free_index;
347 /* Walk the list of submitted nodes and free them one by one,
348 * shoving them in the right 'size' buckets as we go.
354 if (max_free_index != APR_ALLOCATOR_MAX_FREE_UNLIMITED
355 && index > current_free_index) {
356 node->next = freelist;
359 else if (index < MAX_INDEX) {
360 /* Add the node to the appropiate 'size' bucket. Adjust
361 * the max_index when appropiate.
363 if ((node->next = allocator->free[index]) == NULL
364 && index > max_index) {
367 allocator->free[index] = node;
368 if (current_free_index >= index)
369 current_free_index -= index;
371 current_free_index = 0;
374 /* This node is too large to keep in a specific size bucket,
375 * just add it to the sink (at index 0).
377 node->next = allocator->free[0];
378 allocator->free[0] = node;
379 if (current_free_index >= index)
380 current_free_index -= index;
382 current_free_index = 0;
384 } while ((node = next) != NULL);
386 allocator->max_index = max_index;
387 allocator->current_free_index = current_free_index;
390 if (allocator->mutex)
391 apr_thread_mutex_unlock(allocator->mutex);
392 #endif /* APR_HAS_THREADS */
394 while (freelist != NULL) {
396 freelist = node->next;
401 APR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator,
404 return allocator_alloc(allocator, size);
407 APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator,
410 allocator_free(allocator, node);
419 #define APR_POOL_DEBUG_GENERAL 0x01
420 #define APR_POOL_DEBUG_VERBOSE 0x02
421 #define APR_POOL_DEBUG_LIFETIME 0x04
422 #define APR_POOL_DEBUG_OWNER 0x08
423 #define APR_POOL_DEBUG_VERBOSE_ALLOC 0x10
425 #define APR_POOL_DEBUG_VERBOSE_ALL (APR_POOL_DEBUG_VERBOSE \
426 | APR_POOL_DEBUG_VERBOSE_ALLOC)
433 typedef struct cleanup_t cleanup_t;
435 /** A list of processes */
436 struct process_chain {
437 /** The process ID */
439 apr_kill_conditions_e kill_how;
440 /** The next process in the list */
441 struct process_chain *next;
447 typedef struct debug_node_t debug_node_t;
449 struct debug_node_t {
456 #define SIZEOF_DEBUG_NODE_T APR_ALIGN_DEFAULT(sizeof(debug_node_t))
458 #endif /* APR_POOL_DEBUG */
460 /* The ref field in the apr_pool_t struct holds a
461 * pointer to the pointer referencing this pool.
462 * It is used for parent, child, sibling management.
463 * Look at apr_pool_create_ex() and apr_pool_destroy()
464 * to see how it is used.
472 apr_allocator_t *allocator;
473 struct process_chain *subprocesses;
474 apr_abortfunc_t abort_fn;
475 apr_hash_t *user_data;
479 apr_memnode_t *active;
480 apr_memnode_t *self; /* The node containing the pool itself */
481 char *self_first_avail;
483 #else /* APR_POOL_DEBUG */
485 const char *file_line;
486 apr_uint32_t creation_flags;
487 unsigned int stat_alloc;
488 unsigned int stat_total_alloc;
489 unsigned int stat_clear;
491 apr_os_thread_t owner;
492 apr_thread_mutex_t *mutex;
493 #endif /* APR_HAS_THREADS */
494 #endif /* APR_POOL_DEBUG */
496 apr_os_proc_t owner_proc;
497 #endif /* defined(NETWARE) */
500 #define SIZEOF_POOL_T APR_ALIGN_DEFAULT(sizeof(apr_pool_t))
507 static apr_byte_t apr_pools_initialized = 0;
508 static apr_pool_t *global_pool = NULL;
511 static apr_allocator_t *global_allocator = NULL;
512 #endif /* !APR_POOL_DEBUG */
514 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
515 static apr_file_t *file_stderr = NULL;
516 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
522 static void run_cleanups(cleanup_t **c);
523 static void run_child_cleanups(cleanup_t **c);
524 static void free_proc_chain(struct process_chain *procs);
532 APR_DECLARE(apr_status_t) apr_pool_initialize(void)
536 if (apr_pools_initialized++)
539 if ((rv = apr_allocator_create(&global_allocator)) != APR_SUCCESS) {
540 apr_pools_initialized = 0;
544 if ((rv = apr_pool_create_ex(&global_pool, NULL, NULL,
545 global_allocator)) != APR_SUCCESS) {
546 apr_allocator_destroy(global_allocator);
547 global_allocator = NULL;
548 apr_pools_initialized = 0;
552 apr_pool_tag(global_pool, "apr_global_pool");
554 /* This has to happen here because mutexes might be backed by
555 * atomics. It used to be snug and safe in apr_initialize().
557 if ((rv = apr_atomic_init(global_pool)) != APR_SUCCESS) {
563 apr_thread_mutex_t *mutex;
565 if ((rv = apr_thread_mutex_create(&mutex,
566 APR_THREAD_MUTEX_DEFAULT,
567 global_pool)) != APR_SUCCESS) {
571 apr_allocator_mutex_set(global_allocator, mutex);
573 #endif /* APR_HAS_THREADS */
575 apr_allocator_owner_set(global_allocator, global_pool);
580 APR_DECLARE(void) apr_pool_terminate(void)
582 if (!apr_pools_initialized)
585 if (--apr_pools_initialized)
588 apr_pool_destroy(global_pool); /* This will also destroy the mutex */
591 global_allocator = NULL;
595 void netware_pool_proc_cleanup ()
597 apr_pool_t *pool = global_pool->child;
598 apr_os_proc_t owner_proc = (apr_os_proc_t)getnlmhandle();
601 if (pool->owner_proc == owner_proc) {
602 apr_pool_destroy (pool);
603 pool = global_pool->child;
606 pool = pool->sibling;
611 #endif /* defined(NETWARE) */
613 /* Node list management helper macros; list_insert() inserts 'node'
615 #define list_insert(node, point) do { \
616 node->ref = point->ref; \
618 node->next = point; \
619 point->ref = &node->next; \
622 /* list_remove() removes 'node' from its list. */
623 #define list_remove(node) do { \
624 *node->ref = node->next; \
625 node->next->ref = node->ref; \
632 APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t in_size)
634 apr_memnode_t *active, *node;
636 apr_uint32_t free_index;
639 size = APR_ALIGN_DEFAULT(in_size);
640 if (size < in_size) {
642 pool->abort_fn(APR_ENOMEM);
645 active = pool->active;
647 /* If the active node has enough bytes left, use it. */
648 if (size < (apr_size_t)(active->endp - active->first_avail)) {
649 mem = active->first_avail;
650 active->first_avail += size;
656 if (size < (apr_size_t)(node->endp - node->first_avail)) {
660 if ((node = allocator_alloc(pool->allocator, size)) == NULL) {
662 pool->abort_fn(APR_ENOMEM);
668 node->free_index = 0;
670 mem = node->first_avail;
671 node->first_avail += size;
673 list_insert(node, active);
677 free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
678 BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
680 active->free_index = free_index;
682 if (free_index >= node->free_index)
688 while (free_index < node->free_index);
691 list_insert(active, node);
696 /* Provide an implementation of apr_pcalloc for backward compatibility
697 * with code built before apr_pcalloc was a macro
704 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size);
705 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size)
709 if ((mem = apr_palloc(pool, size)) != NULL) {
710 memset(mem, 0, size);
718 * Pool creation/destruction
721 APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool)
723 apr_memnode_t *active;
725 /* Destroy the subpools. The subpools will detach themselves from
726 * this pool thus this loop is safe and easy.
729 apr_pool_destroy(pool->child);
732 run_cleanups(&pool->cleanups);
733 pool->cleanups = NULL;
735 /* Free subprocesses */
736 free_proc_chain(pool->subprocesses);
737 pool->subprocesses = NULL;
739 /* Clear the user data. */
740 pool->user_data = NULL;
742 /* Find the node attached to the pool structure, reset it, make
743 * it the active node and free the rest of the nodes.
745 active = pool->active = pool->self;
746 active->first_avail = pool->self_first_avail;
748 if (active->next == active)
752 allocator_free(pool->allocator, active->next);
753 active->next = active;
754 active->ref = &active->next;
757 APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
759 apr_memnode_t *active;
760 apr_allocator_t *allocator;
762 /* Destroy the subpools. The subpools will detach themselve from
763 * this pool thus this loop is safe and easy.
766 apr_pool_destroy(pool->child);
769 run_cleanups(&pool->cleanups);
771 /* Free subprocesses */
772 free_proc_chain(pool->subprocesses);
774 /* Remove the pool from the parents child list */
777 apr_thread_mutex_t *mutex;
779 if ((mutex = apr_allocator_mutex_get(pool->parent->allocator)) != NULL)
780 apr_thread_mutex_lock(mutex);
781 #endif /* APR_HAS_THREADS */
783 if ((*pool->ref = pool->sibling) != NULL)
784 pool->sibling->ref = pool->ref;
788 apr_thread_mutex_unlock(mutex);
789 #endif /* APR_HAS_THREADS */
792 /* Find the block attached to the pool structure. Save a copy of the
793 * allocator pointer, because the pool struct soon will be no more.
795 allocator = pool->allocator;
800 if (apr_allocator_owner_get(allocator) == pool) {
801 /* Make sure to remove the lock, since it is highly likely to
804 apr_allocator_mutex_set(allocator, NULL);
806 #endif /* APR_HAS_THREADS */
808 /* Free all the nodes in the pool (including the node holding the
809 * pool struct), by giving them back to the allocator.
811 allocator_free(allocator, active);
813 /* If this pool happens to be the owner of the allocator, free
814 * everything in the allocator (that includes the pool struct
815 * and the allocator). Don't worry about destroying the optional mutex
816 * in the allocator, it will have been destroyed by the cleanup function.
818 if (apr_allocator_owner_get(allocator) == pool) {
819 apr_allocator_destroy(allocator);
823 APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
825 apr_abortfunc_t abort_fn,
826 apr_allocator_t *allocator)
834 parent = global_pool;
836 if (!abort_fn && parent)
837 abort_fn = parent->abort_fn;
839 if (allocator == NULL)
840 allocator = parent->allocator;
842 if ((node = allocator_alloc(allocator,
843 MIN_ALLOC - APR_MEMNODE_T_SIZE)) == NULL) {
845 abort_fn(APR_ENOMEM);
851 node->ref = &node->next;
853 pool = (apr_pool_t *)node->first_avail;
854 node->first_avail = pool->self_first_avail = (char *)pool + SIZEOF_POOL_T;
856 pool->allocator = allocator;
857 pool->active = pool->self = node;
858 pool->abort_fn = abort_fn;
860 pool->cleanups = NULL;
861 pool->subprocesses = NULL;
862 pool->user_data = NULL;
866 pool->owner_proc = (apr_os_proc_t)getnlmhandle();
867 #endif /* defined(NETWARE) */
869 if ((pool->parent = parent) != NULL) {
871 apr_thread_mutex_t *mutex;
873 if ((mutex = apr_allocator_mutex_get(parent->allocator)) != NULL)
874 apr_thread_mutex_lock(mutex);
875 #endif /* APR_HAS_THREADS */
877 if ((pool->sibling = parent->child) != NULL)
878 pool->sibling->ref = &pool->sibling;
880 parent->child = pool;
881 pool->ref = &parent->child;
885 apr_thread_mutex_unlock(mutex);
886 #endif /* APR_HAS_THREADS */
889 pool->sibling = NULL;
904 * apr_psprintf is implemented by writing directly into the current
905 * block of the pool, starting right at first_avail. If there's
906 * insufficient room, then a new block is allocated and the earlier
907 * output is copied over. The new block isn't linked into the pool
908 * until all the output is done.
910 * Note that this is completely safe because nothing else can
911 * allocate in this apr_pool_t while apr_psprintf is running. alarms are
912 * blocked, and the only thing outside of apr_pools.c that's invoked
913 * is apr_vformatter -- which was purposefully written to be
914 * self-contained with no callouts.
917 struct psprintf_data {
918 apr_vformatter_buff_t vbuff;
921 apr_byte_t got_a_new_node;
925 #define APR_PSPRINTF_MIN_STRINGSIZE 32
927 static int psprintf_flush(apr_vformatter_buff_t *vbuff)
929 struct psprintf_data *ps = (struct psprintf_data *)vbuff;
930 apr_memnode_t *node, *active;
931 apr_size_t cur_len, size;
934 apr_uint32_t free_index;
938 strp = ps->vbuff.curpos;
939 cur_len = strp - active->first_avail;
942 /* Make sure that we don't try to use a block that has less
943 * than APR_PSPRINTF_MIN_STRINGSIZE bytes left in it. This
944 * also catches the case where size == 0, which would result
945 * in reusing a block that can't even hold the NUL byte.
947 if (size < APR_PSPRINTF_MIN_STRINGSIZE)
948 size = APR_PSPRINTF_MIN_STRINGSIZE;
951 if (!ps->got_a_new_node
952 && size < (apr_size_t)(node->endp - node->first_avail)) {
955 list_insert(node, active);
957 node->free_index = 0;
961 free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
962 BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
964 active->free_index = free_index;
966 if (free_index < node->free_index) {
970 while (free_index < node->free_index);
973 list_insert(active, node);
979 if ((node = allocator_alloc(pool->allocator, size)) == NULL)
982 if (ps->got_a_new_node) {
983 active->next = ps->free;
987 ps->got_a_new_node = 1;
990 memcpy(node->first_avail, active->first_avail, cur_len);
993 ps->vbuff.curpos = node->first_avail + cur_len;
994 ps->vbuff.endpos = node->endp - 1; /* Save a byte for NUL terminator */
999 APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *pool, const char *fmt, va_list ap)
1001 struct psprintf_data ps;
1004 apr_memnode_t *active, *node;
1005 apr_uint32_t free_index;
1007 ps.node = active = pool->active;
1009 ps.vbuff.curpos = ps.node->first_avail;
1011 /* Save a byte for the NUL terminator */
1012 ps.vbuff.endpos = ps.node->endp - 1;
1013 ps.got_a_new_node = 0;
1016 /* Make sure that the first node passed to apr_vformatter has at least
1017 * room to hold the NUL terminator.
1019 if (ps.node->first_avail == ps.node->endp) {
1020 if (psprintf_flush(&ps.vbuff) == -1) {
1021 if (pool->abort_fn) {
1022 pool->abort_fn(APR_ENOMEM);
1029 if (apr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
1031 pool->abort_fn(APR_ENOMEM);
1036 strp = ps.vbuff.curpos;
1039 size = strp - ps.node->first_avail;
1040 size = APR_ALIGN_DEFAULT(size);
1041 strp = ps.node->first_avail;
1042 ps.node->first_avail += size;
1045 allocator_free(pool->allocator, ps.free);
1048 * Link the node in if it's a new one
1050 if (!ps.got_a_new_node)
1053 active = pool->active;
1056 node->free_index = 0;
1058 list_insert(node, active);
1060 pool->active = node;
1062 free_index = (APR_ALIGN(active->endp - active->first_avail + 1,
1063 BOUNDARY_SIZE) - BOUNDARY_SIZE) >> BOUNDARY_INDEX;
1065 active->free_index = free_index;
1066 node = active->next;
1068 if (free_index >= node->free_index)
1074 while (free_index < node->free_index);
1076 list_remove(active);
1077 list_insert(active, node);
1083 #else /* APR_POOL_DEBUG */
1085 * Debug helper functions
1090 * Walk the pool tree rooted at pool, depth first. When fn returns
1091 * anything other than 0, abort the traversal and return the value
1094 static int apr_pool_walk_tree(apr_pool_t *pool,
1095 int (*fn)(apr_pool_t *pool, void *data),
1101 rv = fn(pool, data);
1107 apr_thread_mutex_lock(pool->mutex);
1109 #endif /* APR_HAS_THREADS */
1111 child = pool->child;
1113 rv = apr_pool_walk_tree(child, fn, data);
1117 child = child->sibling;
1122 apr_thread_mutex_unlock(pool->mutex);
1124 #endif /* APR_HAS_THREADS */
1129 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1130 static void apr_pool_log_event(apr_pool_t *pool, const char *event,
1131 const char *file_line, int deref)
1135 apr_file_printf(file_stderr,
1140 #endif /* APR_HAS_THREADS */
1143 "(%10lu/%10lu/%10lu) "
1148 (unsigned long)getpid(),
1150 (unsigned long)apr_os_thread_current(),
1151 #endif /* APR_HAS_THREADS */
1153 (unsigned long)apr_pool_num_bytes(pool, 0),
1154 (unsigned long)apr_pool_num_bytes(pool, 1),
1155 (unsigned long)apr_pool_num_bytes(global_pool, 1),
1156 (unsigned int)pool, pool->tag,
1158 pool->stat_alloc, pool->stat_total_alloc, pool->stat_clear);
1161 apr_file_printf(file_stderr,
1166 #endif /* APR_HAS_THREADS */
1173 (unsigned long)getpid(),
1175 (unsigned long)apr_os_thread_current(),
1176 #endif /* APR_HAS_THREADS */
1183 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1185 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
1186 static int pool_is_child_of(apr_pool_t *parent, void *data)
1188 apr_pool_t *pool = (apr_pool_t *)data;
1190 return (pool == parent);
1193 static int apr_pool_is_child_of(apr_pool_t *pool, apr_pool_t *parent)
1198 return apr_pool_walk_tree(parent, pool_is_child_of, pool);
1200 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
1202 static void apr_pool_check_integrity(apr_pool_t *pool)
1204 /* Rule of thumb: use of the global pool is always
1205 * ok, since the only user is apr_pools.c. Unless
1206 * people have searched for the top level parent and
1207 * started to use that...
1209 if (pool == global_pool || global_pool == NULL)
1213 * This basically checks to see if the pool being used is still
1214 * a relative to the global pool. If not it was previously
1215 * destroyed, in which case we abort().
1217 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME)
1218 if (!apr_pool_is_child_of(pool, global_pool)) {
1219 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1220 apr_pool_log_event(pool, "LIFE",
1221 __FILE__ ":apr_pool_integrity check", 0);
1222 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1225 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_LIFETIME) */
1227 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER)
1229 if (!apr_os_thread_equal(pool->owner, apr_os_thread_current())) {
1230 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1231 apr_pool_log_event(pool, "THREAD",
1232 __FILE__ ":apr_pool_integrity check", 0);
1233 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1236 #endif /* APR_HAS_THREADS */
1237 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_OWNER) */
1242 * Initialization (debug)
1245 APR_DECLARE(apr_status_t) apr_pool_initialize(void)
1249 if (apr_pools_initialized++)
1252 /* Since the debug code works a bit differently then the
1253 * regular pools code, we ask for a lock here. The regular
1254 * pools code has got this lock embedded in the global
1255 * allocator, a concept unknown to debug mode.
1257 if ((rv = apr_pool_create_ex(&global_pool, NULL, NULL,
1258 NULL)) != APR_SUCCESS) {
1262 apr_pool_tag(global_pool, "APR global pool");
1264 apr_pools_initialized = 1;
1266 /* This has to happen here because mutexes might be backed by
1267 * atomics. It used to be snug and safe in apr_initialize().
1269 if ((rv = apr_atomic_init(global_pool)) != APR_SUCCESS) {
1273 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1274 apr_file_open_stderr(&file_stderr, global_pool);
1276 apr_file_printf(file_stderr,
1280 #endif /* APR_HAS_THREADS */
1281 "] ACTION (SIZE /POOL SIZE /TOTAL SIZE) "
1282 "POOL \"TAG\" <__FILE__:__LINE__> (ALLOCS/TOTAL ALLOCS/CLEARS)\n");
1284 apr_pool_log_event(global_pool, "GLOBAL", __FILE__ ":apr_pool_initialize", 0);
1286 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1291 APR_DECLARE(void) apr_pool_terminate(void)
1293 if (!apr_pools_initialized)
1296 apr_pools_initialized = 0;
1298 apr_pool_destroy(global_pool); /* This will also destroy the mutex */
1301 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL)
1303 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALL) */
1308 * Memory allocation (debug)
1311 static void *pool_alloc(apr_pool_t *pool, apr_size_t size)
1316 if ((mem = malloc(size)) == NULL) {
1318 pool->abort_fn(APR_ENOMEM);
1324 if (node == NULL || node->index == 64) {
1325 if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
1327 pool->abort_fn(APR_ENOMEM);
1332 memset(node, 0, SIZEOF_DEBUG_NODE_T);
1334 node->next = pool->nodes;
1339 node->beginp[node->index] = mem;
1340 node->endp[node->index] = (char *)mem + size;
1344 pool->stat_total_alloc++;
1349 APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *pool, apr_size_t size,
1350 const char *file_line)
1354 apr_pool_check_integrity(pool);
1356 mem = pool_alloc(pool, size);
1358 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
1359 apr_pool_log_event(pool, "PALLOC", file_line, 1);
1360 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
1365 APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *pool, apr_size_t size,
1366 const char *file_line)
1370 apr_pool_check_integrity(pool);
1372 mem = pool_alloc(pool, size);
1373 memset(mem, 0, size);
1375 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC)
1376 apr_pool_log_event(pool, "PCALLOC", file_line, 1);
1377 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE_ALLOC) */
1384 * Pool creation/destruction (debug)
1387 #define POOL_POISON_BYTE 'A'
1389 static void pool_clear_debug(apr_pool_t *pool, const char *file_line)
1394 /* Destroy the subpools. The subpools will detach themselves from
1395 * this pool thus this loop is safe and easy.
1398 apr_pool_destroy_debug(pool->child, file_line);
1401 run_cleanups(&pool->cleanups);
1402 pool->cleanups = NULL;
1404 /* Free subprocesses */
1405 free_proc_chain(pool->subprocesses);
1406 pool->subprocesses = NULL;
1408 /* Clear the user data. */
1409 pool->user_data = NULL;
1411 /* Free the blocks, scribbling over them first to help highlight
1412 * use-after-free issues. */
1413 while ((node = pool->nodes) != NULL) {
1414 pool->nodes = node->next;
1416 for (index = 0; index < node->index; index++) {
1417 memset(node->beginp[index], POOL_POISON_BYTE,
1418 node->endp[index] - node->beginp[index]);
1419 free(node->beginp[index]);
1422 memset(node, POOL_POISON_BYTE, SIZEOF_DEBUG_NODE_T);
1426 pool->stat_alloc = 0;
1430 APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *pool,
1431 const char *file_line)
1434 apr_thread_mutex_t *mutex = NULL;
1437 apr_pool_check_integrity(pool);
1439 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
1440 apr_pool_log_event(pool, "CLEAR", file_line, 1);
1441 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
1444 if (pool->parent != NULL)
1445 mutex = pool->parent->mutex;
1447 /* Lock the parent mutex before clearing so that if we have our
1448 * own mutex it won't be accessed by apr_pool_walk_tree after
1449 * it has been destroyed.
1451 if (mutex != NULL && mutex != pool->mutex) {
1452 apr_thread_mutex_lock(mutex);
1456 pool_clear_debug(pool, file_line);
1459 /* If we had our own mutex, it will have been destroyed by
1460 * the registered cleanups. Recreate the mutex. Unlock
1461 * the mutex we obtained above.
1463 if (mutex != pool->mutex) {
1464 (void)apr_thread_mutex_create(&pool->mutex,
1465 APR_THREAD_MUTEX_NESTED, pool);
1468 (void)apr_thread_mutex_unlock(mutex);
1470 #endif /* APR_HAS_THREADS */
1473 APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
1474 const char *file_line)
1476 apr_pool_check_integrity(pool);
1478 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
1479 apr_pool_log_event(pool, "DESTROY", file_line, 1);
1480 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
1482 pool_clear_debug(pool, file_line);
1484 /* Remove the pool from the parents child list */
1487 apr_thread_mutex_t *mutex;
1489 if ((mutex = pool->parent->mutex) != NULL)
1490 apr_thread_mutex_lock(mutex);
1491 #endif /* APR_HAS_THREADS */
1493 if ((*pool->ref = pool->sibling) != NULL)
1494 pool->sibling->ref = pool->ref;
1498 apr_thread_mutex_unlock(mutex);
1499 #endif /* APR_HAS_THREADS */
1502 if (pool->allocator != NULL
1503 && apr_allocator_owner_get(pool->allocator) == pool) {
1504 apr_allocator_destroy(pool->allocator);
1507 /* Free the pool itself */
1511 APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
1513 apr_abortfunc_t abort_fn,
1514 apr_allocator_t *allocator,
1515 const char *file_line)
1522 parent = global_pool;
1525 apr_pool_check_integrity(parent);
1528 allocator = parent->allocator;
1531 if (!abort_fn && parent)
1532 abort_fn = parent->abort_fn;
1534 if ((pool = malloc(SIZEOF_POOL_T)) == NULL) {
1536 abort_fn(APR_ENOMEM);
1541 memset(pool, 0, SIZEOF_POOL_T);
1543 pool->allocator = allocator;
1544 pool->abort_fn = abort_fn;
1545 pool->tag = file_line;
1546 pool->file_line = file_line;
1548 if ((pool->parent = parent) != NULL) {
1551 apr_thread_mutex_lock(parent->mutex);
1552 #endif /* APR_HAS_THREADS */
1553 if ((pool->sibling = parent->child) != NULL)
1554 pool->sibling->ref = &pool->sibling;
1556 parent->child = pool;
1557 pool->ref = &parent->child;
1561 apr_thread_mutex_unlock(parent->mutex);
1562 #endif /* APR_HAS_THREADS */
1565 pool->sibling = NULL;
1570 pool->owner = apr_os_thread_current();
1571 #endif /* APR_HAS_THREADS */
1573 if (parent == NULL || parent->allocator != allocator) {
1577 /* No matter what the creation flags say, always create
1578 * a lock. Without it integrity_check and apr_pool_num_bytes
1579 * blow up (because they traverse pools child lists that
1580 * possibly belong to another thread, in combination with
1581 * the pool having no lock). However, this might actually
1582 * hide problems like creating a child pool of a pool
1583 * belonging to another thread.
1585 if ((rv = apr_thread_mutex_create(&pool->mutex,
1586 APR_THREAD_MUTEX_NESTED, pool)) != APR_SUCCESS) {
1590 #endif /* APR_HAS_THREADS */
1595 pool->mutex = parent->mutex;
1596 #endif /* APR_HAS_THREADS */
1601 #if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
1602 apr_pool_log_event(pool, "CREATE", file_line, 1);
1603 #endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
1610 * "Print" functions (debug)
1613 struct psprintf_data {
1614 apr_vformatter_buff_t vbuff;
1619 static int psprintf_flush(apr_vformatter_buff_t *vbuff)
1621 struct psprintf_data *ps = (struct psprintf_data *)vbuff;
1624 size = ps->vbuff.curpos - ps->mem;
1627 if ((ps->mem = realloc(ps->mem, ps->size)) == NULL)
1630 ps->vbuff.curpos = ps->mem + size;
1631 ps->vbuff.endpos = ps->mem + ps->size - 1;
1636 APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *pool, const char *fmt, va_list ap)
1638 struct psprintf_data ps;
1641 apr_pool_check_integrity(pool);
1644 ps.mem = malloc(ps.size);
1645 ps.vbuff.curpos = ps.mem;
1647 /* Save a byte for the NUL terminator */
1648 ps.vbuff.endpos = ps.mem + ps.size - 1;
1650 if (apr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
1652 pool->abort_fn(APR_ENOMEM);
1657 *ps.vbuff.curpos++ = '\0';
1663 if (node == NULL || node->index == 64) {
1664 if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
1666 pool->abort_fn(APR_ENOMEM);
1671 node->next = pool->nodes;
1676 node->beginp[node->index] = ps.mem;
1677 node->endp[node->index] = ps.mem + ps.size;
1688 APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub)
1692 static int pool_find(apr_pool_t *pool, void *data)
1694 void **pmem = (void **)data;
1701 for (index = 0; index < node->index; index++) {
1702 if (node->beginp[index] <= *pmem
1703 && node->endp[index] > *pmem) {
1715 APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem)
1717 void *pool = (void *)mem;
1719 if (apr_pool_walk_tree(global_pool, pool_find, &pool))
1725 static int pool_num_bytes(apr_pool_t *pool, void *data)
1727 apr_size_t *psize = (apr_size_t *)data;
1734 for (index = 0; index < node->index; index++) {
1735 *psize += (char *)node->endp[index] - (char *)node->beginp[index];
1744 APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *pool, int recurse)
1746 apr_size_t size = 0;
1749 pool_num_bytes(pool, &size);
1754 apr_pool_walk_tree(pool, pool_num_bytes, &size);
1759 APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag)
1763 #endif /* !APR_POOL_DEBUG */
1767 * "Print" functions (common)
1770 APR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...)
1776 res = apr_pvsprintf(p, fmt, ap);
1785 APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abort_fn,
1788 pool->abort_fn = abort_fn;
1791 APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool)
1793 return pool->abort_fn;
1796 APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool)
1799 /* On NetWare, don't return the global_pool, return the application pool
1800 as the top most pool */
1801 if (pool->parent == global_pool)
1805 return pool->parent;
1808 APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool)
1810 return pool->allocator;
1813 /* return TRUE if a is an ancestor of b
1814 * NULL is considered an ancestor of all pools
1816 APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b)
1831 APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag)
1838 * User data management
1841 APR_DECLARE(apr_status_t) apr_pool_userdata_set(const void *data, const char *key,
1842 apr_status_t (*cleanup) (void *),
1846 apr_pool_check_integrity(pool);
1847 #endif /* APR_POOL_DEBUG */
1849 if (pool->user_data == NULL)
1850 pool->user_data = apr_hash_make(pool);
1852 if (apr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING) == NULL) {
1853 char *new_key = apr_pstrdup(pool, key);
1854 apr_hash_set(pool->user_data, new_key, APR_HASH_KEY_STRING, data);
1857 apr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
1861 apr_pool_cleanup_register(pool, data, cleanup, cleanup);
1866 APR_DECLARE(apr_status_t) apr_pool_userdata_setn(const void *data,
1868 apr_status_t (*cleanup)(void *),
1872 apr_pool_check_integrity(pool);
1873 #endif /* APR_POOL_DEBUG */
1875 if (pool->user_data == NULL)
1876 pool->user_data = apr_hash_make(pool);
1878 apr_hash_set(pool->user_data, key, APR_HASH_KEY_STRING, data);
1881 apr_pool_cleanup_register(pool, data, cleanup, cleanup);
1886 APR_DECLARE(apr_status_t) apr_pool_userdata_get(void **data, const char *key,
1890 apr_pool_check_integrity(pool);
1891 #endif /* APR_POOL_DEBUG */
1893 if (pool->user_data == NULL) {
1897 *data = apr_hash_get(pool->user_data, key, APR_HASH_KEY_STRING);
1909 struct cleanup_t *next;
1911 apr_status_t (*plain_cleanup_fn)(void *data);
1912 apr_status_t (*child_cleanup_fn)(void *data);
1915 APR_DECLARE(void) apr_pool_cleanup_register(apr_pool_t *p, const void *data,
1916 apr_status_t (*plain_cleanup_fn)(void *data),
1917 apr_status_t (*child_cleanup_fn)(void *data))
1922 apr_pool_check_integrity(p);
1923 #endif /* APR_POOL_DEBUG */
1926 c = (cleanup_t *)apr_palloc(p, sizeof(cleanup_t));
1928 c->plain_cleanup_fn = plain_cleanup_fn;
1929 c->child_cleanup_fn = child_cleanup_fn;
1930 c->next = p->cleanups;
1935 APR_DECLARE(void) apr_pool_cleanup_kill(apr_pool_t *p, const void *data,
1936 apr_status_t (*cleanup_fn)(void *))
1938 cleanup_t *c, **lastp;
1941 apr_pool_check_integrity(p);
1942 #endif /* APR_POOL_DEBUG */
1948 lastp = &p->cleanups;
1950 if (c->data == data && c->plain_cleanup_fn == cleanup_fn) {
1960 APR_DECLARE(void) apr_pool_child_cleanup_set(apr_pool_t *p, const void *data,
1961 apr_status_t (*plain_cleanup_fn)(void *),
1962 apr_status_t (*child_cleanup_fn)(void *))
1967 apr_pool_check_integrity(p);
1968 #endif /* APR_POOL_DEBUG */
1975 if (c->data == data && c->plain_cleanup_fn == plain_cleanup_fn) {
1976 c->child_cleanup_fn = child_cleanup_fn;
1984 APR_DECLARE(apr_status_t) apr_pool_cleanup_run(apr_pool_t *p, void *data,
1985 apr_status_t (*cleanup_fn)(void *))
1987 apr_pool_cleanup_kill(p, data, cleanup_fn);
1988 return (*cleanup_fn)(data);
1991 static void run_cleanups(cleanup_t **cref)
1993 cleanup_t *c = *cref;
1997 (*c->plain_cleanup_fn)((void *)c->data);
2002 static void run_child_cleanups(cleanup_t **cref)
2004 cleanup_t *c = *cref;
2008 (*c->child_cleanup_fn)((void *)c->data);
2013 static void cleanup_pool_for_exec(apr_pool_t *p)
2015 run_child_cleanups(&p->cleanups);
2017 for (p = p->child; p; p = p->sibling)
2018 cleanup_pool_for_exec(p);
2021 APR_DECLARE(void) apr_pool_cleanup_for_exec(void)
2023 #if !defined(WIN32) && !defined(OS2)
2025 * Don't need to do anything on NT or OS/2, because I
2026 * am actually going to spawn the new process - not
2027 * exec it. All handles that are not inheritable, will
2028 * be automajically closed. The only problem is with
2029 * file handles that are open, but there isn't much
2030 * I can do about that (except if the child decides
2031 * to go out and close them
2033 cleanup_pool_for_exec(global_pool);
2034 #endif /* !defined(WIN32) && !defined(OS2) */
2037 APR_DECLARE_NONSTD(apr_status_t) apr_pool_cleanup_null(void *data)
2039 /* do nothing cleanup routine */
2043 /* Subprocesses don't use the generic cleanup interface because
2044 * we don't want multiple subprocesses to result in multiple
2045 * three-second pauses; the subprocesses have to be "freed" all
2046 * at once. If other resources are introduced with the same property,
2047 * we might want to fold support for that into the generic interface.
2048 * For now, it's a special case.
2050 APR_DECLARE(void) apr_pool_note_subprocess(apr_pool_t *pool, apr_proc_t *proc,
2051 apr_kill_conditions_e how)
2053 struct process_chain *pc = apr_palloc(pool, sizeof(struct process_chain));
2057 pc->next = pool->subprocesses;
2058 pool->subprocesses = pc;
2061 static void free_proc_chain(struct process_chain *procs)
2063 /* Dispose of the subprocesses we've spawned off in the course of
2064 * whatever it was we're cleaning up now. This may involve killing
2065 * some of them off...
2067 struct process_chain *pc;
2068 int need_timeout = 0;
2069 apr_time_t timeout_interval;
2072 return; /* No work. Whew! */
2074 /* First, check to see if we need to do the SIGTERM, sleep, SIGKILL
2075 * dance with any of the processes we're cleaning up. If we've got
2076 * any kill-on-sight subprocesses, ditch them now as well, so they
2077 * don't waste any more cycles doing whatever it is that they shouldn't
2081 #ifndef NEED_WAITPID
2082 /* Pick up all defunct processes */
2083 for (pc = procs; pc; pc = pc->next) {
2084 if (apr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT) != APR_CHILD_NOTDONE)
2085 pc->kill_how = APR_KILL_NEVER;
2087 #endif /* !defined(NEED_WAITPID) */
2089 for (pc = procs; pc; pc = pc->next) {
2091 if ((pc->kill_how == APR_KILL_AFTER_TIMEOUT)
2092 || (pc->kill_how == APR_KILL_ONLY_ONCE)) {
2094 * Subprocess may be dead already. Only need the timeout if not.
2095 * Note: apr_proc_kill on Windows is TerminateProcess(), which is
2096 * similar to a SIGKILL, so always give the process a timeout
2097 * under Windows before killing it.
2099 if (apr_proc_kill(pc->proc, SIGTERM) == APR_SUCCESS)
2102 else if (pc->kill_how == APR_KILL_ALWAYS) {
2103 #else /* WIN32 knows only one fast, clean method of killing processes today */
2104 if (pc->kill_how != APR_KILL_NEVER) {
2106 pc->kill_how = APR_KILL_ALWAYS;
2108 apr_proc_kill(pc->proc, SIGKILL);
2112 /* Sleep only if we have to. The sleep algorithm grows
2113 * by a factor of two on each iteration. TIMEOUT_INTERVAL
2114 * is equal to TIMEOUT_USECS / 64.
2117 timeout_interval = TIMEOUT_INTERVAL;
2118 apr_sleep(timeout_interval);
2121 /* check the status of the subprocesses */
2123 for (pc = procs; pc; pc = pc->next) {
2124 if (pc->kill_how == APR_KILL_AFTER_TIMEOUT) {
2125 if (apr_proc_wait(pc->proc, NULL, NULL, APR_NOWAIT)
2126 == APR_CHILD_NOTDONE)
2127 need_timeout = 1; /* subprocess is still active */
2129 pc->kill_how = APR_KILL_NEVER; /* subprocess has exited */
2133 if (timeout_interval >= TIMEOUT_USECS) {
2136 apr_sleep(timeout_interval);
2137 timeout_interval *= 2;
2139 } while (need_timeout);
2142 /* OK, the scripts we just timed out for have had a chance to clean up
2143 * --- now, just get rid of them, and also clean up the system accounting
2146 for (pc = procs; pc; pc = pc->next) {
2147 if (pc->kill_how == APR_KILL_AFTER_TIMEOUT)
2148 apr_proc_kill(pc->proc, SIGKILL);
2151 /* Now wait for all the signaled processes to die */
2152 for (pc = procs; pc; pc = pc->next) {
2153 if (pc->kill_how != APR_KILL_NEVER)
2154 (void)apr_proc_wait(pc->proc, NULL, NULL, APR_WAIT);
2160 * Pool creation/destruction stubs, for people who are running
2161 * mixed release/debug enviroments.
2165 APR_DECLARE(void *) apr_palloc_debug(apr_pool_t *pool, apr_size_t size,
2166 const char *file_line)
2168 return apr_palloc(pool, size);
2171 APR_DECLARE(void *) apr_pcalloc_debug(apr_pool_t *pool, apr_size_t size,
2172 const char *file_line)
2174 return apr_pcalloc(pool, size);
2177 APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *pool,
2178 const char *file_line)
2180 apr_pool_clear(pool);
2183 APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
2184 const char *file_line)
2186 apr_pool_destroy(pool);
2189 APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
2191 apr_abortfunc_t abort_fn,
2192 apr_allocator_t *allocator,
2193 const char *file_line)
2195 return apr_pool_create_ex(newpool, parent, abort_fn, allocator);
2198 #else /* APR_POOL_DEBUG */
2201 APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size);
2203 APR_DECLARE(void *) apr_palloc(apr_pool_t *pool, apr_size_t size)
2205 return apr_palloc_debug(pool, size, "undefined");
2209 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size);
2211 APR_DECLARE(void *) apr_pcalloc(apr_pool_t *pool, apr_size_t size)
2213 return apr_pcalloc_debug(pool, size, "undefined");
2216 #undef apr_pool_clear
2217 APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool);
2219 APR_DECLARE(void) apr_pool_clear(apr_pool_t *pool)
2221 apr_pool_clear_debug(pool, "undefined");
2224 #undef apr_pool_destroy
2225 APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool);
2227 APR_DECLARE(void) apr_pool_destroy(apr_pool_t *pool)
2229 apr_pool_destroy_debug(pool, "undefined");
2232 #undef apr_pool_create_ex
2233 APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
2235 apr_abortfunc_t abort_fn,
2236 apr_allocator_t *allocator);
2238 APR_DECLARE(apr_status_t) apr_pool_create_ex(apr_pool_t **newpool,
2240 apr_abortfunc_t abort_fn,
2241 apr_allocator_t *allocator)
2243 return apr_pool_create_ex_debug(newpool, parent,
2244 abort_fn, allocator,
2248 #endif /* APR_POOL_DEBUG */
2251 APR_DECLARE(void) apr_allocator_set_max_free(apr_allocator_t *allocator,
2254 apr_allocator_max_free_set(allocator, size);
2258 APR_DECLARE(void) apr_pool_set_abort(apr_abortfunc_t abort_fn,
2261 apr_pool_abort_set(abort_fn, pool);
2265 APR_DECLARE(apr_abortfunc_t) apr_pool_get_abort(apr_pool_t *pool)
2267 return apr_pool_abort_get(pool);
2271 APR_DECLARE(apr_pool_t *) apr_pool_get_parent(apr_pool_t *pool)
2273 return apr_pool_parent_get(pool);
2277 APR_DECLARE(void) apr_allocator_set_owner(apr_allocator_t *allocator,
2280 apr_allocator_owner_set(allocator, pool);
2284 APR_DECLARE(apr_pool_t *) apr_allocator_get_owner(
2285 apr_allocator_t *allocator)
2287 return apr_allocator_owner_get(allocator);
2292 APR_DECLARE(apr_thread_mutex_t *) apr_allocator_get_mutex(
2293 apr_allocator_t *allocator)
2295 return apr_allocator_mutex_get(allocator);
2299 APR_DECLARE(void) apr_allocator_set_mutex(apr_allocator_t *allocator,
2300 apr_thread_mutex_t *mutex)
2302 apr_allocator_mutex_set(allocator, mutex);
2304 #endif /* APR_HAS_THREADS */