apr_pools.c
来自「Apache 2.0.63 is the current stable vers」· C语言 代码 · 共 2,297 行 · 第 1/5 页
C
2,297 行
{
debug_node_t *node;
apr_uint32_t index;
/* Destroy the subpools. The subpools will detach themselves from
* this pool thus this loop is safe and easy.
*/
while (pool->child)
apr_pool_destroy_debug(pool->child, file_line);
/* Run cleanups */
run_cleanups(&pool->cleanups);
pool->cleanups = NULL;
/* Free subprocesses */
free_proc_chain(pool->subprocesses);
pool->subprocesses = NULL;
/* Clear the user data. */
pool->user_data = NULL;
/* Free the blocks, scribbling over them first to help highlight
* use-after-free issues. */
while ((node = pool->nodes) != NULL) {
pool->nodes = node->next;
for (index = 0; index < node->index; index++) {
memset(node->beginp[index], POOL_POISON_BYTE,
node->endp[index] - node->beginp[index]);
free(node->beginp[index]);
}
memset(node, POOL_POISON_BYTE, SIZEOF_DEBUG_NODE_T);
free(node);
}
pool->stat_alloc = 0;
pool->stat_clear++;
}
APR_DECLARE(void) apr_pool_clear_debug(apr_pool_t *pool,
const char *file_line)
{
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex = NULL;
#endif
apr_pool_check_integrity(pool);
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
apr_pool_log_event(pool, "CLEAR", file_line, 1);
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
#if APR_HAS_THREADS
if (pool->parent != NULL)
mutex = pool->parent->mutex;
/* Lock the parent mutex before clearing so that if we have our
* own mutex it won't be accessed by apr_pool_walk_tree after
* it has been destroyed.
*/
if (mutex != NULL && mutex != pool->mutex) {
apr_thread_mutex_lock(mutex);
}
#endif
pool_clear_debug(pool, file_line);
#if APR_HAS_THREADS
/* If we had our own mutex, it will have been destroyed by
* the registered cleanups. Recreate the mutex. Unlock
* the mutex we obtained above.
*/
if (mutex != pool->mutex) {
(void)apr_thread_mutex_create(&pool->mutex,
APR_THREAD_MUTEX_NESTED, pool);
if (mutex != NULL)
(void)apr_thread_mutex_unlock(mutex);
}
#endif /* APR_HAS_THREADS */
}
APR_DECLARE(void) apr_pool_destroy_debug(apr_pool_t *pool,
const char *file_line)
{
apr_pool_check_integrity(pool);
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
apr_pool_log_event(pool, "DESTROY", file_line, 1);
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
pool_clear_debug(pool, file_line);
/* Remove the pool from the parents child list */
if (pool->parent) {
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex;
if ((mutex = pool->parent->mutex) != NULL)
apr_thread_mutex_lock(mutex);
#endif /* APR_HAS_THREADS */
if ((*pool->ref = pool->sibling) != NULL)
pool->sibling->ref = pool->ref;
#if APR_HAS_THREADS
if (mutex)
apr_thread_mutex_unlock(mutex);
#endif /* APR_HAS_THREADS */
}
if (pool->allocator != NULL
&& apr_allocator_owner_get(pool->allocator) == pool) {
apr_allocator_destroy(pool->allocator);
}
/* Free the pool itself */
free(pool);
}
APR_DECLARE(apr_status_t) apr_pool_create_ex_debug(apr_pool_t **newpool,
apr_pool_t *parent,
apr_abortfunc_t abort_fn,
apr_allocator_t *allocator,
const char *file_line)
{
apr_pool_t *pool;
*newpool = NULL;
if (!parent) {
parent = global_pool;
}
else {
apr_pool_check_integrity(parent);
if (!allocator)
allocator = parent->allocator;
}
if (!abort_fn && parent)
abort_fn = parent->abort_fn;
if ((pool = malloc(SIZEOF_POOL_T)) == NULL) {
if (abort_fn)
abort_fn(APR_ENOMEM);
return APR_ENOMEM;
}
memset(pool, 0, SIZEOF_POOL_T);
pool->allocator = allocator;
pool->abort_fn = abort_fn;
pool->tag = file_line;
pool->file_line = file_line;
if ((pool->parent = parent) != NULL) {
#if APR_HAS_THREADS
if (parent->mutex)
apr_thread_mutex_lock(parent->mutex);
#endif /* APR_HAS_THREADS */
if ((pool->sibling = parent->child) != NULL)
pool->sibling->ref = &pool->sibling;
parent->child = pool;
pool->ref = &parent->child;
#if APR_HAS_THREADS
if (parent->mutex)
apr_thread_mutex_unlock(parent->mutex);
#endif /* APR_HAS_THREADS */
}
else {
pool->sibling = NULL;
pool->ref = NULL;
}
#if APR_HAS_THREADS
pool->owner = apr_os_thread_current();
#endif /* APR_HAS_THREADS */
if (parent == NULL || parent->allocator != allocator) {
#if APR_HAS_THREADS
apr_status_t rv;
/* No matter what the creation flags say, always create
* a lock. Without it integrity_check and apr_pool_num_bytes
* blow up (because they traverse pools child lists that
* possibly belong to another thread, in combination with
* the pool having no lock). However, this might actually
* hide problems like creating a child pool of a pool
* belonging to another thread.
*/
if ((rv = apr_thread_mutex_create(&pool->mutex,
APR_THREAD_MUTEX_NESTED, pool)) != APR_SUCCESS) {
free(pool);
return rv;
}
#endif /* APR_HAS_THREADS */
}
else {
#if APR_HAS_THREADS
if (parent)
pool->mutex = parent->mutex;
#endif /* APR_HAS_THREADS */
}
*newpool = pool;
#if (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE)
apr_pool_log_event(pool, "CREATE", file_line, 1);
#endif /* (APR_POOL_DEBUG & APR_POOL_DEBUG_VERBOSE) */
return APR_SUCCESS;
}
/*
* "Print" functions (debug)
*/
struct psprintf_data {
apr_vformatter_buff_t vbuff;
char *mem;
apr_size_t size;
};
static int psprintf_flush(apr_vformatter_buff_t *vbuff)
{
struct psprintf_data *ps = (struct psprintf_data *)vbuff;
apr_size_t size;
size = ps->vbuff.curpos - ps->mem;
ps->size <<= 1;
if ((ps->mem = realloc(ps->mem, ps->size)) == NULL)
return -1;
ps->vbuff.curpos = ps->mem + size;
ps->vbuff.endpos = ps->mem + ps->size - 1;
return 0;
}
APR_DECLARE(char *) apr_pvsprintf(apr_pool_t *pool, const char *fmt, va_list ap)
{
struct psprintf_data ps;
debug_node_t *node;
apr_pool_check_integrity(pool);
ps.size = 64;
ps.mem = malloc(ps.size);
ps.vbuff.curpos = ps.mem;
/* Save a byte for the NUL terminator */
ps.vbuff.endpos = ps.mem + ps.size - 1;
if (apr_vformatter(psprintf_flush, &ps.vbuff, fmt, ap) == -1) {
if (pool->abort_fn)
pool->abort_fn(APR_ENOMEM);
return NULL;
}
*ps.vbuff.curpos++ = '\0';
/*
* Link the node in
*/
node = pool->nodes;
if (node == NULL || node->index == 64) {
if ((node = malloc(SIZEOF_DEBUG_NODE_T)) == NULL) {
if (pool->abort_fn)
pool->abort_fn(APR_ENOMEM);
return NULL;
}
node->next = pool->nodes;
pool->nodes = node;
node->index = 0;
}
node->beginp[node->index] = ps.mem;
node->endp[node->index] = ps.mem + ps.size;
node->index++;
return ps.mem;
}
/*
* Debug functions
*/
APR_DECLARE(void) apr_pool_join(apr_pool_t *p, apr_pool_t *sub)
{
}
static int pool_find(apr_pool_t *pool, void *data)
{
void **pmem = (void **)data;
debug_node_t *node;
apr_uint32_t index;
node = pool->nodes;
while (node) {
for (index = 0; index < node->index; index++) {
if (node->beginp[index] <= *pmem
&& node->endp[index] > *pmem) {
*pmem = pool;
return 1;
}
}
node = node->next;
}
return 0;
}
APR_DECLARE(apr_pool_t *) apr_pool_find(const void *mem)
{
void *pool = (void *)mem;
if (apr_pool_walk_tree(global_pool, pool_find, &pool))
return pool;
return NULL;
}
static int pool_num_bytes(apr_pool_t *pool, void *data)
{
apr_size_t *psize = (apr_size_t *)data;
debug_node_t *node;
apr_uint32_t index;
node = pool->nodes;
while (node) {
for (index = 0; index < node->index; index++) {
*psize += (char *)node->endp[index] - (char *)node->beginp[index];
}
node = node->next;
}
return 0;
}
APR_DECLARE(apr_size_t) apr_pool_num_bytes(apr_pool_t *pool, int recurse)
{
apr_size_t size = 0;
if (!recurse) {
pool_num_bytes(pool, &size);
return size;
}
apr_pool_walk_tree(pool, pool_num_bytes, &size);
return size;
}
APR_DECLARE(void) apr_pool_lock(apr_pool_t *pool, int flag)
{
}
#endif /* !APR_POOL_DEBUG */
/*
* "Print" functions (common)
*/
APR_DECLARE_NONSTD(char *) apr_psprintf(apr_pool_t *p, const char *fmt, ...)
{
va_list ap;
char *res;
va_start(ap, fmt);
res = apr_pvsprintf(p, fmt, ap);
va_end(ap);
return res;
}
/*
* Pool Properties
*/
APR_DECLARE(void) apr_pool_abort_set(apr_abortfunc_t abort_fn,
apr_pool_t *pool)
{
pool->abort_fn = abort_fn;
}
APR_DECLARE(apr_abortfunc_t) apr_pool_abort_get(apr_pool_t *pool)
{
return pool->abort_fn;
}
APR_DECLARE(apr_pool_t *) apr_pool_parent_get(apr_pool_t *pool)
{
#ifdef NETWARE
/* On NetWare, don't return the global_pool, return the application pool
as the top most pool */
if (pool->parent == global_pool)
return NULL;
else
#endif
return pool->parent;
}
APR_DECLARE(apr_allocator_t *) apr_pool_allocator_get(apr_pool_t *pool)
{
return pool->allocator;
}
/* return TRUE if a is an ancestor of b
* NULL is considered an ancestor of all pools
*/
APR_DECLARE(int) apr_pool_is_ancestor(apr_pool_t *a, apr_pool_t *b)
{
if (a == NULL)
return 1;
while (b) {
if (a == b)
return 1;
b = b->parent;
}
return 0;
}
APR_DECLARE(void) apr_pool_tag(apr_pool_t *pool, const char *tag)
{
pool->tag = tag;
}
/*
* User data management
*/
APR_DECLARE(apr_status_t) apr_pool_userdata_set(const void *data, const char *key,
apr_status_t (*cleanup) (void *),
apr_pool_t *pool)
{
#if APR_POOL_DEBUG
apr_pool_check_integrity(pool);
#endif /* APR_POOL_DEBUG */
if (pool->user_data == NULL)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?