Apache Portable Runtime

apr_allocator.h

Go to the documentation of this file.
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                           __attribute__((nonnull(1)));
00076 
00077 /**
00078  * Destroy an allocator
00079  * @param allocator The allocator to be destroyed
00080  * @remark Any memnodes not given back to the allocator prior to destroying
00081  *         will _not_ be free()d.
00082  */
00083 APR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator)
00084                   __attribute__((nonnull(1)));
00085 
00086 /**
00087  * Allocate a block of mem from the allocator
00088  * @param allocator The allocator to allocate from
00089  * @param size The size of the mem to allocate (excluding the
00090  *        memnode structure)
00091  */
00092 APR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator,
00093                                                  apr_size_t size)
00094                              __attribute__((nonnull(1)));
00095 
00096 /**
00097  * Free a list of blocks of mem, giving them back to the allocator.
00098  * The list is typically terminated by a memnode with its next field
00099  * set to NULL.
00100  * @param allocator The allocator to give the mem back to
00101  * @param memnode The memory node to return
00102  */
00103 APR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator,
00104                                      apr_memnode_t *memnode)
00105                   __attribute__((nonnull(1,2)));
00106  
00107 /**
00108  * Get the true size that would be allocated for the given size (including
00109  * the header and alignment).
00110  * @param list The allocator from which to the memory would be allocated
00111  * @param size The size to align
00112  * @return The aligned size (or zero on apr_size_t overflow)
00113  */
00114 APR_DECLARE(apr_size_t) apr_allocator_align(apr_allocator_t *allocator,
00115                                             apr_size_t size);
00116 
00117 #include "apr_pools.h"
00118 
00119 /**
00120  * Set the owner of the allocator
00121  * @param allocator The allocator to set the owner for
00122  * @param pool The pool that is to own the allocator
00123  * @remark Typically pool is the highest level pool using the allocator
00124  */
00125 /*
00126  * XXX: see if we can come up with something a bit better.  Currently
00127  * you can make a pool an owner, but if the pool doesn't use the allocator
00128  * the allocator will never be destroyed.
00129  */
00130 APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
00131                                           apr_pool_t *pool)
00132                   __attribute__((nonnull(1)));
00133 
00134 /**
00135  * Get the current owner of the allocator
00136  * @param allocator The allocator to get the owner from
00137  */
00138 APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
00139                           __attribute__((nonnull(1)));
00140 
00141 /**
00142  * Set the current threshold at which the allocator should start
00143  * giving blocks back to the system.
00144  * @param allocator The allocator to set the threshold on
00145  * @param size The threshold.  0 == unlimited.
00146  */
00147 APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
00148                                              apr_size_t size)
00149                   __attribute__((nonnull(1)));
00150 
00151 #include "apr_thread_mutex.h"
00152 
00153 #if APR_HAS_THREADS
00154 /**
00155  * Set a mutex for the allocator to use
00156  * @param allocator The allocator to set the mutex for
00157  * @param mutex The mutex
00158  */
00159 APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
00160                                           apr_thread_mutex_t *mutex)
00161                   __attribute__((nonnull(1)));
00162 
00163 /**
00164  * Get the mutex currently set for the allocator
00165  * @param allocator The allocator
00166  */
00167 APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
00168                                           apr_allocator_t *allocator)
00169                                   __attribute__((nonnull(1)));
00170 
00171 #endif /* APR_HAS_THREADS */
00172 
00173 /** @} */
00174 
00175 #ifdef __cplusplus
00176 }
00177 #endif
00178 
00179 #endif /* !APR_ALLOCATOR_H */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines