| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: mem1.c,v 1.57 2021/12/25 13:51:42 rillig Exp $ */ | | 1 | /* $NetBSD: mem1.c,v 1.58 2022/02/27 06:55:13 rillig Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 1994, 1995 Jochen Pohl | | 4 | * Copyright (c) 1994, 1995 Jochen Pohl |
5 | * All Rights Reserved. | | 5 | * All Rights Reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -27,27 +27,27 @@ | | | @@ -27,27 +27,27 @@ |
27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | */ | | 32 | */ |
33 | | | 33 | |
34 | #if HAVE_NBTOOL_CONFIG_H | | 34 | #if HAVE_NBTOOL_CONFIG_H |
35 | #include "nbtool_config.h" | | 35 | #include "nbtool_config.h" |
36 | #endif | | 36 | #endif |
37 | | | 37 | |
38 | #include <sys/cdefs.h> | | 38 | #include <sys/cdefs.h> |
39 | #if defined(__RCSID) && !defined(lint) | | 39 | #if defined(__RCSID) && !defined(lint) |
40 | __RCSID("$NetBSD: mem1.c,v 1.57 2021/12/25 13:51:42 rillig Exp $"); | | 40 | __RCSID("$NetBSD: mem1.c,v 1.58 2022/02/27 06:55:13 rillig Exp $"); |
41 | #endif | | 41 | #endif |
42 | | | 42 | |
43 | #include <sys/param.h> | | 43 | #include <sys/param.h> |
44 | #include <stdlib.h> | | 44 | #include <stdlib.h> |
45 | #include <string.h> | | 45 | #include <string.h> |
46 | | | 46 | |
47 | #include "lint1.h" | | 47 | #include "lint1.h" |
48 | | | 48 | |
49 | /* | | 49 | /* |
50 | * Filenames allocated by record_filename are shared and have unlimited | | 50 | * Filenames allocated by record_filename are shared and have unlimited |
51 | * lifetime. | | 51 | * lifetime. |
52 | */ | | 52 | */ |
53 | struct filename { | | 53 | struct filename { |
| @@ -187,29 +187,26 @@ typedef struct memory_block { | | | @@ -187,29 +187,26 @@ typedef struct memory_block { |
187 | size_t size; /* total size of memory block */ | | 187 | size_t size; /* total size of memory block */ |
188 | struct memory_block *next; | | 188 | struct memory_block *next; |
189 | } memory_block; | | 189 | } memory_block; |
190 | | | 190 | |
191 | /* | | 191 | /* |
192 | * Array of pointers to lists of memory blocks. mem_block_level is used as | | 192 | * Array of pointers to lists of memory blocks. mem_block_level is used as |
193 | * index into this array. | | 193 | * index into this array. |
194 | */ | | 194 | */ |
195 | static memory_block **mblks; | | 195 | static memory_block **mblks; |
196 | | | 196 | |
197 | /* number of elements in *mblks */ | | 197 | /* number of elements in *mblks */ |
198 | static size_t nmblks; | | 198 | static size_t nmblks; |
199 | | | 199 | |
200 | /* free list for memory blocks */ | | | |
201 | static memory_block *frmblks; | | | |
202 | | | | |
203 | /* length of new allocated memory blocks */ | | 200 | /* length of new allocated memory blocks */ |
204 | static size_t mblklen; | | 201 | static size_t mblklen; |
205 | | | 202 | |
206 | | | 203 | |
207 | static memory_block * | | 204 | static memory_block * |
208 | xnewblk(void) | | 205 | xnewblk(void) |
209 | { | | 206 | { |
210 | memory_block *mb = xmalloc(sizeof(*mb)); | | 207 | memory_block *mb = xmalloc(sizeof(*mb)); |
211 | | | 208 | |
212 | mb->start = xmalloc(mblklen); | | 209 | mb->start = xmalloc(mblklen); |
213 | mb->size = mblklen; | | 210 | mb->size = mblklen; |
214 | | | 211 | |
215 | return mb; | | 212 | return mb; |
| @@ -225,69 +222,59 @@ xgetblk(memory_block **mbp, size_t s) | | | @@ -225,69 +222,59 @@ xgetblk(memory_block **mbp, size_t s) |
225 | | | 222 | |
226 | /* | | 223 | /* |
227 | * If the first block of the list has not enough free space, | | 224 | * If the first block of the list has not enough free space, |
228 | * or there is no first block, get a new block. The new block | | 225 | * or there is no first block, get a new block. The new block |
229 | * is taken from the free list or, if there is no block on the | | 226 | * is taken from the free list or, if there is no block on the |
230 | * free list, is allocated using xnewblk(). | | 227 | * free list, is allocated using xnewblk(). |
231 | * | | 228 | * |
232 | * If a new block is allocated it is initialized with zero. | | 229 | * If a new block is allocated it is initialized with zero. |
233 | * Blocks taken from the free list are zero'd in xfreeblk(). | | 230 | * Blocks taken from the free list are zero'd in xfreeblk(). |
234 | */ | | 231 | */ |
235 | | | 232 | |
236 | s = WORST_ALIGN(s); | | 233 | s = WORST_ALIGN(s); |
237 | if ((mb = *mbp) == NULL || mb->nfree < s) { | | 234 | if ((mb = *mbp) == NULL || mb->nfree < s) { |
238 | if ((mb = frmblks) == NULL || mb->size < s) { | | 235 | if (s > mblklen) { |
239 | if (s > mblklen) { | | 236 | t = mblklen; |
240 | t = mblklen; | | 237 | mblklen = s; |
241 | mblklen = s; | | 238 | } |
242 | } | | 239 | mb = xnewblk(); |
243 | mb = xnewblk(); | | | |
244 | #ifndef BLKDEBUG | | 240 | #ifndef BLKDEBUG |
245 | (void)memset(mb->start, 0, mb->size); | | 241 | (void)memset(mb->start, 0, mb->size); |
246 | #endif | | 242 | #endif |
247 | if (t > 0) | | 243 | if (t > 0) |
248 | mblklen = t; | | 244 | mblklen = t; |
249 | } else { | | | |
250 | frmblks = mb->next; | | | |
251 | } | | | |
252 | mb->first_free = mb->start; | | 245 | mb->first_free = mb->start; |
253 | mb->nfree = mb->size; | | 246 | mb->nfree = mb->size; |
254 | mb->next = *mbp; | | 247 | mb->next = *mbp; |
255 | *mbp = mb; | | 248 | *mbp = mb; |
256 | } | | 249 | } |
257 | p = mb->first_free; | | 250 | p = mb->first_free; |
258 | mb->first_free = (char *)mb->first_free + s; | | 251 | mb->first_free = (char *)mb->first_free + s; |
259 | mb->nfree -= s; | | 252 | mb->nfree -= s; |
260 | #ifdef BLKDEBUG | | 253 | #ifdef BLKDEBUG |
261 | (void)memset(p, 0, s); | | 254 | (void)memset(p, 0, s); |
262 | #endif | | 255 | #endif |
263 | return p; | | 256 | return p; |
264 | } | | 257 | } |
265 | | | 258 | |
266 | /* | | 259 | /* Free all blocks from list *fmbp. */ |
267 | * Move all blocks from list *fmbp to free list. For each block, set all | | | |
268 | * used memory to zero. | | | |
269 | */ | | | |
270 | static void | | 260 | static void |
271 | xfreeblk(memory_block **fmbp) | | 261 | xfreeblk(memory_block **fmbp) |
272 | { | | 262 | { |
273 | memory_block *mb; | | 263 | memory_block *mb; |
274 | | | 264 | |
275 | while ((mb = *fmbp) != NULL) { | | 265 | while ((mb = *fmbp) != NULL) { |
276 | *fmbp = mb->next; | | 266 | *fmbp = mb->next; |
277 | mb->next = frmblks; | | 267 | free(mb); |
278 | frmblks = mb; | | | |
279 | (void)memset(mb->start, INVALID_MEM_BYTE, | | | |
280 | mb->size - mb->nfree); | | | |
281 | } | | 268 | } |
282 | } | | 269 | } |
283 | | | 270 | |
284 | void | | 271 | void |
285 | initmem(void) | | 272 | initmem(void) |
286 | { | | 273 | { |
287 | | | 274 | |
288 | mblklen = mem_block_size(); | | 275 | mblklen = mem_block_size(); |
289 | mblks = xcalloc(nmblks = ML_INC, sizeof(*mblks)); | | 276 | mblks = xcalloc(nmblks = ML_INC, sizeof(*mblks)); |
290 | } | | 277 | } |
291 | | | 278 | |
292 | | | 279 | |
293 | /* Allocate memory associated with level l, initialized with zero. */ | | 280 | /* Allocate memory associated with level l, initialized with zero. */ |