sys/stat.h: fix typo in statx member name stx_dio_offset_align
[musl.git] / src / regex / tre-mem.c
blob86f809d45564ee3190be8d65aac328f97f2882b4
1 /*
2 tre-mem.c - TRE memory allocator
4 Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi>
5 All rights reserved.
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
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
36 allocators, though.
39 #include <stdlib.h>
40 #include <string.h>
42 #include "tre.h"
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.
51 /* Returns a new memory allocator or NULL if out of memory. */
52 tre_mem_t
53 tre_mem_new_impl(int provided, void *provided_block)
55 tre_mem_t mem;
56 if (provided)
58 mem = provided_block;
59 memset(mem, 0, sizeof(*mem));
61 else
62 mem = xcalloc(1, sizeof(*mem));
63 if (mem == NULL)
64 return NULL;
65 return mem;
69 /* Frees the memory allocator and all memory allocated with it. */
70 void
71 tre_mem_destroy(tre_mem_t mem)
73 tre_list_t *tmp, *l = mem->blocks;
75 while (l != NULL)
77 xfree(l->data);
78 tmp = l->next;
79 xfree(l);
80 l = tmp;
82 xfree(mem);
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 *
89 tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
90 int zero, size_t size)
92 void *ptr;
94 if (mem->failed)
96 return NULL;
99 if (mem->n < size)
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)
106 if (provided_block == NULL)
108 mem->failed = 1;
109 return NULL;
111 mem->ptr = provided_block;
112 mem->n = TRE_MEM_BLOCK_SIZE;
114 else
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)
124 mem->failed = 1;
125 return NULL;
127 l->data = xmalloc(block_size);
128 if (l->data == NULL)
130 xfree(l);
131 mem->failed = 1;
132 return NULL;
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;
145 /* Make sure the next pointer will be aligned. */
146 size += ALIGN(mem->ptr + size, long);
148 /* Allocate from current block. */
149 ptr = mem->ptr;
150 mem->ptr += size;
151 mem->n -= size;
153 /* Set to zero if needed. */
154 if (zero)
155 memset(ptr, 0, size);
157 return ptr;