| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "makeint.h" |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#ifndef HAVE_DMALLOC_H |
| 23 |
|
| 24 |
#undef xmalloc |
| 25 |
#undef xcalloc |
| 26 |
#undef xrealloc |
| 27 |
#undef xstrdup |
| 28 |
|
| 29 |
void * |
| 30 |
xmalloc (size_t size) |
| 31 |
{ |
| 32 |
|
| 33 |
void *result = malloc (size ? size : 1); |
| 34 |
if (result == 0) |
| 35 |
OUT_OF_MEM(); |
| 36 |
return result; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
void * |
| 41 |
xcalloc (size_t size) |
| 42 |
{ |
| 43 |
|
| 44 |
void *result = calloc (size ? size : 1, 1); |
| 45 |
if (result == 0) |
| 46 |
OUT_OF_MEM(); |
| 47 |
return result; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
void * |
| 52 |
xrealloc (void *ptr, size_t size) |
| 53 |
{ |
| 54 |
void *result; |
| 55 |
|
| 56 |
|
| 57 |
if (! size) |
| 58 |
size = 1; |
| 59 |
result = ptr ? realloc (ptr, size) : malloc (size); |
| 60 |
if (result == 0) |
| 61 |
OUT_OF_MEM(); |
| 62 |
return result; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
char * |
| 67 |
xstrdup (const char *ptr) |
| 68 |
{ |
| 69 |
char *result; |
| 70 |
|
| 71 |
#ifdef HAVE_STRDUP |
| 72 |
result = strdup (ptr); |
| 73 |
#else |
| 74 |
result = malloc (strlen (ptr) + 1); |
| 75 |
#endif |
| 76 |
|
| 77 |
if (result == 0) |
| 78 |
OUT_OF_MEM(); |
| 79 |
|
| 80 |
#ifdef HAVE_STRDUP |
| 81 |
return result; |
| 82 |
#else |
| 83 |
return strcpy (result, ptr); |
| 84 |
#endif |
| 85 |
} |
| 86 |
|
| 87 |
#endif |
| 88 |
|
| 89 |
char * |
| 90 |
xstrndup (const char *str, size_t length) |
| 91 |
{ |
| 92 |
char *result; |
| 93 |
|
| 94 |
#ifdef HAVE_STRNDUP |
| 95 |
result = strndup (str, length); |
| 96 |
if (result == 0) |
| 97 |
OUT_OF_MEM(); |
| 98 |
#else |
| 99 |
result = xmalloc (length + 1); |
| 100 |
if (length > 0) |
| 101 |
strncpy (result, str, length); |
| 102 |
result[length] = '\0'; |
| 103 |
#endif |
| 104 |
|
| 105 |
return result; |
| 106 |
} |