1 /*
2 tre-mem.c - TRE memory allocator
3
4 Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi>
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13
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.
17
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.
29
30 */
31
32 /*
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
36 allocators, though.
37 */
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "tre.h"
43
44 /*
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
48 allocators, though.
49 */
50
51 /* Returns a new memory allocator or NULL if out of memory. */
52 tre_mem_t
tre_mem_new_impl(int provided,void * provided_block)53 tre_mem_new_impl(int provided, void *provided_block)
54 {
55 tre_mem_t mem;
56 if (provided)
57 {
58 mem = provided_block;
59 memset(mem, 0, sizeof(*mem));
60 }
61 else
62 mem = xcalloc(1, sizeof(*mem));
63 if (mem == NULL)
64 return NULL;
65 return mem;
66 }
67
68
69 /* Frees the memory allocator and all memory allocated with it. */
70 void
tre_mem_destroy(tre_mem_t mem)71 tre_mem_destroy(tre_mem_t mem)
72 {
73 tre_list_t *tmp, *l = mem->blocks;
74
75 while (l != NULL)
76 {
77 xfree(l->data);
78 tmp = l->next;
79 xfree(l);
80 l = tmp;
81 }
82 xfree(mem);
83 }
84
85
86 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
87 allocated block or NULL if an underlying malloc() failed. */
88 void *
tre_mem_alloc_impl(tre_mem_t mem,int provided,void * provided_block,int zero,size_t size)89 tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
90 int zero, size_t size)
91 {
92 void *ptr;
93
94 if (mem->failed)
95 {
96 return NULL;
97 }
98
99 if (mem->n < size)
100 {
101 /* We need more memory than is available in the current block.
102 Allocate a new block. */
103 tre_list_t *l;
104 if (provided)
105 {
106 if (provided_block == NULL)
107 {
108 mem->failed = 1;
109 return NULL;
110 }
111 mem->ptr = provided_block;
112 mem->n = TRE_MEM_BLOCK_SIZE;
113 }
114 else
115 {
116 int block_size;
117 if (size * 8 > TRE_MEM_BLOCK_SIZE)
118 block_size = size * 8;
119 else
120 block_size = TRE_MEM_BLOCK_SIZE;
121 l = xmalloc(sizeof(*l));
122 if (l == NULL)
123 {
124 mem->failed = 1;
125 return NULL;
126 }
127 l->data = xmalloc(block_size);
128 if (l->data == NULL)
129 {
130 xfree(l);
131 mem->failed = 1;
132 return NULL;
133 }
134 l->next = NULL;
135 if (mem->current != NULL)
136 mem->current->next = l;
137 if (mem->blocks == NULL)
138 mem->blocks = l;
139 mem->current = l;
140 mem->ptr = l->data;
141 mem->n = block_size;
142 }
143 }
144
145 /* Make sure the next pointer will be aligned. */
146 size += ALIGN(mem->ptr + size, long);
147
148 /* Allocate from current block. */
149 ptr = mem->ptr;
150 mem->ptr += size;
151 mem->n -= size;
152
153 /* Set to zero if needed. */
154 if (zero)
155 memset(ptr, 0, size);
156
157 return ptr;
158 }
159