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 #include "apr_pools.h"
00108 
00109 /**
00110  * Set the owner of the allocator
00111  * @param allocator The allocator to set the owner for
00112  * @param pool The pool that is to own the allocator
00113  * @remark Typically pool is the highest level pool using the allocator
00114  */
00115 /*
00116  * XXX: see if we can come up with something a bit better.  Currently
00117  * you can make a pool an owner, but if the pool doesn't use the allocator
00118  * the allocator will never be destroyed.
00119  */
00120 APR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
00121                                           apr_pool_t *pool)
00122                   __attribute__((nonnull(1)));
00123 
00124 /**
00125  * Get the current owner of the allocator
00126  * @param allocator The allocator to get the owner from
00127  */
00128 APR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
00129                           __attribute__((nonnull(1)));
00130 
00131 /**
00132  * Set the current threshold at which the allocator should start
00133  * giving blocks back to the system.
00134  * @param allocator The allocator the set the threshold on
00135  * @param size The threshold.  0 == unlimited.
00136  */
00137 APR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
00138                                              apr_size_t size)
00139                   __attribute__((nonnull(1)));
00140 
00141 #include "apr_thread_mutex.h"
00142 
00143 #if APR_HAS_THREADS
00144 /**
00145  * Set a mutex for the allocator to use
00146  * @param allocator The allocator to set the mutex for
00147  * @param mutex The mutex
00148  */
00149 APR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
00150                                           apr_thread_mutex_t *mutex)
00151                   __attribute__((nonnull(1)));
00152 
00153 /**
00154  * Get the mutex currently set for the allocator
00155  * @param allocator The allocator
00156  */
00157 APR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
00158                                           apr_allocator_t *allocator)
00159                                   __attribute__((nonnull(1)));
00160 
00161 #endif /* APR_HAS_THREADS */
00162 
00163 /** @} */
00164 
00165 #ifdef __cplusplus
00166 }
00167 #endif
00168 
00169 #endif /* !APR_ALLOCATOR_H */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines