Mercurial > hg > cdb
comparison alloc.c @ 0:2cb46628feec
from cdb-0.75 per http://cr.yp.to/cdb.html,
retrieved 2025-01-03,
contents appear to have been create on 2000-02-19
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Wed, 29 Jan 2025 10:50:34 +0000 |
| parents | |
| children | 3bfef1f22a97 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:2cb46628feec |
|---|---|
| 1 #include "alloc.h" | |
| 2 #include "error.h" | |
| 3 extern char *malloc(); | |
| 4 extern void free(); | |
| 5 | |
| 6 #define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */ | |
| 7 #define SPACE 4096 /* must be multiple of ALIGNMENT */ | |
| 8 | |
| 9 typedef union { char irrelevant[ALIGNMENT]; double d; } aligned; | |
| 10 static aligned realspace[SPACE / ALIGNMENT]; | |
| 11 #define space ((char *) realspace) | |
| 12 static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */ | |
| 13 | |
| 14 /*@null@*//*@out@*/char *alloc(n) | |
| 15 unsigned int n; | |
| 16 { | |
| 17 char *x; | |
| 18 n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */ | |
| 19 if (n <= avail) { avail -= n; return space + avail; } | |
| 20 x = malloc(n); | |
| 21 if (!x) errno = error_nomem; | |
| 22 return x; | |
| 23 } | |
| 24 | |
| 25 void alloc_free(x) | |
| 26 char *x; | |
| 27 { | |
| 28 if (x >= space) | |
| 29 if (x < space + SPACE) | |
| 30 return; /* XXX: assuming that pointers are flat */ | |
| 31 free(x); | |
| 32 } |
