add malloc wrapper
parent
4dbc77845a
commit
7df80d8cd1
@ -0,0 +1,60 @@
|
||||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "neo/_toolchain.h"
|
||||
#include "neo/_types.h"
|
||||
|
||||
void nfree(void *ptr);
|
||||
|
||||
/**
|
||||
* Allocate `size` bytes of memory and return a pointer to the memory region.
|
||||
* The memory is *not* initialized; use `nzalloc` if you want it to be.
|
||||
* If `size` is 0, the allocation fails.
|
||||
* If the allocation fails, the error is set and `nil` is returned.
|
||||
*
|
||||
* @param size: Desired memory size in bytes
|
||||
* @param err: Error object
|
||||
*/
|
||||
__neo_malloc(nfree, 1)
|
||||
void *nalloc(usize size, error *err);
|
||||
|
||||
/**
|
||||
* Allocate `size` bytes of memory and return a pointer to the memory region.
|
||||
* The memory is initialized to zeroes; use `nalloc` if you don't want that.
|
||||
* If `size` is 0, the allocation fails.
|
||||
* If the allocation fails, the error is set and `nil` is returned.
|
||||
*
|
||||
* @param size: Desired memory size in bytes
|
||||
* @param err: Error object
|
||||
*/
|
||||
__neo_malloc(nfree, 1)
|
||||
void *nzalloc(usize size, error *err);
|
||||
|
||||
/**
|
||||
* Resize an allocated memory region to fit at least `newsize` bytes and return
|
||||
* the updated pointer. The old pointer becomes invalid.
|
||||
* If `newsize` is 0, the memory is released and `nil` is returned.
|
||||
* If `ptr` is `nil`, the function behaves like `nalloc`.
|
||||
* If allocation fails, an error is yeeted.
|
||||
*
|
||||
* @param ptr: The pointer to the memory region to be expanded,
|
||||
* as returned by `nalloc` or `nzalloc`.
|
||||
* @param newsize: The new size.
|
||||
* @param err: Error object
|
||||
*/
|
||||
__neo_malloc(nfree, 1)
|
||||
void *nrealloc(void *ptr, usize newsize, error *err);
|
||||
|
||||
/*
|
||||
* This file is part of libneo.
|
||||
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
*
|
||||
* libneo is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
@ -0,0 +1,87 @@
|
||||
/** See the end of this file for copyright and license terms. */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "neo/_error.h"
|
||||
#include "neo/_nalloc.h"
|
||||
#include "neo/_stddef.h"
|
||||
#include "neo/_types.h"
|
||||
|
||||
void nfree(void *ptr)
|
||||
{
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
/*
|
||||
* the error messages are nil if malloc fails because if we are running low
|
||||
* on memory, vsnprintf (which yeet() uses) is probably gonna fail as well
|
||||
*/
|
||||
|
||||
void *nalloc(usize size, error *err)
|
||||
{
|
||||
if (size == 0) {
|
||||
yeet(err, EINVAL, "Requested memory size is 0");
|
||||
return nil;
|
||||
}
|
||||
|
||||
void *ptr = malloc(size);
|
||||
|
||||
if (!ptr)
|
||||
yeet(err, ENOMEM, nil);
|
||||
else
|
||||
neat(err);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *zalloc(usize size, error *err)
|
||||
{
|
||||
if (size == 0) {
|
||||
yeet(err, EINVAL, "Requested memory size is 0");
|
||||
return nil;
|
||||
}
|
||||
|
||||
void *ptr = calloc(1, size);
|
||||
|
||||
if (!ptr)
|
||||
yeet(err, ENOMEM, nil);
|
||||
else
|
||||
neat(err);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *nrealloc(void *ptr, usize newsz, error *err)
|
||||
{
|
||||
void *ret = realloc(ptr, newsz);
|
||||
|
||||
if (!ret) {
|
||||
if (newsz == 0) {
|
||||
ret = nil;
|
||||
neat(err);
|
||||
} else {
|
||||
ret = ptr;
|
||||
yeet(err, ENOMEM, nil);
|
||||
}
|
||||
} else {
|
||||
neat(err);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* This file is part of libneo.
|
||||
* Copyright (c) 2021 Fefie <owo@fef.moe>.
|
||||
*
|
||||
* libneo is non-violent software: you may only use, redistribute,
|
||||
* and/or modify it under the terms of the CNPLv6+ as found in
|
||||
* the LICENSE file in the source code root directory or at
|
||||
* <https://git.pixie.town/thufie/CNPL>.
|
||||
*
|
||||
* libneo comes with ABSOLUTELY NO WARRANTY, to the extent
|
||||
* permitted by applicable law. See the CNPLv6+ for details.
|
||||
*/
|
Loading…
Reference in New Issue