Module Name: src Committed By: rillig Date: Sun Feb 27 07:38:54 UTC 2022
Modified Files: src/usr.bin/xlint/lint1: lex.c lint1.h mem1.c Log Message: lint: clean up memory allocation No functional change. To generate a diff of this commit: cvs rdiff -u -r1.98 -r1.99 src/usr.bin/xlint/lint1/lex.c cvs rdiff -u -r1.136 -r1.137 src/usr.bin/xlint/lint1/lint1.h cvs rdiff -u -r1.58 -r1.59 src/usr.bin/xlint/lint1/mem1.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/xlint/lint1/lex.c diff -u src/usr.bin/xlint/lint1/lex.c:1.98 src/usr.bin/xlint/lint1/lex.c:1.99 --- src/usr.bin/xlint/lint1/lex.c:1.98 Sat Feb 26 23:07:28 2022 +++ src/usr.bin/xlint/lint1/lex.c Sun Feb 27 07:38:54 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: lex.c,v 1.98 2022/02/26 23:07:28 rillig Exp $ */ +/* $NetBSD: lex.c,v 1.99 2022/02/27 07:38:54 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -38,7 +38,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: lex.c,v 1.98 2022/02/26 23:07:28 rillig Exp $"); +__RCSID("$NetBSD: lex.c,v 1.99 2022/02/27 07:38:54 rillig Exp $"); #endif #include <ctype.h> @@ -370,7 +370,7 @@ static void freesb(sbuf_t *sb) { - (void)memset(sb, INVALID_MEM_BYTE, sizeof(*sb)); + (void)memset(sb, 0xa5, sizeof(*sb)); sb->sb_next = sbuf_free_list; sbuf_free_list = sb; } Index: src/usr.bin/xlint/lint1/lint1.h diff -u src/usr.bin/xlint/lint1/lint1.h:1.136 src/usr.bin/xlint/lint1/lint1.h:1.137 --- src/usr.bin/xlint/lint1/lint1.h:1.136 Sun Feb 27 01:47:28 2022 +++ src/usr.bin/xlint/lint1/lint1.h Sun Feb 27 07:38:54 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: lint1.h,v 1.136 2022/02/27 01:47:28 rillig Exp $ */ +/* $NetBSD: lint1.h,v 1.137 2022/02/27 07:38:54 rillig Exp $ */ /* * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. @@ -485,12 +485,6 @@ typedef struct err_set { assert_failed(__FILE__, __LINE__, __func__, #cond); \ } while (false) -#ifdef BLKDEBUG -#define INVALID_MEM_BYTE 0xa5 -#else -#define INVALID_MEM_BYTE 0 -#endif - extern err_set msgset; Index: src/usr.bin/xlint/lint1/mem1.c diff -u src/usr.bin/xlint/lint1/mem1.c:1.58 src/usr.bin/xlint/lint1/mem1.c:1.59 --- src/usr.bin/xlint/lint1/mem1.c:1.58 Sun Feb 27 06:55:13 2022 +++ src/usr.bin/xlint/lint1/mem1.c Sun Feb 27 07:38:54 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: mem1.c,v 1.58 2022/02/27 06:55:13 rillig Exp $ */ +/* $NetBSD: mem1.c,v 1.59 2022/02/27 07:38:54 rillig Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl @@ -37,7 +37,7 @@ #include <sys/cdefs.h> #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: mem1.c,v 1.58 2022/02/27 06:55:13 rillig Exp $"); +__RCSID("$NetBSD: mem1.c,v 1.59 2022/02/27 07:38:54 rillig Exp $"); #endif #include <sys/param.h> @@ -172,45 +172,27 @@ get_filename_id(const char *s) } /* - * Memory for declarations and other things which must be available + * Memory for declarations and other things that must be available * until the end of a block (or the end of the translation unit) * is associated with the corresponding mem_block_level, which may be 0. * Because this memory is allocated in large blocks associated with * a given level it can be freed easily at the end of a block. */ -#define ML_INC ((size_t)32) /* Increment for length of *mblks */ - typedef struct memory_block { void *start; /* beginning of memory block */ void *first_free; /* first free byte */ size_t nfree; /* # of free bytes */ - size_t size; /* total size of memory block */ struct memory_block *next; } memory_block; -/* - * Array of pointers to lists of memory blocks. mem_block_level is used as - * index into this array. - */ -static memory_block **mblks; - -/* number of elements in *mblks */ -static size_t nmblks; -/* length of new allocated memory blocks */ -static size_t mblklen; +static size_t mblk_size; /* size of newly allocated memory blocks */ +/* Array of lists of memory blocks, indexed by mem_block_level. */ +static memory_block **mblks; +static size_t nmblks; /* number of elements in *mblks */ +#define ML_INC ((size_t)32) /* Increment for length of *mblks */ -static memory_block * -xnewblk(void) -{ - memory_block *mb = xmalloc(sizeof(*mb)); - - mb->start = xmalloc(mblklen); - mb->size = mblklen; - - return mb; -} /* Allocate new memory, initialized with zero. */ static void * @@ -218,41 +200,23 @@ xgetblk(memory_block **mbp, size_t s) { memory_block *mb; void *p; - size_t t = 0; - - /* - * If the first block of the list has not enough free space, - * or there is no first block, get a new block. The new block - * is taken from the free list or, if there is no block on the - * free list, is allocated using xnewblk(). - * - * If a new block is allocated it is initialized with zero. - * Blocks taken from the free list are zero'd in xfreeblk(). - */ s = WORST_ALIGN(s); + if ((mb = *mbp) == NULL || mb->nfree < s) { - if (s > mblklen) { - t = mblklen; - mblklen = s; - } - mb = xnewblk(); -#ifndef BLKDEBUG - (void)memset(mb->start, 0, mb->size); -#endif - if (t > 0) - mblklen = t; + size_t block_size = s > mblk_size ? s : mblk_size; + mb = xmalloc(sizeof(*mb)); + mb->start = xmalloc(block_size); mb->first_free = mb->start; - mb->nfree = mb->size; + mb->nfree = block_size; mb->next = *mbp; *mbp = mb; } + p = mb->first_free; mb->first_free = (char *)mb->first_free + s; mb->nfree -= s; -#ifdef BLKDEBUG (void)memset(p, 0, s); -#endif return p; } @@ -272,7 +236,7 @@ void initmem(void) { - mblklen = mem_block_size(); + mblk_size = mem_block_size(); mblks = xcalloc(nmblks = ML_INC, sizeof(*mblks)); }