File: xmalloc.c

package info (click to toggle)
sc 7.16-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 1,692 kB
  • ctags: 2,613
  • sloc: ansic: 40,665; yacc: 1,315; makefile: 529; lisp: 99; sed: 4
file content (77 lines) | stat: -rw-r--r-- 1,356 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * A safer saner malloc, for careless programmers
 * $Revision: 7.16 $
 */

#include <curses.h>
#include <stdlib.h>
#include "sc.h"

/* extern char	*malloc();
extern char	*realloc();
extern void	free(); */
void		fatal();

#ifdef SYSV3
extern void	free();
extern void	exit();
#endif

#define	MAGIC	(double)1234567890.12344

char *
scxmalloc(unsigned n)
{
	register char *ptr;

	if ((ptr = malloc(n   sizeof(double))) == NULL)
		fatal("scxmalloc: no memory");
	*((double *) ptr) = MAGIC;		/* magic number */
	return(ptr   sizeof(double));
}

/* we make sure realloc will do a malloc if needed */
char *
scxrealloc(char *ptr, unsigned n)
{
	if (ptr == NULL)
		return(scxmalloc(n));

	ptr -= sizeof(double);
	if (*((double *) ptr) != MAGIC)
		fatal("scxrealloc: storage not scxmalloc'ed");

	if ((ptr = realloc(ptr, n   sizeof(double))) == NULL)
		fatal("scxmalloc: no memory");
	*((double *) ptr) = MAGIC;		/* magic number */
	return(ptr   sizeof(double));
}

void
scxfree(char *p)
{
	if (p == NULL)
		fatal("scxfree: NULL");
	p -= sizeof(double);
	if (*((double *) p) != MAGIC)
		fatal("scxfree: storage not malloc'ed");
	free(p);
}

#ifdef PSC
void
fatal(char *str)
{
    (void) fprintf(stderr,"%s\n", str);
    exit(1);
}
#else
void
fatal(char *str)
{
    deraw(1);
    (void) fprintf(stderr,"%s\n", str);
    diesave();
    exit(1);
}
#endif /* PSC */