/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
* applicable.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "apr.h"
#include "apr_private.h"
#include "apr_atomic.h"
#include "apr_portable.h" /* for get_os_proc */
#include "apr_strings.h"
#include "apr_general.h"
#include "apr_pools.h"
#include "apr_allocator.h"
#include "apr_lib.h"
#include "apr_thread_mutex.h"
#include "apr_hash.h"
#include "apr_time.h"
#define APR_WANT_MEMFUNC
#include "apr_want.h"
#include "apr_env.h"
#if APR_HAVE_STDLIB_H
#include <stdlib.h> /* for malloc, free and abort */
#endif
#if APR_HAVE_UNISTD_H
#include <unistd.h> /* for getpid */
#endif
/*
* Magic numbers
*/
#define MIN_ALLOC 8192
#define MAX_INDEX 20
#define BOUNDARY_INDEX 12
#define BOUNDARY_SIZE (1 << BOUNDARY_INDEX)
/*
* Timing constants for killing subprocesses
* There is a total 3-second delay between sending a SIGINT
* and sending of the final SIGKILL.
* TIMEOUT_INTERVAL should be set to TIMEOUT_USECS / 64
* for the exponetial timeout alogrithm.
*/
#define TIMEOUT_USECS 3000000
#define TIMEOUT_INTERVAL 46875
/*
* Allocator
*/
struct apr_allocator_t {
apr_uint32_t max_index;
apr_uint32_t max_free_index;
apr_uint32_t current_free_index;
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex;
#endif /* APR_HAS_THREADS */
apr_pool_t *owner;
apr_memnode_t *free[MAX_INDEX];
};
#define SIZEOF_ALLOCATOR_T APR_ALIGN_DEFAULT(sizeof(apr_allocator_t))
/*
* Allocator
*/
APR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator)
{
apr_allocator_t *new_allocator;
*allocator = NULL;
if ((new_allocator = malloc(SIZEOF_ALLOCATOR_T)) == NULL)
return APR_ENOMEM;
memset(new_allocator, 0, SIZEOF_ALLOCATOR_T);
new_allocator->max_free_index = APR_ALLOCATOR_MAX_FREE_UNLIMITED;
*allocator = new_allocator;
return APR_SUCCESS;
}
APR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator)
{
apr_uint32_t index;
apr_memnode_t *node, **ref;
for (index = 0; index < MAX_INDEX; index++) {
ref = &allocator->free[index];
while ((node = *ref) != NULL) {
*ref = node->next;
free(node);
}
}
free(allocator);
}
#if APR_HAS_THREADS
APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
apr_thread_mutex_t *mutex)
{
allocator->mutex = mutex;
}
APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
apr_allocator_t *allocator)
{
return allocator->mutex;
}
#endif /* APR_HAS_THREADS */
APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
apr_pool_t *pool)
{
allocator->owner = pool;
}
APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
{
return allocator->owner;
}
APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
apr_size_t in_size)
{
apr_uint32_t max_free_index;
apr_uint32_t size = (APR_UINT32_TRUNC_CAST)in_size;
#if APR_HAS_THREADS
apr_thread_mutex_t *mutex;
mutex = apr_allocator_mutex_get(allocator);
if (mutex != NULL)
apr_thread_mutex_lock(mutex);
#endif /* APR_HAS_THREADS */
max_free_index = APR_ALIGN(size, BOUNDARY_SIZE) >> BOUNDARY_INDEX;
allocator->current_free_index += max_free_index;
allocator->current_free_index -= allocator->max_free_index;
allocator->max_free_index = max_free_index;
if (allocator->current_free_index > max_free_index)
allocator->current_free_index = max_free_index;
#if APR_HAS_THREADS
if (mutex != NULL)
apr_thread_mutex_unlock(mutex);
#endif
}
static APR_INLINE
apr_memnode_t *allocator_alloc(apr_allocator_t *allocator, apr_size_t size)
{
apr_memnode_t *node, **ref;
apr_uint32_t max_index;
apr_size_t i, index;
/* Round up the block size to the next boundary, but always
* allocate at least a certain size (MIN_ALLOC).
*/
size = APR_ALIGN(size + APR_MEMNODE_T_SIZE, BOUNDARY_SIZE);
if (size < MIN_ALLOC)
size = MIN_ALLOC;
/* Find the index for this node size by
* dividing its size by the boundary size
*/
index = (size >> BOUNDARY_INDEX) - 1;
if (index > APR_UINT32_MAX) {
return NULL;
}
/* First see if there are any nodes in the area we know
* our node will fit into.
*/
if (index <= allocator->max_index) {
#if APR_HAS_THREADS
if (allocator->mutex)
apr_thread_mutex_lock(allocator->mutex);
#endif /* APR_HAS_THREADS */
/* Walk the free list to see if there are
* any nodes on it of the requested size
*
* NOTE: an optimization would be to check
* allocator->free[index] first and if no
* node is present, directly use
* allocator->free[max_index]. This seems
* like overkill though and could cause
* memory waste.
*/
max_index = allocator->max_index;
ref = &allocator->free[index];
i = index;
while (*ref == NULL && i < max_index) {
ref++;
i++;
}
if ((node = *ref) != NULL) {
/* If we have found a node and it doesn't have any
* nodes waiting in line behind it _and_ we are on
* the highest available index, find the new highest
* available index
*/
if ((*ref = node->next) == NULL && i >= max_index) {
do {
ref--;
max_index--;
}
while (*ref == NULL && max_index > 0);
allocator->max_index = max_index;
}
allocator->current_free_index += node->index;
if (allocator->current_free_index > allocator->max_free_index)
allocator->current_free_index = allocator->max_free_index;
#if APR_HAS_THREADS
if (allocator->mutex)
apr_thread_mutex_unlock(allocator->mutex);
#endif /* APR_HAS_THREADS */
node->next = NULL;
node->first_avail = (char *)node + APR_MEMNODE_T_SIZE;
return node;
}
#if APR_HAS_THREADS
if (allocator->mutex)
apr_thread_mutex_unlock(allocator->mutex);
#endif /* APR_HAS_THREADS */
}
/* If we found nothing, seek the sink (at index 0), if
* it is not empty.
*/
else if (allocator->free[0]) {
#if APR_HAS_THREADS
if (allocator->mutex)
apr_thread_mutex_lock(allocator->mutex);
#endif /* APR_HAS_THREADS */
/* Walk the free list to see if there are
* any nodes on it of the requested size
*/
ref = &allocator->free[0];
while ((node = *ref) != NULL && index > node->index)
ref = &node->next;
if (node) {
*ref = node->next;
allocator->current_free_index += node->index;
if (allocator->current_free_index > allocator->max_free_index)
allocator->current_free_index = allocator->max_free_index;
#if APR_HAS_THREADS
if (allocator->mutex)
apr_thread_mutex_unlock(allocator->mutex);
#endif /* APR_HAS_THREADS */
node->next = NULL;
node->first_avail = (char *)node + A
没有合适的资源?快使用搜索试试~ 我知道了~
apr-1.2.7.tar.gz
5星 · 超过95%的资源 需积分: 10 61 下载量 123 浏览量
2008-07-09
13:31:58
上传
评论
收藏 1.05MB GZ 举报
温馨提示
共459个文件
c:245个
h:101个
in:11个
apr-1.2.7.tar.gz
资源推荐
资源详情
资源评论
收起资源包目录
apr-1.2.7.tar.gz (459个子文件)
win32ver.awk 4KB
make_exports.awk 3KB
make_nw_export.awk 2KB
make_var_export.awk 1023B
nw_ver.awk 668B
prebuildNW.bat 2KB
buildconf 3KB
apr_pools.c 59KB
jlibtool.c 46KB
apr_snprintf.c 41KB
apr_tables.c 38KB
sha2.c 37KB
filepath.c 36KB
proc.c 31KB
sendrecv.c 29KB
sockaddr.c 29KB
filestat.c 27KB
testfile.c 25KB
proc_mutex.c 24KB
sendfile.c 22KB
proc.c 19KB
open.c 19KB
shm.c 18KB
dso.c 18KB
aplibtool.c 18KB
testpoll.c 18KB
proc.c 17KB
readwrite.c 17KB
sockets.c 16KB
sendrecv.c 15KB
apr_hash.c 14KB
proc.c 14KB
errorcodes.c 13KB
readwrite.c 13KB
sockets.c 13KB
testhash.c 13KB
proc.c 13KB
signals.c 12KB
dir.c 12KB
filestat.c 12KB
sockopt.c 12KB
apr_atomic.c 12KB
apr_strings.c 11KB
select.c 11KB
getopt.c 11KB
teststr.c 11KB
time.c 10KB
time.c 10KB
filepath.c 10KB
abts.c 10KB
port.c 9KB
testlock.c 9KB
sockets.c 9KB
utf8.c 9KB
testfileinfo.c 9KB
testrand2.c 9KB
readwrite.c 9KB
multicast.c 9KB
userinfo.c 9KB
dir.c 9KB
apr_cpystrn.c 9KB
apr_random.c 9KB
testsock.c 9KB
testtime.c 9KB
shm.c 8KB
testatomic.c 8KB
testdir.c 8KB
sockopt.c 8KB
apr_fnmatch.c 8KB
pipe.c 8KB
kqueue.c 8KB
filestat.c 8KB
testlfs.c 8KB
testlockperf.c 8KB
misc.c 8KB
testshm.c 8KB
thread.c 8KB
epoll.c 8KB
testnames.c 8KB
open.c 8KB
thread.c 8KB
filesys.c 7KB
dso.c 7KB
poll.c 7KB
testdso.c 7KB
testipsub.c 7KB
filestat.c 7KB
thread.c 7KB
pipe.c 7KB
timestr.c 7KB
mktemp.c 6KB
inet_ntop.c 6KB
sendrecv.c 6KB
rand.c 6KB
thread.c 6KB
thread.c 6KB
filedup.c 6KB
otherchild.c 6KB
pollset.c 6KB
testargs.c 6KB
共 459 条
- 1
- 2
- 3
- 4
- 5
资源评论
- black_dk2014-11-07环境已搭建成功。谢分享
aboutibm
- 粉丝: 20
- 资源: 113
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功