mem1.c (80284) | mem1.c (91592) |
---|---|
1/* $NetBSD: mem1.c,v 1.2 1995/07/03 21:24:25 cgd Exp $ */ | 1/* $NetBSD: mem1.c,v 1.6 2002/01/29 02:43:39 tv 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: --- 16 unchanged lines hidden (view full) --- 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 | 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: --- 16 unchanged lines hidden (view full) --- 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 80284 2001-07-24 14:02:07Z obrien $"; | 34#include <sys/cdefs.h> 35#if defined(__RCSID) && !defined(lint) 36__RCSID("$NetBSD: mem1.c,v 1.6 2002/01/29 02:43:39 tv Exp $"); |
37#endif | 37#endif |
38__FBSDID("$FreeBSD: head/usr.bin/xlint/lint1/mem1.c 91592 2002-03-03 15:12:50Z markm $"); |
|
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> | 39 40#include <sys/types.h> 41#include <sys/mman.h> 42#include <sys/param.h> 43#include <stdlib.h> 44#include <string.h> 45#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 | 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)); | 61static fn_t *srchfn(const char *, size_t); |
62 63/* 64 * Look for a Filename of length l. 65 */ 66static fn_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; | 67srchfn(const char *s, 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 * | 68{ 69 fn_t *fn; 70 71 for (fn = fnames; fn != NULL; fn = fn->fn_nxt) { 72 if (fn->fn_len == len && memcmp(fn->fn_name, s, len) == 0) 73 break; 74 } 75 return (fn); 76} 77 78/* 79 * Return a shared string for filename s. 80 */ 81const char * |
84fnalloc(s) 85 const char *s; | 82fnalloc(const char *s) |
86{ | 83{ |
84 |
|
87 return (s != NULL ? fnnalloc(s, strlen(s)) : NULL); 88} 89 90const char * | 85 return (s != NULL ? fnnalloc(s, strlen(s)) : NULL); 86} 87 88const char * |
91fnnalloc(s, len) 92 const char *s; 93 size_t len; | 89fnnalloc(const char *s, size_t len) |
94{ 95 fn_t *fn; 96 97 static int nxt_id = 0; 98 99 if (s == NULL) 100 return (NULL); 101 --- 17 unchanged lines hidden (view full) --- 119 } 120 return (fn->fn_name); 121} 122 123/* 124 * Get id of a filename. 125 */ 126int | 90{ 91 fn_t *fn; 92 93 static int nxt_id = 0; 94 95 if (s == NULL) 96 return (NULL); 97 --- 17 unchanged lines hidden (view full) --- 115 } 116 return (fn->fn_name); 117} 118 119/* 120 * Get id of a filename. 121 */ 122int |
127getfnid(s) 128 const char *s; | 123getfnid(const char *s) |
129{ 130 fn_t *fn; 131 132 if (s == NULL || (fn = srchfn(s, strlen(s))) == NULL) 133 return (-1); 134 return (fn->fn_id); 135} 136 --- 24 unchanged lines hidden (view full) --- 161static size_t nmblks; 162 163/* free list for memory blocks */ 164static mbl_t *frmblks; 165 166/* length of new allocated memory blocks */ 167static size_t mblklen; 168 | 124{ 125 fn_t *fn; 126 127 if (s == NULL || (fn = srchfn(s, strlen(s))) == NULL) 128 return (-1); 129 return (fn->fn_id); 130} 131 --- 24 unchanged lines hidden (view full) --- 156static size_t nmblks; 157 158/* free list for memory blocks */ 159static mbl_t *frmblks; 160 161/* length of new allocated memory blocks */ 162static size_t mblklen; 163 |
169static void *xgetblk __P((mbl_t **, size_t)); 170static void xfreeblk __P((mbl_t **)); 171static mbl_t *xnewblk __P((void)); | 164static void *xgetblk(mbl_t **, size_t); 165static void xfreeblk(mbl_t **); 166static mbl_t *xnewblk(void); |
172 173static mbl_t * | 167 168static mbl_t * |
174xnewblk() | 169xnewblk(void) |
175{ 176 mbl_t *mb; 177 int prot, flags; 178 179 if ((mb = malloc(sizeof (mbl_t))) == NULL) 180 nomem(); 181 182 /* use mmap instead of malloc to avoid malloc's size overhead */ 183 184 prot = PROT_READ | PROT_WRITE; 185 flags = MAP_ANON | MAP_PRIVATE; 186 mb->blk = mmap(NULL, mblklen, prot, flags, -1, (off_t)0); 187 if (mb->blk == (void *)MAP_FAILED) 188 err(1, "can't map memory"); | 170{ 171 mbl_t *mb; 172 int prot, flags; 173 174 if ((mb = malloc(sizeof (mbl_t))) == NULL) 175 nomem(); 176 177 /* use mmap instead of malloc to avoid malloc's size overhead */ 178 179 prot = PROT_READ | PROT_WRITE; 180 flags = MAP_ANON | MAP_PRIVATE; 181 mb->blk = mmap(NULL, mblklen, prot, flags, -1, (off_t)0); 182 if (mb->blk == (void *)MAP_FAILED) 183 err(1, "can't map memory"); |
189 if (ALIGN((u_long)mb->blk) != (u_long)mb->blk) 190 errx(1, "mapped address is not aligned"); | |
191 192 mb->size = mblklen; 193 194 return (mb); 195} 196 197/* 198 * Allocate new memory. If the first block of the list has not enough 199 * free space, or there is no first block, get a new block. The new 200 * block is taken from the free list or, if there is no block on the 201 * free list, is allocated using xnewblk(). If a new block is allocated 202 * it is initialized with zero. Blocks taken from the free list are 203 * zero'd in xfreeblk(). 204 */ 205static void * | 184 185 mb->size = mblklen; 186 187 return (mb); 188} 189 190/* 191 * Allocate new memory. If the first block of the list has not enough 192 * free space, or there is no first block, get a new block. The new 193 * block is taken from the free list or, if there is no block on the 194 * free list, is allocated using xnewblk(). If a new block is allocated 195 * it is initialized with zero. Blocks taken from the free list are 196 * zero'd in xfreeblk(). 197 */ 198static void * |
206xgetblk(mbp, s) 207 mbl_t **mbp; 208 size_t s; | 199xgetblk(mbl_t **mbp, size_t s) |
209{ 210 mbl_t *mb; 211 void *p; 212 213 s = ALIGN(s); 214 if ((mb = *mbp) == NULL || mb->nfree < s) { 215 if ((mb = frmblks) == NULL) { 216 mb = xnewblk(); --- 12 unchanged lines hidden (view full) --- 229 return (p); 230} 231 232/* 233 * Move all blocks from list *fmbp to free list. For each block, set all 234 * used memory to zero. 235 */ 236static void | 200{ 201 mbl_t *mb; 202 void *p; 203 204 s = ALIGN(s); 205 if ((mb = *mbp) == NULL || mb->nfree < s) { 206 if ((mb = frmblks) == NULL) { 207 mb = xnewblk(); --- 12 unchanged lines hidden (view full) --- 220 return (p); 221} 222 223/* 224 * Move all blocks from list *fmbp to free list. For each block, set all 225 * used memory to zero. 226 */ 227static void |
237xfreeblk(fmbp) 238 mbl_t **fmbp; | 228xfreeblk(mbl_t **fmbp) |
239{ 240 mbl_t *mb; 241 242 while ((mb = *fmbp) != NULL) { 243 *fmbp = mb->nxt; 244 mb->nxt = frmblks; 245 frmblks = mb; 246 (void)memset(mb->blk, 0, mb->size - mb->nfree); 247 } 248} 249 250void | 229{ 230 mbl_t *mb; 231 232 while ((mb = *fmbp) != NULL) { 233 *fmbp = mb->nxt; 234 mb->nxt = frmblks; 235 frmblks = mb; 236 (void)memset(mb->blk, 0, mb->size - mb->nfree); 237 } 238} 239 240void |
251initmem() | 241initmem(void) |
252{ 253 int pgsz; 254 255 pgsz = getpagesize(); 256 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz; 257 258 if ((mblks = calloc(nmblks = ML_INC, sizeof (mbl_t *))) == NULL) 259 nomem(); 260} 261 | 242{ 243 int pgsz; 244 245 pgsz = getpagesize(); 246 mblklen = ((MBLKSIZ + pgsz - 1) / pgsz) * pgsz; 247 248 if ((mblks = calloc(nmblks = ML_INC, sizeof (mbl_t *))) == NULL) 249 nomem(); 250} 251 |
262 | 252 |
263/* 264 * Allocate memory associated with level l. 265 */ 266void * | 253/* 254 * Allocate memory associated with level l. 255 */ 256void * |
267getlblk(l, s) 268 int l; 269 size_t s; | 257getlblk(int l, size_t s) |
270{ | 258{ |
259 |
|
271 while (l >= nmblks) { 272 if ((mblks = realloc(mblks, (nmblks + ML_INC) * 273 sizeof (mbl_t *))) == NULL) 274 nomem(); 275 (void)memset(&mblks[nmblks], 0, ML_INC * sizeof (mbl_t *)); 276 nmblks += ML_INC; 277 } 278 return (xgetblk(&mblks[l], s)); 279} 280 281void * | 260 while (l >= nmblks) { 261 if ((mblks = realloc(mblks, (nmblks + ML_INC) * 262 sizeof (mbl_t *))) == NULL) 263 nomem(); 264 (void)memset(&mblks[nmblks], 0, ML_INC * sizeof (mbl_t *)); 265 nmblks += ML_INC; 266 } 267 return (xgetblk(&mblks[l], s)); 268} 269 270void * |
282getblk(s) 283 size_t s; | 271getblk(size_t s) |
284{ | 272{ |
273 |
|
285 return (getlblk(mblklev, s)); 286} 287 288/* 289 * Free all memory associated with level l. 290 */ 291void | 274 return (getlblk(mblklev, s)); 275} 276 277/* 278 * Free all memory associated with level l. 279 */ 280void |
292freelblk(l) 293 int l; | 281freelblk(int l) |
294{ | 282{ |
283 |
|
295 xfreeblk(&mblks[l]); 296} 297 298void | 284 xfreeblk(&mblks[l]); 285} 286 287void |
299freeblk() | 288freeblk(void) |
300{ | 289{ |
290 |
|
301 freelblk(mblklev); 302} 303 304/* 305 * tgetblk() returns memory which is associated with the current 306 * expression. 307 */ 308static mbl_t *tmblk; 309 310void * | 291 freelblk(mblklev); 292} 293 294/* 295 * tgetblk() returns memory which is associated with the current 296 * expression. 297 */ 298static mbl_t *tmblk; 299 300void * |
311tgetblk(s) 312 size_t s; | 301tgetblk(size_t s) |
313{ | 302{ |
303 |
|
314 return (xgetblk(&tmblk, s)); 315} 316 317/* 318 * Get memory for a new tree node. 319 */ 320tnode_t * | 304 return (xgetblk(&tmblk, s)); 305} 306 307/* 308 * Get memory for a new tree node. 309 */ 310tnode_t * |
321getnode() | 311getnode(void) |
322{ | 312{ |
313 |
|
323 return (tgetblk(sizeof (tnode_t))); 324} 325 326/* | 314 return (tgetblk(sizeof (tnode_t))); 315} 316 317/* |
327 * Free all memory which is allocated by the the current expression. | 318 * Free all memory which is allocated by the current expression. |
328 */ 329void | 319 */ 320void |
330tfreeblk() | 321tfreeblk(void) |
331{ | 322{ |
323 |
|
332 xfreeblk(&tmblk); 333} 334 335/* 336 * Save the memory which is used by the current expression. This memory 337 * is not freed by the next tfreeblk() call. The pointer returned can be 338 * used to restore the memory. 339 */ 340mbl_t * | 324 xfreeblk(&tmblk); 325} 326 327/* 328 * Save the memory which is used by the current expression. This memory 329 * is not freed by the next tfreeblk() call. The pointer returned can be 330 * used to restore the memory. 331 */ 332mbl_t * |
341tsave() | 333tsave(void) |
342{ 343 mbl_t *tmem; 344 345 tmem = tmblk; 346 tmblk = NULL; 347 return (tmem); 348} 349 350/* 351 * Free all memory used for the current expression and the memory used 352 * be a previous expression and saved by tsave(). The next call to 353 * tfreeblk() frees the restored memory. 354 */ 355void | 334{ 335 mbl_t *tmem; 336 337 tmem = tmblk; 338 tmblk = NULL; 339 return (tmem); 340} 341 342/* 343 * Free all memory used for the current expression and the memory used 344 * be a previous expression and saved by tsave(). The next call to 345 * tfreeblk() frees the restored memory. 346 */ 347void |
356trestor(tmem) 357 mbl_t *tmem; | 348trestor(mbl_t *tmem) |
358{ | 349{ |
350 |
|
359 tfreeblk(); 360 if (tmblk != NULL) { 361 free(tmblk->blk); 362 free(tmblk); 363 } 364 tmblk = tmem; 365} | 351 tfreeblk(); 352 if (tmblk != NULL) { 353 free(tmblk->blk); 354 free(tmblk); 355 } 356 tmblk = tmem; 357} |