-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxmalloc.c
136 lines (118 loc) · 2.44 KB
/
xmalloc.c
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* xmalloc.c
* Copyright (C) 1998-2005 A.J. van Os
*
* Description:
* Extended malloc and friends
*/
#include <stdlib.h>
#include <string.h>
#include "antiword.h"
static char *szMessage =
"Memory allocation failed, unable to continue";
#if defined(__dos) && !defined(__DJGPP__)
static char *szDosMessage =
"DOS can't allocate this kind of memory, unable to continue";
#endif /* __dos && !__DJGPP__ */
/*
* xmalloc - Allocates dynamic memory
*
* See malloc(3), but unlike malloc(3) xmalloc does not return in case
* of error.
*/
void *
xmalloc(size_t tSize)
{
void *pvTmp;
TRACE_MSG("xmalloc");
if (tSize == 0) {
tSize = 1;
}
pvTmp = malloc(tSize);
if (pvTmp == NULL) {
DBG_MSG("xmalloc returned NULL");
DBG_DEC(tSize);
werr(1, szMessage);
}
return pvTmp;
} /* end of xmalloc */
/*
* xcalloc - Allocates and zeros dynamic memory
*
* See calloc(3), but unlike calloc(3) xcalloc does not return in case of error
*/
void *
xcalloc(size_t tNmemb, size_t tSize)
{
void *pvTmp;
TRACE_MSG("xcalloc");
#if defined(__dos) && !defined(__DJGPP__)
if ((ULONG)tNmemb * (ULONG)tSize > 0xffffUL) {
DBG_DEC((ULONG)tNmemb * (ULONG)tSize);
werr(1, szDosMessage);
}
#endif /* __dos && !__DJGPP__ */
if (tNmemb == 0 || tSize == 0) {
tNmemb = 1;
tSize = 1;
}
pvTmp = calloc(tNmemb, tSize);
if (pvTmp == NULL) {
DBG_MSG("xcalloc returned NULL");
werr(1, szMessage);
}
return pvTmp;
} /* end of xcalloc */
/*
* xrealloc - Changes the size of a memory object
*
* See realloc(3), but unlike realloc(3) xrealloc does not return in case
* of error.
*/
void *
xrealloc(void *pvArg, size_t tSize)
{
void *pvTmp;
TRACE_MSG("xrealloc");
pvTmp = realloc(pvArg, tSize);
if (pvTmp == NULL) {
DBG_MSG("realloc returned NULL");
werr(1, szMessage);
}
return pvTmp;
} /* end of xrealloc */
/*
* xstrdup - Duplicate a string
*
* See strdup(3), but unlike strdup(3) xstrdup does not return in case
* of error.
*
* NOTE:
* Does not use strdup(3), because some systems don't have it.
*/
char *
xstrdup(const char *szArg)
{
char *szTmp;
TRACE_MSG("xstrdup");
szTmp = xmalloc(strlen(szArg) + 1);
strcpy(szTmp, szArg);
return szTmp;
} /* end of xstrdup */
/*
* xfree - Deallocates dynamic memory
*
* See free(3).
*
* returns NULL;
* This makes p=xfree(p) possible, free memory and overwrite the pointer to it.
*/
void *
xfree(void *pvArg)
{
TRACE_MSG("xfree");
if (pvArg != NULL) {
free(pvArg);
}
return NULL;
} /* end of xfree */