apr_allocator.h revision 253734
1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251875Speter * contributor license agreements.  See the NOTICE file distributed with
3251875Speter * this work for additional information regarding copyright ownership.
4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251875Speter * (the "License"); you may not use this file except in compliance with
6251875Speter * the License.  You may obtain a copy of the License at
7251875Speter *
8251875Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251875Speter *
10251875Speter * Unless required by applicable law or agreed to in writing, software
11251875Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251875Speter * See the License for the specific language governing permissions and
14251875Speter * limitations under the License.
15251875Speter */
16251875Speter
17251875Speter#ifndef APR_ALLOCATOR_H
18251875Speter#define APR_ALLOCATOR_H
19251875Speter
20251875Speter/**
21251875Speter * @file apr_allocator.h
22251875Speter * @brief APR Internal Memory Allocation
23251875Speter */
24251875Speter
25251875Speter#include "apr.h"
26251875Speter#include "apr_errno.h"
27251875Speter#define APR_WANT_MEMFUNC /**< For no good reason? */
28251875Speter#include "apr_want.h"
29251875Speter
30251875Speter#ifdef __cplusplus
31251875Speterextern "C" {
32251875Speter#endif
33251875Speter
34251875Speter/**
35251875Speter * @defgroup apr_allocator Internal Memory Allocation
36251875Speter * @ingroup APR
37251875Speter * @{
38251875Speter */
39251875Speter
40251875Speter/** the allocator structure */
41251875Spetertypedef struct apr_allocator_t apr_allocator_t;
42251875Speter/** the structure which holds information about the allocation */
43251875Spetertypedef struct apr_memnode_t apr_memnode_t;
44251875Speter
45251875Speter/** basic memory node structure
46251875Speter * @note The next, ref and first_avail fields are available for use by the
47251875Speter *       caller of apr_allocator_alloc(), the remaining fields are read-only.
48251875Speter *       The next field has to be used with caution and sensibly set when the
49251875Speter *       memnode is passed back to apr_allocator_free().  See apr_allocator_free()
50251875Speter *       for details.
51251875Speter *       The ref and first_avail fields will be properly restored by
52251875Speter *       apr_allocator_free().
53251875Speter */
54251875Speterstruct apr_memnode_t {
55251875Speter    apr_memnode_t *next;            /**< next memnode */
56251875Speter    apr_memnode_t **ref;            /**< reference to self */
57251875Speter    apr_uint32_t   index;           /**< size */
58251875Speter    apr_uint32_t   free_index;      /**< how much free */
59251875Speter    char          *first_avail;     /**< pointer to first free memory */
60251875Speter    char          *endp;            /**< pointer to end of free memory */
61251875Speter};
62251875Speter
63251875Speter/** The base size of a memory node - aligned.  */
64251875Speter#define APR_MEMNODE_T_SIZE APR_ALIGN_DEFAULT(sizeof(apr_memnode_t))
65251875Speter
66251875Speter/** Symbolic constants */
67251875Speter#define APR_ALLOCATOR_MAX_FREE_UNLIMITED 0
68251875Speter
69251875Speter/**
70251875Speter * Create a new allocator
71251875Speter * @param allocator The allocator we have just created.
72251875Speter *
73251875Speter */
74253734SpeterAPR_DECLARE(apr_status_t) apr_allocator_create(apr_allocator_t **allocator)
75253734Speter                          __attribute__((nonnull(1)));
76251875Speter
77251875Speter/**
78251875Speter * Destroy an allocator
79251875Speter * @param allocator The allocator to be destroyed
80251875Speter * @remark Any memnodes not given back to the allocator prior to destroying
81251875Speter *         will _not_ be free()d.
82251875Speter */
83253734SpeterAPR_DECLARE(void) apr_allocator_destroy(apr_allocator_t *allocator)
84253734Speter                  __attribute__((nonnull(1)));
85251875Speter
86251875Speter/**
87251875Speter * Allocate a block of mem from the allocator
88251875Speter * @param allocator The allocator to allocate from
89251875Speter * @param size The size of the mem to allocate (excluding the
90251875Speter *        memnode structure)
91251875Speter */
92251875SpeterAPR_DECLARE(apr_memnode_t *) apr_allocator_alloc(apr_allocator_t *allocator,
93253734Speter                                                 apr_size_t size)
94253734Speter                             __attribute__((nonnull(1)));
95251875Speter
96251875Speter/**
97251875Speter * Free a list of blocks of mem, giving them back to the allocator.
98251875Speter * The list is typically terminated by a memnode with its next field
99251875Speter * set to NULL.
100251875Speter * @param allocator The allocator to give the mem back to
101251875Speter * @param memnode The memory node to return
102251875Speter */
103251875SpeterAPR_DECLARE(void) apr_allocator_free(apr_allocator_t *allocator,
104253734Speter                                     apr_memnode_t *memnode)
105253734Speter                  __attribute__((nonnull(1,2)));
106251875Speter
107251875Speter#include "apr_pools.h"
108251875Speter
109251875Speter/**
110251875Speter * Set the owner of the allocator
111251875Speter * @param allocator The allocator to set the owner for
112251875Speter * @param pool The pool that is to own the allocator
113251875Speter * @remark Typically pool is the highest level pool using the allocator
114251875Speter */
115251875Speter/*
116251875Speter * XXX: see if we can come up with something a bit better.  Currently
117251875Speter * you can make a pool an owner, but if the pool doesn't use the allocator
118251875Speter * the allocator will never be destroyed.
119251875Speter */
120251875SpeterAPR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
121253734Speter                                          apr_pool_t *pool)
122253734Speter                  __attribute__((nonnull(1)));
123251875Speter
124251875Speter/**
125251875Speter * Get the current owner of the allocator
126251875Speter * @param allocator The allocator to get the owner from
127251875Speter */
128253734SpeterAPR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
129253734Speter                          __attribute__((nonnull(1)));
130251875Speter
131251875Speter/**
132251875Speter * Set the current threshold at which the allocator should start
133251875Speter * giving blocks back to the system.
134251875Speter * @param allocator The allocator the set the threshold on
135251875Speter * @param size The threshold.  0 == unlimited.
136251875Speter */
137251875SpeterAPR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
138253734Speter                                             apr_size_t size)
139253734Speter                  __attribute__((nonnull(1)));
140251875Speter
141251875Speter#include "apr_thread_mutex.h"
142251875Speter
143251875Speter#if APR_HAS_THREADS
144251875Speter/**
145251875Speter * Set a mutex for the allocator to use
146251875Speter * @param allocator The allocator to set the mutex for
147251875Speter * @param mutex The mutex
148251875Speter */
149251875SpeterAPR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
150253734Speter                                          apr_thread_mutex_t *mutex)
151253734Speter                  __attribute__((nonnull(1)));
152251875Speter
153251875Speter/**
154251875Speter * Get the mutex currently set for the allocator
155251875Speter * @param allocator The allocator
156251875Speter */
157251875SpeterAPR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
158253734Speter                                          apr_allocator_t *allocator)
159253734Speter                                  __attribute__((nonnull(1)));
160251875Speter
161251875Speter#endif /* APR_HAS_THREADS */
162251875Speter
163251875Speter/** @} */
164251875Speter
165251875Speter#ifdef __cplusplus
166251875Speter}
167251875Speter#endif
168251875Speter
169251875Speter#endif /* !APR_ALLOCATOR_H */
170