2 tre-mem.c - TRE memory allocator
4 Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi>
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
19 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 This memory allocator is for allocating small memory blocks efficiently
34 in terms of memory overhead and execution speed. The allocated blocks
35 cannot be freed individually, only all at once. There can be multiple
45 This memory allocator is for allocating small memory blocks efficiently
46 in terms of memory overhead and execution speed. The allocated blocks
47 cannot be freed individually, only all at once. There can be multiple
51 /* Returns a new memory allocator or NULL if out of memory. */
53 tre_mem_new_impl(int provided
, void *provided_block
)
59 memset(mem
, 0, sizeof(*mem
));
62 mem
= xcalloc(1, sizeof(*mem
));
69 /* Frees the memory allocator and all memory allocated with it. */
71 tre_mem_destroy(tre_mem_t mem
)
73 tre_list_t
*tmp
, *l
= mem
->blocks
;
86 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
87 allocated block or NULL if an underlying malloc() failed. */
89 tre_mem_alloc_impl(tre_mem_t mem
, int provided
, void *provided_block
,
90 int zero
, size_t size
)
101 /* We need more memory than is available in the current block.
102 Allocate a new block. */
106 if (provided_block
== NULL
)
111 mem
->ptr
= provided_block
;
112 mem
->n
= TRE_MEM_BLOCK_SIZE
;
117 if (size
* 8 > TRE_MEM_BLOCK_SIZE
)
118 block_size
= size
* 8;
120 block_size
= TRE_MEM_BLOCK_SIZE
;
121 l
= xmalloc(sizeof(*l
));
127 l
->data
= xmalloc(block_size
);
135 if (mem
->current
!= NULL
)
136 mem
->current
->next
= l
;
137 if (mem
->blocks
== NULL
)
145 /* Make sure the next pointer will be aligned. */
146 size
+= ALIGN(mem
->ptr
+ size
, long);
148 /* Allocate from current block. */
153 /* Set to zero if needed. */
155 memset(ptr
, 0, size
);