add malloc wrapper

main
anna 3 years ago
parent 4dbc77845a
commit 7df80d8cd1
Signed by: fef
GPG Key ID: EC22E476DC2D3D84

@ -9,6 +9,7 @@
#include "neo/_error.h"
#include "neo/_types.h"
#include "neo/_stddef.h"
#include "neo/_nalloc.h"
/*
* This file is part of libneo.

@ -19,7 +19,7 @@ void yeet(error *err, int number, const char *restrict fmt, ...);
/**
* Functions accepting an error pointer must call this to indicate success.
*/
#define succeed(errptr) ({ \
#define neat(errptr) ({ \
if (errptr) \
*(errptr) = nil; \
})

@ -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.
*/

@ -2,6 +2,14 @@
#pragma once
#ifdef __clang__ /* clang doesn't support specifying a deallocator qwq */
#define __neo_malloc(deallocator, argindex) \
__attribute__(( __malloc__ ))
#else
#define __neo_malloc(deallocator, argindex) \
__attribute__(( __malloc__, __malloc__(deallocator, argindex) ))
#endif
#define __neo_section(name) __attribute__(( __section__(#name) ))
#define __neo_init(fn) \

@ -26,6 +26,7 @@ target_include_directories(neo PRIVATE
target_sources(neo PRIVATE
./error.c
./nalloc.c
./nref.c
)

@ -4,13 +4,15 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "neo/_error.h"
#include "neo/_nalloc.h"
#include "neo/_nref.h"
#include "neo/_stddef.h"
#include "neo/_string.h"
#include "neo/_toolchain.h"
#include "neo/_types.h"
#include "neo/toolchain.h"
/* flags for the error structure */
#define NEO_ERR_CAUGHT (1 << 0)

@ -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…
Cancel
Save