mem1.c revision 75782
1/* $NetBSD: mem1.c,v 1.2 1995/07/03 21:24:25 cgd Exp $ */ 2 3/* 4 * Copyright (c) 1994, 1995 Jochen Pohl 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 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Jochen Pohl for 18 * The NetBSD Project. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 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 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 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char rcsid[] = 36 "$FreeBSD: head/usr.bin/xlint/lint1/mem1.c 75782 2001-04-21 11:17:44Z asmodai $"; 37#endif 38 39#include <sys/types.h> 40#include <sys/mman.h> 41#include <sys/param.h> 42#include <stdlib.h> 43#include <string.h> 44#include <unistd.h> 45#include <err.h> 46 47#include "lint1.h" 48 49/* 50 * Filenames allocated by fnalloc() and fnnalloc() are shared. 51 */ 52typedef struct fn { 53 char *fn_name; 54 size_t fn_len; 55 int fn_id; 56 struct fn *fn_nxt; 57} fn_t; 58 59static fn_t *fnames; 60 61static fn_t *srchfn __P((const char *, size_t)); 62 63/* 64 * Look for a Filename of length l. 65 */ 66static fn_t * 67srchfn(s, len) 68 const char *s; 69 size_t len; 70{ 71 fn_t *fn; 72 73 for (fn = fnames; fn != NULL; fn = fn->fn_nxt) { 74 if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0) 75 break; 76 } 77 return (fn); 78} 79 80/* 81 * Return a shared string for filename s. 82 */ 83const char * 84fnalloc(s) 85 const char *s; 86{ 87 return (s != NULL ? fnnalloc(s, strlen(s)) : NULL); 88} 89 90const char * 91fnnalloc(s, len) 92 const char *s; 93 size_t len; 94{ 95 fn_t *fn; 96 97 static int nxt_id = 0; 98 99 if (s == NULL) 100 return (NULL); 101 102 if ((fn = srchfn(s, len)) == NULL) { 103 fn = xmalloc(sizeof (fn_t)); 104 /* Do not used strdup() because string is not NUL-terminated.*/ 105 fn->fn_name = xmalloc(len + 1); 106 (void)memcpy(fn->fn_name, s, len); 107 fn->fn_name[len] = '\0'; 108 fn->fn_len = len; 109 fn->fn_id = nxt_id++; 110 fn->fn_nxt = fnames; 111 fnames = fn; 112 /* Write id of this filename to the output file. */ 113 outclr(); 114 outint(fn->fn_id); 115 outchar('s'); 116 outstrg(fn->fn_name); 117 } 118 return (fn->fn_name); 119} 120 121/* 122 * Get id of a filename. 123 */ 124int 125getfnid(s) 126 const char *s; 127{ 128 fn_t *fn; 129 130 if (s == NULL || (fn = srchfn(s, strlen(s))) == NULL) 131 return (-1); 132 return (fn->fn_id); 133} 134 135/* 136 * Memory for declarations and other things which must be available 137 * until the end of a block (or the end of the translation unit) 138 * are assoziated with the level (mblklev) of the block (or wiht 0). 139 * Because these memory is allocated in large blocks associated with 140 * a given level it can be freed easily at the end of a block. 141 */ 142#define ML_INC ((size_t)32) /* Increment for length of *mblks */ 143 144typedef struct mbl { 145 void *blk; /* beginning of memory block */ 146 void *ffree; /* first free byte */ 147 size_t nfree; /* # of free bytes */ 148 size_t size; /* total size of memory block */ 149 struct mbl *nxt; /* next block */ 150} mbl_t; 151 152/* 153 * Array of pointers to lists of memory blocks. mblklev is used as 154 * index into this array. 155 */ 156static mbl_t **mblks; 157 158/* number of elements in *mblks */ 159static size_t nmblks; 160 161/* free list for memory blocks */ 162static mbl_t *frmblks; 163 164/* length of new allocated memory blocks */ 165static size_t mblklen; 166 167static void *xgetblk __P((mbl_t **, size_t)); 168static void xfreeblk __P((mbl_t **)); 169static mbl_t *xnewblk __P((void)); 170 171static mbl_t * 172xnewblk() 173{ 174 mbl_t *mb; 175 int prot, flags; 176 177 mb = xmalloc(sizeof (mbl_t)); 178 179 /* use mmap instead of malloc to avoid malloc's size overhead */ 180 181 prot = PROT_READ | PROT_WRITE; 182 flags = MAP_ANON | MAP_PRIVATE; 183 mb->blk = mmap(NULL, mblklen, prot, flags, -1, (off_t)0); 184 if (mb->blk == (void *)MAP_FAILED) 185 err(1, "can't map memory"); 186 if (ALIGN((u_long)mb->blk) != (u_long)mb->blk) 187 errx(1, "mapped address is not aligned"); 188 189 mb->size = mblklen; 190 191 return (mb); 192} 193 194/* 195 * Allocate new memory. If the first block of the list has not enough 196 * free space, or there is no first block, get a new block. The new 197 * block is taken from the free list or, if there is no block on the 198 * free list, is allocated using xnewblk(). If a new block is allocated 199 * it is initialized with zero. Blocks taken from the free list are 200 * zero'd in xfreeblk(). 201 */ 202static void * 203xgetblk(mbp, s) 204 mbl_t **mbp; 205 size_t s; 206{ 207 mbl_t *mb; 208 void *p; 209 210 s = ALIGN(s); 211 if ((mb = *mbp) == NULL || mb->nfree < s) { 212 if ((mb = frmblks) == NULL) { 213 mb = xnewblk(); 214 (void)memset(mb->blk, 0, mb->size); 215 } else { 216 frmblks = mb->nxt; 217 } 218 mb->ffree = mb->blk; 219 mb->nfree = mb->size;; 220 mb->nxt = *mbp; 221 *mbp = mb; 222 } 223 p = mb->ffree; 224 mb->ffree = (char *)mb->ffree + s; 225 mb->nfree -= s; 226 return (p); 227} 228 229/* 230 * Move all blocks from list *fmbp to free list. For each block, set all 231 * used memory to zero. 232 */ 233static void 234xfreeblk(fmbp) 235 mbl_t **fmbp; 236{ 237 mbl_t *mb; 238 239 while ((mb = *fmbp) != NULL) { 240 *fmbp = mb->nxt; 241 mb->nxt = frmblks; 242 frmblks = mb; 243 (void)memset(mb->blk, 0, mb->size - mb->nfree); 244 } 245} 246 247void 248initmem() 249{ 250 int pgsz; 251 252 pgsz = getpagesize(); 253 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz; 254 255 mblks = xcalloc(nmblks = ML_INC, sizeof (mbl_t *)); 256} 257 258 259/* 260 * Allocate memory associated with level l. 261 */ 262void * 263getlblk(l, s) 264 int l; 265 size_t s; 266{ 267 while (l >= nmblks) { 268 mblks = xrealloc(mblks, (nmblks + ML_INC) * sizeof (mbl_t *)); 269 (void)memset(&mblks[nmblks], 0, ML_INC * sizeof (mbl_t *)); 270 nmblks += ML_INC; 271 } 272 return (xgetblk(&mblks[l], s)); 273} 274 275void * 276getblk(s) 277 size_t s; 278{ 279 return (getlblk(mblklev, s)); 280} 281 282/* 283 * Free all memory associated with level l. 284 */ 285void 286freelblk(l) 287 int l; 288{ 289 xfreeblk(&mblks[l]); 290} 291 292void 293freeblk() 294{ 295 freelblk(mblklev); 296} 297 298/* 299 * tgetblk() returns memory which is associated with the current 300 * expression. 301 */ 302static mbl_t *tmblk; 303 304void * 305tgetblk(s) 306 size_t s; 307{ 308 return (xgetblk(&tmblk, s)); 309} 310 311/* 312 * Get memory for a new tree node. 313 */ 314tnode_t * 315getnode() 316{ 317 return (tgetblk(sizeof (tnode_t))); 318} 319 320/* 321 * Free all memory which is allocated by the the current expression. 322 */ 323void 324tfreeblk() 325{ 326 xfreeblk(&tmblk); 327} 328 329/* 330 * Save the memory which is used by the current expression. This memory 331 * is not freed by the next tfreeblk() call. The pointer returned can be 332 * used to restore the memory. 333 */ 334mbl_t * 335tsave() 336{ 337 mbl_t *tmem; 338 339 tmem = tmblk; 340 tmblk = NULL; 341 return (tmem); 342} 343 344/* 345 * Free all memory used for the current expression and the memory used 346 * be a previous expression and saved by tsave(). The next call to 347 * tfreeblk() frees the restored memory. 348 */ 349void 350trestor(tmem) 351 mbl_t *tmem; 352{ 353 tfreeblk(); 354 if (tmblk != NULL) { 355 free(tmblk->blk); 356 free(tmblk); 357 } 358 tmblk = tmem; 359} 360