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)));
106362181Sdim
107362181Sdim/**
108362181Sdim * Get the true size that would be allocated for the given size (including
109362181Sdim * the header and alignment).
110362181Sdim * @param allocator The allocator from which to the memory would be allocated
111362181Sdim * @param size The size to align
112362181Sdim * @return The aligned size (or zero on apr_size_t overflow)
113362181Sdim */
114362181SdimAPR_DECLARE(apr_size_t) apr_allocator_align(apr_allocator_t *allocator,
115362181Sdim                                            apr_size_t size);
116251875Speter
117251875Speter#include "apr_pools.h"
118251875Speter
119251875Speter/**
120251875Speter * Set the owner of the allocator
121251875Speter * @param allocator The allocator to set the owner for
122251875Speter * @param pool The pool that is to own the allocator
123251875Speter * @remark Typically pool is the highest level pool using the allocator
124251875Speter */
125251875Speter/*
126251875Speter * XXX: see if we can come up with something a bit better.  Currently
127251875Speter * you can make a pool an owner, but if the pool doesn't use the allocator
128251875Speter * the allocator will never be destroyed.
129251875Speter */
130251875SpeterAPR_DECLARE(void) apr_allocator_owner_set(apr_allocator_t *allocator,
131253734Speter                                          apr_pool_t *pool)
132253734Speter                  __attribute__((nonnull(1)));
133251875Speter
134251875Speter/**
135251875Speter * Get the current owner of the allocator
136251875Speter * @param allocator The allocator to get the owner from
137251875Speter */
138253734SpeterAPR_DECLARE(apr_pool_t *) apr_allocator_owner_get(apr_allocator_t *allocator)
139253734Speter                          __attribute__((nonnull(1)));
140251875Speter
141251875Speter/**
142251875Speter * Set the current threshold at which the allocator should start
143251875Speter * giving blocks back to the system.
144266735Speter * @param allocator The allocator to set the threshold on
145251875Speter * @param size The threshold.  0 == unlimited.
146251875Speter */
147251875SpeterAPR_DECLARE(void) apr_allocator_max_free_set(apr_allocator_t *allocator,
148253734Speter                                             apr_size_t size)
149253734Speter                  __attribute__((nonnull(1)));
150251875Speter
151251875Speter#include "apr_thread_mutex.h"
152251875Speter
153251875Speter#if APR_HAS_THREADS
154251875Speter/**
155251875Speter * Set a mutex for the allocator to use
156251875Speter * @param allocator The allocator to set the mutex for
157251875Speter * @param mutex The mutex
158251875Speter */
159251875SpeterAPR_DECLARE(void) apr_allocator_mutex_set(apr_allocator_t *allocator,
160253734Speter                                          apr_thread_mutex_t *mutex)
161253734Speter                  __attribute__((nonnull(1)));
162251875Speter
163251875Speter/**
164251875Speter * Get the mutex currently set for the allocator
165251875Speter * @param allocator The allocator
166251875Speter */
167251875SpeterAPR_DECLARE(apr_thread_mutex_t *) apr_allocator_mutex_get(
168253734Speter                                          apr_allocator_t *allocator)
169253734Speter                                  __attribute__((nonnull(1)));
170251875Speter
171251875Speter#endif /* APR_HAS_THREADS */
172251875Speter
173251875Speter/** @} */
174251875Speter
175251875Speter#ifdef __cplusplus
176251875Speter}
177251875Speter#endif
178251875Speter
179251875Speter#endif /* !APR_ALLOCATOR_H */
180