xref: /original-bsd/contrib/sc/xmalloc.c (revision b30b9691)
1 /*
2  * A safer saner malloc, for careless programmers
3  * $Revision: 6.8 $
4  */
5 
6 #include <stdio.h>
7 #include <curses.h>
8 #include "sc.h"
9 
10 extern char *malloc();
11 
12 #ifdef SYSV3
13 extern void free();
14 extern void exit();
15 #endif
16 
17 char *
18 xmalloc(n)
19 unsigned n;
20 {
21 register char *ptr;
22 
23 if ((ptr = malloc(n + sizeof(double))) == NULL)
24     fatal("xmalloc: no memory");
25 *((int *) ptr) = 12345;		/* magic number */
26 return(ptr + sizeof(double));
27 }
28 
29 xfree(p)
30 char *p;
31 {
32 if (p == NULL)
33     fatal("xfree: NULL");
34 p -= sizeof(double);
35 if (*((int *) p) != 12345)
36     fatal("xfree: storage not malloc'ed");
37 free(p);
38 }
39 
40 fatal(str)
41 char *str;
42 {
43     deraw();
44     (void) fprintf(stderr,"%s\n", str);
45     exit(1);
46 }
47