alloc.c
author tomas@localhost
Thu, 01 Nov 2007 14:46:11 +0100
changeset 0 c045670f36e9
permissions -rw-r--r--
Imported queue-fix-1.4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     1
#include "alloc.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     2
#include "error.h"
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     3
extern char *malloc();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     4
extern void free();
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     5
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     6
#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     7
#define SPACE 4096 /* must be multiple of ALIGNMENT */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     8
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
     9
typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    10
static aligned realspace[SPACE / ALIGNMENT];
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    11
#define space ((char *) realspace)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    12
static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    13
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    14
/*@null@*//*@out@*/char *alloc(n)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    15
unsigned int n;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    16
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    17
  char *x;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    18
  n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    19
  if (n <= avail) { avail -= n; return space + avail; }
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    20
  x = malloc(n);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    21
  if (!x) errno = error_nomem;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    22
  return x;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    23
}
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    24
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    25
void alloc_free(x)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    26
char *x;
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    27
{
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    28
  if (x >= space)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    29
    if (x < space + SPACE)
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    30
      return; /* XXX: assuming that pointers are flat */
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    31
  free(x);
c045670f36e9 Imported queue-fix-1.4
tomas@localhost
parents:
diff changeset
    32
}