ucspi-ssl  0.99e
TLS encryption for IPv6 communication
alloc.c
Go to the documentation of this file.
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(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(char *x)
26 {
27  if (x >= space)
28  if (x < space + SPACE)
29  return; /* XXX: assuming that pointers are flat */
30  free(x);
31 }
char * alloc(unsigned int n)
Definition: alloc.c:15
int error_nomem
Definition: error.c:15
#define space
Definition: alloc.c:12
#define ALIGNMENT
Definition: alloc.c:7
Definition: alloc.c:10
#define SPACE
Definition: alloc.c:8
void alloc_free(char *x)
Definition: alloc.c:25