xalloc impl...maybe. needs testing.

This commit is contained in:
2026-01-15 13:36:56 -05:00
parent 30273486f1
commit cbd01e7a47
2 changed files with 37 additions and 6 deletions

View File

@@ -13,7 +13,6 @@ extern "C" {
#endif #endif
void* ni_xmalloc(size_t size); void* ni_xmalloc(size_t size);
void ni_free(void* ptr);
void* ni_xcalloc(size_t nmemb,size_t size); void* ni_xcalloc(size_t nmemb,size_t size);
void* ni_xrealloc(void* ptr, size_t size); void* ni_xrealloc(void* ptr, size_t size);

View File

@@ -1,5 +1,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
@@ -8,25 +9,56 @@
#include "nickel/xalloc.h" #include "nickel/xalloc.h"
void* ni_xmalloc(size_t size) { void* ni_xmalloc(size_t size) {
void* retptr = malloc(size);
} if (retptr != NULL) {
return retptr;
void ni_free(void* ptr) { } else {
perror("xmalloc failed");
exit(EXIT_FAILURE);
}
} }
void* ni_xcalloc(size_t nmemb,size_t size) { 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* 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) { 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) { 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);
}
} }