core: Add xmalloc() implementation
Signed-off-by: Chloe M. <chloe@mirocom.org>
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2026, Chloe M.
|
||||
* Provided under the BSD-3 clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <assert.h>
|
||||
#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;
|
||||
}
|
||||
@@ -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 */
|
||||
Reference in New Issue
Block a user