regional.h revision 1.1.1.1.2.2 1 1.1.1.1.2.2 pgoyette /*
2 1.1.1.1.2.2 pgoyette * regional.h -- region based memory allocator.
3 1.1.1.1.2.2 pgoyette *
4 1.1.1.1.2.2 pgoyette * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 1.1.1.1.2.2 pgoyette *
6 1.1.1.1.2.2 pgoyette * This software is open source.
7 1.1.1.1.2.2 pgoyette *
8 1.1.1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
9 1.1.1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
10 1.1.1.1.2.2 pgoyette * are met:
11 1.1.1.1.2.2 pgoyette *
12 1.1.1.1.2.2 pgoyette * Redistributions of source code must retain the above copyright notice,
13 1.1.1.1.2.2 pgoyette * this list of conditions and the following disclaimer.
14 1.1.1.1.2.2 pgoyette *
15 1.1.1.1.2.2 pgoyette * Redistributions in binary form must reproduce the above copyright notice,
16 1.1.1.1.2.2 pgoyette * this list of conditions and the following disclaimer in the documentation
17 1.1.1.1.2.2 pgoyette * and/or other materials provided with the distribution.
18 1.1.1.1.2.2 pgoyette *
19 1.1.1.1.2.2 pgoyette * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1.1.1.2.2 pgoyette * be used to endorse or promote products derived from this software without
21 1.1.1.1.2.2 pgoyette * specific prior written permission.
22 1.1.1.1.2.2 pgoyette *
23 1.1.1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1.1.1.2.2 pgoyette * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1.1.1.2.2 pgoyette * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1.1.1.2.2 pgoyette * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1.1.1.2.2 pgoyette * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1.1.1.2.2 pgoyette * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1.1.1.2.2 pgoyette * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1.1.1.2.2 pgoyette * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1.1.1.2.2 pgoyette * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1.1.1.2.2 pgoyette * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1.1.1.2.2 pgoyette * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1.1.1.2.2 pgoyette */
35 1.1.1.1.2.2 pgoyette
36 1.1.1.1.2.2 pgoyette /**
37 1.1.1.1.2.2 pgoyette * \file
38 1.1.1.1.2.2 pgoyette * Regional allocator. Allocates small portions of of larger chunks.
39 1.1.1.1.2.2 pgoyette * Based on region-allocator from NSD, but rewritten to be light.
40 1.1.1.1.2.2 pgoyette *
41 1.1.1.1.2.2 pgoyette * Different from (nsd) region-allocator.h
42 1.1.1.1.2.2 pgoyette * o does not have recycle bin
43 1.1.1.1.2.2 pgoyette * o does not collect stats; just enough to answer get_mem() in use.
44 1.1.1.1.2.2 pgoyette * o does not keep cleanup list
45 1.1.1.1.2.2 pgoyette * o does not have function pointers to setup
46 1.1.1.1.2.2 pgoyette * o allocs the regional struct inside the first block.
47 1.1.1.1.2.2 pgoyette * o can take a block to create regional from.
48 1.1.1.1.2.2 pgoyette * o blocks and large allocations are kept on singly linked lists.
49 1.1.1.1.2.2 pgoyette */
50 1.1.1.1.2.2 pgoyette
51 1.1.1.1.2.2 pgoyette #ifndef UTIL_REGIONAL_H_
52 1.1.1.1.2.2 pgoyette #define UTIL_REGIONAL_H_
53 1.1.1.1.2.2 pgoyette
54 1.1.1.1.2.2 pgoyette /**
55 1.1.1.1.2.2 pgoyette * the regional* is the first block*.
56 1.1.1.1.2.2 pgoyette * every block has a ptr to the next in first bytes.
57 1.1.1.1.2.2 pgoyette * and so does the regional struct, which is the first block.
58 1.1.1.1.2.2 pgoyette */
59 1.1.1.1.2.2 pgoyette struct regional
60 1.1.1.1.2.2 pgoyette {
61 1.1.1.1.2.2 pgoyette /**
62 1.1.1.1.2.2 pgoyette * next chunk. NULL if first chunk is the only chunk.
63 1.1.1.1.2.2 pgoyette * first inside that chunk is the char* next pointer.
64 1.1.1.1.2.2 pgoyette * When regional_free_all() has been called this value is NULL.
65 1.1.1.1.2.2 pgoyette */
66 1.1.1.1.2.2 pgoyette char* next;
67 1.1.1.1.2.2 pgoyette /** first large object, cast to char** to obtain next ptr */
68 1.1.1.1.2.2 pgoyette char* large_list;
69 1.1.1.1.2.2 pgoyette /** total large size */
70 1.1.1.1.2.2 pgoyette size_t total_large;
71 1.1.1.1.2.2 pgoyette /** initial chunk size */
72 1.1.1.1.2.2 pgoyette size_t first_size;
73 1.1.1.1.2.2 pgoyette /** number of bytes available in the current chunk. */
74 1.1.1.1.2.2 pgoyette size_t available;
75 1.1.1.1.2.2 pgoyette /** current chunk data position. */
76 1.1.1.1.2.2 pgoyette char* data;
77 1.1.1.1.2.2 pgoyette };
78 1.1.1.1.2.2 pgoyette
79 1.1.1.1.2.2 pgoyette /**
80 1.1.1.1.2.2 pgoyette * Create a new regional.
81 1.1.1.1.2.2 pgoyette * @return: newly allocated regional.
82 1.1.1.1.2.2 pgoyette */
83 1.1.1.1.2.2 pgoyette struct regional* regional_create(void);
84 1.1.1.1.2.2 pgoyette
85 1.1.1.1.2.2 pgoyette /**
86 1.1.1.1.2.2 pgoyette * Create a new region, with custom settings.
87 1.1.1.1.2.2 pgoyette * @param size: length of first block.
88 1.1.1.1.2.2 pgoyette * @return: newly allocated regional.
89 1.1.1.1.2.2 pgoyette */
90 1.1.1.1.2.2 pgoyette struct regional* regional_create_custom(size_t size);
91 1.1.1.1.2.2 pgoyette
92 1.1.1.1.2.2 pgoyette /**
93 1.1.1.1.2.2 pgoyette * Free all memory associated with regional. Only keeps the first block with
94 1.1.1.1.2.2 pgoyette * the regional inside it.
95 1.1.1.1.2.2 pgoyette * @param r: the region.
96 1.1.1.1.2.2 pgoyette */
97 1.1.1.1.2.2 pgoyette void regional_free_all(struct regional *r);
98 1.1.1.1.2.2 pgoyette
99 1.1.1.1.2.2 pgoyette /**
100 1.1.1.1.2.2 pgoyette * Destroy regional. All memory associated with regional is freed as if
101 1.1.1.1.2.2 pgoyette * regional_free_all was called, as well as destroying the regional struct.
102 1.1.1.1.2.2 pgoyette * @param r: to delete.
103 1.1.1.1.2.2 pgoyette */
104 1.1.1.1.2.2 pgoyette void regional_destroy(struct regional *r);
105 1.1.1.1.2.2 pgoyette
106 1.1.1.1.2.2 pgoyette /**
107 1.1.1.1.2.2 pgoyette * Allocate size bytes of memory inside regional. The memory is
108 1.1.1.1.2.2 pgoyette * deallocated when region_free_all is called for this region.
109 1.1.1.1.2.2 pgoyette * @param r: the region.
110 1.1.1.1.2.2 pgoyette * @param size: number of bytes.
111 1.1.1.1.2.2 pgoyette * @return: pointer to memory allocated.
112 1.1.1.1.2.2 pgoyette */
113 1.1.1.1.2.2 pgoyette void *regional_alloc(struct regional *r, size_t size);
114 1.1.1.1.2.2 pgoyette
115 1.1.1.1.2.2 pgoyette /**
116 1.1.1.1.2.2 pgoyette * Allocate size bytes of memory inside regional and copy INIT into it.
117 1.1.1.1.2.2 pgoyette * The memory is deallocated when region_free_all is called for this
118 1.1.1.1.2.2 pgoyette * region.
119 1.1.1.1.2.2 pgoyette * @param r: the region.
120 1.1.1.1.2.2 pgoyette * @param init: to copy.
121 1.1.1.1.2.2 pgoyette * @param size: number of bytes.
122 1.1.1.1.2.2 pgoyette * @return: pointer to memory allocated.
123 1.1.1.1.2.2 pgoyette */
124 1.1.1.1.2.2 pgoyette void *regional_alloc_init(struct regional* r, const void *init, size_t size);
125 1.1.1.1.2.2 pgoyette
126 1.1.1.1.2.2 pgoyette /**
127 1.1.1.1.2.2 pgoyette * Allocate size bytes of memory inside regional that are initialized to
128 1.1.1.1.2.2 pgoyette * 0. The memory is deallocated when region_free_all is called for
129 1.1.1.1.2.2 pgoyette * this region.
130 1.1.1.1.2.2 pgoyette * @param r: the region.
131 1.1.1.1.2.2 pgoyette * @param size: number of bytes.
132 1.1.1.1.2.2 pgoyette * @return: pointer to memory allocated.
133 1.1.1.1.2.2 pgoyette */
134 1.1.1.1.2.2 pgoyette void *regional_alloc_zero(struct regional *r, size_t size);
135 1.1.1.1.2.2 pgoyette
136 1.1.1.1.2.2 pgoyette /**
137 1.1.1.1.2.2 pgoyette * Duplicate string and allocate the result in regional.
138 1.1.1.1.2.2 pgoyette * @param r: the region.
139 1.1.1.1.2.2 pgoyette * @param string: null terminated string.
140 1.1.1.1.2.2 pgoyette * @return: pointer to memory allocated.
141 1.1.1.1.2.2 pgoyette */
142 1.1.1.1.2.2 pgoyette char *regional_strdup(struct regional *r, const char *string);
143 1.1.1.1.2.2 pgoyette
144 1.1.1.1.2.2 pgoyette /** Debug print regional statistics to log */
145 1.1.1.1.2.2 pgoyette void regional_log_stats(struct regional *r);
146 1.1.1.1.2.2 pgoyette
147 1.1.1.1.2.2 pgoyette /** get total memory size in use by region */
148 1.1.1.1.2.2 pgoyette size_t regional_get_mem(struct regional* r);
149 1.1.1.1.2.2 pgoyette
150 1.1.1.1.2.2 pgoyette #endif /* UTIL_REGIONAL_H_ */
151