1238106Sdes/*
2238106Sdes * regional.h -- region based memory allocator.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes * Regional allocator. Allocates small portions of of larger chunks.
39238106Sdes * Based on region-allocator from NSD, but rewritten to be light.
40238106Sdes *
41238106Sdes * Different from (nsd) region-allocator.h
42238106Sdes * 	o does not have recycle bin
43238106Sdes * 	o does not collect stats; just enough to answer get_mem() in use.
44238106Sdes * 	o does not keep cleanup list
45238106Sdes * 	o does not have function pointers to setup
46238106Sdes * 	o allocs the regional struct inside the first block.
47238106Sdes * 	o can take a block to create regional from.
48238106Sdes * 	o blocks and large allocations are kept on singly linked lists.
49238106Sdes */
50238106Sdes
51238106Sdes#ifndef UTIL_REGIONAL_H_
52238106Sdes#define UTIL_REGIONAL_H_
53238106Sdes
54238106Sdes/**
55238106Sdes * the regional* is the first block*.
56238106Sdes * every block has a ptr to the next in first bytes.
57238106Sdes * and so does the regional struct, which is the first block.
58238106Sdes */
59238106Sdesstruct regional
60238106Sdes{
61238106Sdes	/**
62238106Sdes	 * next chunk. NULL if first chunk is the only chunk.
63238106Sdes	 * first inside that chunk is the char* next pointer.
64238106Sdes	 * When regional_free_all() has been called this value is NULL.
65238106Sdes	 */
66238106Sdes	char* next;
67238106Sdes	/** first large object, cast to char** to obtain next ptr */
68238106Sdes	char* large_list;
69238106Sdes	/** total large size */
70238106Sdes	size_t total_large;
71238106Sdes	/** initial chunk size */
72238106Sdes	size_t first_size;
73238106Sdes	/** number of bytes available in the current chunk. */
74238106Sdes	size_t available;
75238106Sdes	/** current chunk data position. */
76238106Sdes	char* data;
77238106Sdes};
78238106Sdes
79238106Sdes/**
80238106Sdes * Create a new regional.
81238106Sdes * @return: newly allocated regional.
82238106Sdes */
83238106Sdesstruct regional* regional_create(void);
84238106Sdes
85238106Sdes/**
86238106Sdes * Create a new region, with custom settings.
87238106Sdes * @param size: length of first block.
88238106Sdes * @return: newly allocated regional.
89238106Sdes */
90238106Sdesstruct regional* regional_create_custom(size_t size);
91238106Sdes
92238106Sdes/**
93238106Sdes * Free all memory associated with regional. Only keeps the first block with
94238106Sdes * the regional inside it.
95238106Sdes * @param r: the region.
96238106Sdes */
97238106Sdesvoid regional_free_all(struct regional *r);
98238106Sdes
99238106Sdes/**
100238106Sdes * Destroy regional.  All memory associated with regional is freed as if
101238106Sdes * regional_free_all was called, as well as destroying the regional struct.
102238106Sdes * @param r: to delete.
103238106Sdes */
104238106Sdesvoid regional_destroy(struct regional *r);
105238106Sdes
106238106Sdes/**
107238106Sdes * Allocate size bytes of memory inside regional.  The memory is
108238106Sdes * deallocated when region_free_all is called for this region.
109238106Sdes * @param r: the region.
110238106Sdes * @param size: number of bytes.
111238106Sdes * @return: pointer to memory allocated.
112238106Sdes */
113238106Sdesvoid *regional_alloc(struct regional *r, size_t size);
114238106Sdes
115238106Sdes/**
116238106Sdes * Allocate size bytes of memory inside regional and copy INIT into it.
117238106Sdes * The memory is deallocated when region_free_all is called for this
118238106Sdes * region.
119238106Sdes * @param r: the region.
120238106Sdes * @param init: to copy.
121238106Sdes * @param size: number of bytes.
122238106Sdes * @return: pointer to memory allocated.
123238106Sdes */
124238106Sdesvoid *regional_alloc_init(struct regional* r, const void *init, size_t size);
125238106Sdes
126238106Sdes/**
127238106Sdes * Allocate size bytes of memory inside regional that are initialized to
128238106Sdes * 0.  The memory is deallocated when region_free_all is called for
129238106Sdes * this region.
130238106Sdes * @param r: the region.
131238106Sdes * @param size: number of bytes.
132238106Sdes * @return: pointer to memory allocated.
133238106Sdes */
134238106Sdesvoid *regional_alloc_zero(struct regional *r, size_t size);
135238106Sdes
136238106Sdes/**
137238106Sdes * Duplicate string and allocate the result in regional.
138238106Sdes * @param r: the region.
139238106Sdes * @param string: null terminated string.
140238106Sdes * @return: pointer to memory allocated.
141238106Sdes */
142238106Sdeschar *regional_strdup(struct regional *r, const char *string);
143238106Sdes
144238106Sdes/** Debug print regional statistics to log */
145238106Sdesvoid regional_log_stats(struct regional *r);
146238106Sdes
147238106Sdes/** get total memory size in use by region */
148238106Sdessize_t regional_get_mem(struct regional* r);
149238106Sdes
150238106Sdes#endif /* UTIL_REGIONAL_H_ */
151