From 4da05d05c943f07a858a4b04cf2c94a07b84cdbb Mon Sep 17 00:00:00 2001 From: "Chloe M." Date: Thu, 30 Apr 2026 08:51:19 -0400 Subject: [PATCH] core: Add xmalloc() implementation Signed-off-by: Chloe M. --- core/xmalloc.c | 24 ++++++++++++++++++++++++ inc/blobchain/memlib.h | 18 ++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 core/xmalloc.c create mode 100644 inc/blobchain/memlib.h diff --git a/core/xmalloc.c b/core/xmalloc.c new file mode 100644 index 0000000..464605b --- /dev/null +++ b/core/xmalloc.c @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#include +#include +#include +#include +#include "blobchain/memlib.h" + +void * +xmalloc(size_t sz) +{ + void *p; + + assert(sz != 0 && "xmalloc size cannot be zero!"); + if ((p = malloc(sz)) == NULL) { + printf("fatal: out of memory\n"); + exit(1); + } + + return p; +} diff --git a/inc/blobchain/memlib.h b/inc/blobchain/memlib.h new file mode 100644 index 0000000..d9635c8 --- /dev/null +++ b/inc/blobchain/memlib.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#ifndef BLOBCHAIN_MEMLIB_H +#define BLOBCHAIN_MEMLIB_H 1 + +/* + * Allocate memory and exit upon failure + * + * @sz: Number of bytes to allocate + * + * Always returns on success + */ +void *xmalloc(size_t sz); + +#endif /* !BLOBCHAIN_MEMLIB_H */