s/qmail  3.3.23
Next generation secure email transport
alloc.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdint.h>
3 #include "alloc.h"
4 #include "error.h"
5 extern void *malloc();
6 extern void free();
7 
8 #define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */
9 #define SPACE 4096 /* must be multiple of ALIGNMENT */
10 
11 typedef union { char irrelevant[ALIGNMENT]; double d; } aligned;
12 static aligned realspace[SPACE / ALIGNMENT];
13 #define space ((char *) realspace)
14 static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
15 
16 /*@null@*//*@out@*/char *alloc(unsigned int n)
17 {
18  char *x;
19  n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */
20  if (n <= avail) { avail -= n; return space + avail; }
21  x = malloc(n);
22  if (!x) errno = error_nomem;
23  return x;
24 }
25 
26 void alloc_free(char *x)
27 {
28  if (x >= space)
29  if (x < space + SPACE)
30  return; /* XXX: assuming that pointers are flat */
31  free(x);
32 }
char * alloc(unsigned int n)
Definition: alloc.c:16
int error_nomem
Definition: error.c:13
#define space
Definition: alloc.c:13
#define ALIGNMENT
Definition: alloc.c:8
struct del * d[CHANNELS]
Definition: qmail-send.c:716
Definition: alloc.c:11
int errno
void free()
unsigned x
Definition: matchup.c:36
#define SPACE
Definition: alloc.c:9
unsigned n
Definition: matchup.c:36
void alloc_free(char *x)
Definition: alloc.c:26
void * malloc()