aboutsummaryrefslogtreecommitdiff
blob: 40a47626573676c59599c84760fa5b4409bacc8e (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * debug.c
 *
 * Simle debugging/logging macro's and functions.
 *
 * Copyright 1999-2008 Gentoo Foundation
 * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org>
 * Licensed under the GPL-2
 */

#include "headers.h"
#include "sbutil.h"

void *
__xcalloc(size_t nmemb, size_t size, const char *file, const char *func, size_t line)
{
	void *ret = calloc(nmemb, size);

	if (ret == NULL) {
		SB_EERROR("calloc()", " %s:%s():%zu: calloc(%zu, %zu) failed: %s\n",
			file, func, line, nmemb, size, strerror(errno));
		abort();
	}

	return ret;
}

void *
__xmalloc(size_t size, const char *file, const char *func, size_t line)
{
	void *ret = malloc(size);

	if (ret == NULL) {
		SB_EERROR("malloc()", " %s:%s():%zu: malloc(%zu) failed: %s\n",
			file, func, line, size, strerror(errno));
		abort();
	}

	return ret;
}

void *
__xzalloc(size_t size /*, const char *file, const char *func, size_t line */)
{
	return memset(xmalloc(size), 0x00, size);
}

void *
__xrealloc(void *ptr, size_t size, const char *file, const char *func, size_t line)
{
	void *ret = realloc(ptr, size);

	if (ret == NULL) {
		SB_EERROR("realloc()", " %s:%s():%zu: realloc(%p, %zu) failed: %s\n",
			file, func, line, ptr, size, strerror(errno));
		abort();
	}

	return ret;
}

char *
__xstrdup(const char *str, const char *file, const char *func, size_t line)
{
	char *ret = strdup(str);

	if (ret == NULL) {
		SB_EERROR("strdup()", " %s:%s():%zu: strdup(%p) failed: %s\n",
			file, func, line, str, strerror(errno));
		abort();
	}

	return ret;
}

#ifndef HAVE_STRNDUP
static inline char *sb_strndup(const char *str, size_t n)
{
	size_t r;
	for (r = 0; r < n; ++r)
		if (!str[r])
			break;

	char *ret = xmalloc(r + 1);
	memcpy(ret, str, r);
	ret[r] = '\0';
	return ret;
}
# define strndup sb_strndup
#endif

char *
__xstrndup(const char *str, size_t size, const char *file, const char *func, size_t line)
{
	char *ret = strndup(str, size);

	if (ret == NULL) {
		SB_EERROR("strndup()", " %s:%s():%zu: strndup(%p, %zu) failed: %s\n",
			file, func, line, str, size, strerror(errno));
		abort();
	}

	return ret;
}