File: xmalloc.c

package info (click to toggle)
hx 0.7.14-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 564 kB
  • ctags: 834
  • sloc: ansic: 7,901; sh: 152; makefile: 81
file content (59 lines) | stat: -rw-r--r-- 730 bytes parent folder | download | duplicates (3)
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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include "xmalloc.h"

extern void term_printf (const char *fmt, ...);
extern void hx_exit (int);

static void
xdie (size_t size)
{
	term_printf("xmalloc: could not allocate %lu bytes\n", (unsigned long)size);
	hx_exit(111);
}

void *
xmalloc (size_t size)
{
	void *p;

	if (!(p = malloc(size)))
		xdie(size);

	return p;
}

void *
xrealloc (void *ptr, size_t size)
{
	void *p;

	if (!(p = ptr ? realloc(ptr, size) : malloc(size)))
		xdie(size);

	return p;
}

void
xfree (void *ptr)
{
	if (ptr)
		free(ptr);
}

char *
xstrdup (const char *str)
{
	char *p;

	if (!str)
		return 0;
	strcpy((p = xmalloc(strlen(str) + 1)), str);

	return p;
}