summaryrefslogtreecommitdiff
path: root/command_line/CmdLine/Src/OSLib/MemUtils.c
diff options
context:
space:
mode:
authorAsh Wolf <ninji@wuffs.org>2022-10-12 18:07:57 +0100
committerAsh Wolf <ninji@wuffs.org>2022-10-12 18:07:57 +0100
commit945f73751630db0420522b5d6af0629d90376a45 (patch)
tree4b374c13d61dd0f54abefa49c6c348d6a56c6a52 /command_line/CmdLine/Src/OSLib/MemUtils.c
parentf080c0e765878ae91949d91a0bc10b87e6c8269b (diff)
downloadMWCC-945f73751630db0420522b5d6af0629d90376a45.tar.gz
MWCC-945f73751630db0420522b5d6af0629d90376a45.zip
almost finished OSLib
Diffstat (limited to 'command_line/CmdLine/Src/OSLib/MemUtils.c')
-rw-r--r--command_line/CmdLine/Src/OSLib/MemUtils.c41
1 files changed, 37 insertions, 4 deletions
diff --git a/command_line/CmdLine/Src/OSLib/MemUtils.c b/command_line/CmdLine/Src/OSLib/MemUtils.c
index 4eb611c..fcacd81 100644
--- a/command_line/CmdLine/Src/OSLib/MemUtils.c
+++ b/command_line/CmdLine/Src/OSLib/MemUtils.c
@@ -1,21 +1,54 @@
#include "oslib.h"
void *xmalloc(const char *what, int size) {
-
+ void *ret;
+
+ ret = malloc(size ? size : 1);
+ if (!ret) {
+ fprintf(
+ stderr,
+ "*** Out of memory when allocating %d bytes%s%s",
+ size,
+ what ? " for " : "",
+ what ? what : "");
+ exit(-23);
+ return 0;
+ }
+
+ return ret;
}
void *xcalloc(const char *what, int size) {
+ void *ret;
+ ret = xmalloc(what, size);
+ memset(ret, 0, size);
+ return ret;
}
void *xrealloc(const char *what, void *old, int size) {
-
+ void *ret;
+
+ ret = realloc(old, size ? size : 1);
+ if (!ret) {
+ fprintf(
+ stderr,
+ "*** Out of memory when resizing buffer to %d bytes%s%s",
+ size,
+ what ? " for " : "",
+ what ? what : "");
+ exit(-23);
+ return 0;
+ }
+
+ return ret;
}
char *xstrdup(const char *str) {
-
+ return strcpy(xmalloc(0, strlen(str) + 1), str);
}
void xfree(void *ptr) {
-
+ if (ptr)
+ free(ptr);
}