00001 /* Licensed to the Apache Software Foundation (ASF) under one or more 00002 * contributor license agreements. See the NOTICE file distributed with 00003 * this work for additional information regarding copyright ownership. 00004 * The ASF licenses this file to You under the Apache License, Version 2.0 00005 * (the "License"); you may not use this file except in compliance with 00006 * the License. You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef APR_ALLOCATOR_H 00018 #define APR_ALLOCATOR_H 00019 00020 /** 00021 * @file apr_allocator.h 00022 * @brief APR Internal Memory Allocation 00023 */ 00024 00025 #include "apr.h" 00026 #include "apr_errno.h" 00027 #define APR_WANT_MEMFUNC /**< For no good reason? */ 00028 #include "apr_want.h" 00029 00030 #ifdef __cplusplus 00031 extern "C" { 00032 #endif 00033 00034 /** 00035 * @defgroup apr_allocator Internal Memory Allocation 00036 * @ingroup APR 00037 * @{ 00038 */ 00039 00040 /** the allocator structure */ 00041 typedef struct apr_allocator_t apr_allocator_t; 00042 /** the structure which holds information about the allocation */ 00043 typedef struct apr_memnode_t apr_memnode_t; 00044 00045 /** basic memory node structure 00046 * @note The next, ref and first_avail fields are available for use by the 00047 * caller of apr_allocator_alloc(), the remaining fields are read-only. 00048 * The next field has to be used with caution and sensibly set when the 00049 * memnode is passed back to apr_allocator_free(). See apr_allocator_free() 00050 * for details. 00051 * The ref and first_avail fields will be properly restored by 00052 * apr_allocator_free(). 00053 */ 00054 struct apr_memnode_t { 00055 apr_memnode_t *next; /**< next memnode */ 00056 apr_memnode_t **ref; /**< reference to self */ 00057 apr_uint32_t index; /**< size */ 00058 apr_uint32_t free_index; /**< how much free */ 00059 char *first_avail; /**< pointer to first free memory */ 00060 char *endp; /**< pointer to end of free memory */ 00061 }; 00062 00063 /** The base size of a memory node - aligned. */ 00064 #define APR_MEMNODE_T_SIZE APR_ALIGN_DEFAULT(sizeof(apr_memnode_t)) 00065 00066 /** Symbolic constants */ 00067 #define APR_ALLOCATOR_MAX_FREE_UNLIMITED 0 00068 00069 /** 00070 * Create a new allocator 00071 * @param allocator The allocator we have just created. 00072 * 00073 */ 00074 APR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator); 00075 00076 /** 00077 * Destroy an allocator 00078 * @param allocator The allocator to be destroyed 00079 * @remark Any memnodes not given back to the allocator prior to destroying 00080 * will _not_ be free()d. 00081 */ 00082 APR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator); 00083 00084 /** 00085 * Allocate a block of mem from the allocator 00086 * @param allocator The allocator to allocate from 00087 * @param size The size of the mem to allocate (excluding the 00088 * memnode structure) 00089 */ 00090 APR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator, 00091 apr_size_t size); 00092 00093 /** 00094 * Free a list of blocks of mem, giving them back to the allocator. 00095 * The list is typically terminated by a memnode with its next field 00096 * set to NULL. 00097 * @param allocator The allocator to give the mem back to 00098 * @param memnode The memory node to return 00099 */ 00100 APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator, 00101 apr_memnode_t *memnode); 00102 00103 #include "apr_pools.h" 00104 00105 /** 00106 * Set the owner of the allocator 00107 * @param allocator The allocator to set the owner for 00108 * @param pool The pool that is to own the allocator 00109 * @remark Typically pool is the highest level pool using the allocator 00110 */ 00111 /* 00112 * XXX: see if we can come up with something a bit better. Currently 00113 * you can make a pool an owner, but if the pool doesn't use the allocator 00114 * the allocator will never be destroyed. 00115 */ 00116 APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator, 00117 apr_pool_t *pool); 00118 00119 /** 00120 * Get the current owner of the allocator 00121 * @param allocator The allocator to get the owner from 00122 */ 00123 APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator); 00124 00125 /** 00126 * Set the current threshold at which the allocator should start 00127 * giving blocks back to the system. 00128 * @param allocator The allocator the set the threshold on 00129 * @param size The threshold. 0 == unlimited. 00130 */ 00131 APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator, 00132 apr_size_t size); 00133 00134 #include "apr_thread_mutex.h" 00135 00136 #if APR_HAS_THREADS 00137 /** 00138 * Set a mutex for the allocator to use 00139 * @param allocator The allocator to set the mutex for 00140 * @param mutex The mutex 00141 */ 00142 APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator, 00143 apr_thread_mutex_t *mutex); 00144 00145 /** 00146 * Get the mutex currently set for the allocator 00147 * @param allocator The allocator 00148 */ 00149 APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get( 00150 apr_allocator_t *allocator); 00151 00152 #endif /* APR_HAS_THREADS */ 00153 00154 /** @} */ 00155 00156 #ifdef __cplusplus 00157 } 00158 #endif 00159 00160 #endif /* !APR_ALLOCATOR_H */