diff --git a/include/nickel/xalloc.h b/include/nickel/xalloc.h index 563c383..122f8d8 100644 --- a/include/nickel/xalloc.h +++ b/include/nickel/xalloc.h @@ -13,7 +13,6 @@ extern "C" { #endif void* ni_xmalloc(size_t size); -void ni_free(void* ptr); void* ni_xcalloc(size_t nmemb,size_t size); void* ni_xrealloc(void* ptr, size_t size); diff --git a/src/nickel/xalloc.c b/src/nickel/xalloc.c index c48cba0..6d2e2b3 100644 --- a/src/nickel/xalloc.c +++ b/src/nickel/xalloc.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -8,25 +9,56 @@ #include "nickel/xalloc.h" void* ni_xmalloc(size_t size) { + void* retptr = malloc(size); -} - -void ni_free(void* ptr) { - + if (retptr != NULL) { + return retptr; + } else { + perror("xmalloc failed"); + exit(EXIT_FAILURE); + } } void* ni_xcalloc(size_t nmemb,size_t size) { + void* retptr = calloc(nmemb, size); + if (retptr != NULL) { + return retptr; + } else { + perror("xcalloc failed"); + exit(EXIT_FAILURE); + } } void* ni_xrealloc(void* ptr, size_t size) { + void* retptr = realloc(ptr, size); + if (retptr != NULL) { + return retptr; + } else { + perror("xrealloc failed"); + exit(EXIT_FAILURE); + } } char* ni_xstrdup(const char *s) { - + void* dupstr = strdup(s); + + if (dupstr != NULL) { + return dupstr; + } else { + perror("xdupstr failed"); + exit(EXIT_FAILURE); + } } char* ni_xstrndup(const char s[], size_t n) { + void* dupstr = strndup(s, n); + if (dupstr != NULL) { + return dupstr; + } else { + perror("xdupstr failed"); + exit(EXIT_FAILURE); + } } \ No newline at end of file