version 1.26, 2011/04/24 02:23:57 |
version 1.27, 2014/07/05 05:05:51 |
|
|
/* |
/* |
* Malloc with error checking |
* Malloc with error checking |
*/ |
*/ |
char * |
void * |
xmalloc(size_t amt) |
xmalloc(size_t amt) |
{ |
{ |
char *ptr; |
void *ptr; |
|
|
if ((ptr = (char *)malloc(amt)) == NULL) |
if ((ptr = malloc(amt)) == NULL) |
fatalerr("Cannot malloc %zu bytes of memory.", amt); |
fatalerr("Cannot malloc %zu bytes of memory.", amt); |
|
|
return(ptr); |
return (ptr); |
} |
} |
|
|
/* |
/* |
* realloc with error checking |
* realloc with error checking |
*/ |
*/ |
char * |
void * |
xrealloc(char *baseptr, size_t amt) |
xrealloc(void *baseptr, size_t amt) |
{ |
{ |
char *new; |
void *new; |
|
|
if ((new = (char *)realloc(baseptr, amt)) == NULL) |
if ((new = realloc(baseptr, amt)) == NULL) |
fatalerr("Cannot realloc %zu bytes of memory.", amt); |
fatalerr("Cannot realloc %zu bytes of memory.", amt); |
|
|
return(new); |
return (new); |
} |
} |
|
|
/* |
/* |
* calloc with error checking |
* calloc with error checking |
*/ |
*/ |
char * |
void * |
xcalloc(size_t num, size_t esize) |
xcalloc(size_t num, size_t esize) |
{ |
{ |
char *ptr; |
void *ptr; |
|
|
if ((ptr = (char *)calloc(num, esize)) == NULL) |
if ((ptr = calloc(num, esize)) == NULL) |
fatalerr("Cannot calloc %zu * %zu = %zu bytes of memory.", |
fatalerr("Cannot calloc %zu * %zu = %zu bytes of memory.", |
num, esize, num * esize); |
num, esize, num * esize); |
|
|
return(ptr); |
return (ptr); |
} |
} |
|
|
/* |
/* |
|
|
xstrdup(const char *str) |
xstrdup(const char *str) |
{ |
{ |
size_t len = strlen(str) + 1; |
size_t len = strlen(str) + 1; |
char *nstr = (char *) malloc(len); |
char *nstr = xmalloc(len); |
|
|
if (nstr == NULL) |
return (memcpy(nstr, str, len)); |
fatalerr("Cannot malloc %zu bytes of memory.", len); |
|
|
|
return(memcpy(nstr, str, len)); |
|
} |
} |
|
|
/* |
/* |