3 ms·
djb's qmail does almost exactly this! It works surprisingly well for short-lived forked processes. typedef union { char irrelevant[ALIGNMENT]; double d; } al
by cpeterso 13y ago
djb's qmail does almost exactly this! It works surprisingly well for short-lived forked processes.
typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
static aligned realspace[SPACE / ALIGNMENT];
#define space ((char *) realspace)
static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
char *alloc(n)
unsigned int n;
{
n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */
if (n <= avail) {
avail -= n;
return space + avail;
}
return malloc(n);
}
void alloc_free(x)
char *x;
{
if (x >= space && x < space + SPACE) {
return; /* XXX: assuming that pointers are flat */
}
free(x);
}
- a-priori 13y agoCool, didn't know that! It's basically a degenerate pooled memory allocator. That one is a bit more clever in that it falls through to a proper malloc when it runs out of space. That's assuming, of course, that you have a proper malloc to fall through too. Mine will eventually wrap around and clobber whatever's at 0x0. It assumes you have a very liberal page allocator in the background to handle page faults for whatever addresses you decide to point to and automatically wire them up to RAM.