runit

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

alloc.c (823B)


      1 /* Public domain. */
      2 
      3 #include <stdlib.h>
      4 #include "alloc.h"
      5 #include "error.h"
      6 
      7 #define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */
      8 #define SPACE 2048 /* must be multiple of ALIGNMENT */
      9 
     10 typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
     11 static aligned realspace[SPACE / ALIGNMENT];
     12 #define space ((char *) realspace)
     13 static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
     14 
     15 /*@null@*//*@out@*/char *alloc( size_t 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( char* x)
     26 {
     27   if (x >= space)
     28     if (x < space + SPACE)
     29       return; /* XXX: assuming that pointers are flat */
     30   free(x);
     31 }