New allocators, testing framework, overall improvements

This commit is contained in:
2026-08-22 02:13:04 -05:00
parent 69437a14ad
commit c1c603a250
24 changed files with 2004 additions and 1515 deletions
+286
View File
@@ -0,0 +1,286 @@
/**
* @file allocators.h
* @brief This header contains the definition of the memory functions and basic
* allocator object.
* @author John Christman (sorakatadzuma@gmail.com)
* @copyright Malunal Studios, LLC.
*/
#include "malunal/config.h"
#include "malunal/types/error.h"
#include "malunal/types/uuid.h"
#ifndef MALUNAL_ALLOCATOR_HEADER
#define MALUNAL_ALLOCATOR_HEADER
/**
* @brief Imports an external constant for the UUID of @c allocator_t.
* @details This is needed to properly be able to cast @c allocator_t compliant
* types to an @c allocator_t.
*/
extern
const uuid_t
UUID_ALLOCATOR_T;
/**
* @brief Imports an external constant for the error domain of @c allocator_t.
* @details This is needed to detect from what domain an error has come from,
* so that error codes can mean different things based on its domain.
*/
extern
const error_domain_t
ERROR_DOMAIN_ALLOCATOR_T;
/**
* @brief A pointer to a function which will attempt to acquire some memory
* address from the allocator instance, with the size of the buffer
* equal to @c size and the address placed into the location pointed
* to by @c out.
* @param allocator A pointer to the allocator to acquire memory from.
* @param size The size of the buffer to acquire from the allocator.
* @param out A pointer to the location where the buffer pointer
* should be stored if the buffer could be acquired.
* @returns An error code if the allocator could not acquire the memory.
*/
typedef error_t
(*allocator_acquire_pfn_t)(
malunal_mptr_t allocator,
malunal_size_t size,
malunal_mptr_t* out
);
/**
* @brief A pointer to a function which will attempt to dispose some memory
* address back to the allocator instance, given the @c size of the
* memory buffer at @c address.
* @param allocator A pointer to the allocator to dispose memory to.
* @param address A pointer to the memory to dispose to the allocator.
* @param size The size of the memory buffer to dispose.
* @returns An error code if the allocator could not dispose the memory.
*/
typedef error_t
(*allocator_dispose_pfn_t)(
malunal_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
);
/**
* @brief Defines the allocator virtual function table.
* @details Implementing classes of the allocator interface are expected to
* provide valid pointers to these functions that are implementation
* specific.
*/
typedef struct {
/**
* @brief A pointer to a function which will attempt to acquire some memory
* address from the allocator instance, with the size of the buffer
* equal to @c size and the address placed into the location pointed
* to by @c out.
*/
allocator_acquire_pfn_t const acquire;
/**
* @brief A pointer to a function which will attempt to dispose some memory
* address back to the allocator instance, given the @c size of the
* memory buffer at @c address.
*/
allocator_dispose_pfn_t const dispose;
} allocator_vtable_t;
/**
* @brief Defines the abstract allocator object.
* @details Since this is only an interface, it only contains a pointer to the
* virtual table for itself. Other objects which implement this, may
* come with member variables.
*/
typedef struct {
/**
* @brief A pointer to an immutable allocator virtual function table.
* @details Contains the function pointers to the allocator specific functions
* that make this allocator function as one.
*/
const allocator_vtable_t* vtable;
} allocator_t;
/**
* @brief A pointer to a mutable allocator.
* @details This is provided to simplify type declarations for functions
* requiring allocators that are meant to be mutable.
*/
typedef allocator_t* allocator_mptr_t;
/**
* @brief A pointer to a immutable allocator view.
* @details This is provided to simplify type declarations for functions
* requiring allocators that are meant to be immutable.
*/
typedef const allocator_t* allocator_iptr_t;
/**
* @brief Defines a set of errors that an allocator may throw.
* @details These are extremely useful for debugging an allocator or catching
* runtime issues that can be fixed.
*/
typedef enum {
/**
* @brief The generic allocator error.
* @details If the allocator does not know what to throw, or in test cases,
* this may be thrown.
*/
ALLOCATOR_ERROR_FAILURE,
/**
* @brief The allocator instance was null.
* @details Allocator functions take it by reference because it is a rather
* large object. So, when the reference is expected to be non-null,
* which is always, this will be thrown.
*/
ALLOCATOR_ERROR_NULL_ALLOCATOR,
/**
* @brief The allocator ran out of memory.
* @details By nature of memory being a limited resource, if the system or the
* allocator run out of memory this will be thrown.
*/
ALLOCATOR_ERROR_OUT_OF_MEMORY,
/**
* @brief The address being released does not belong to the allocator.
* @details Allocators expect any given address to have been allocated by
* itself. Most allocators can check if it was allocated from its
* memory pool, and if it wasn't this will be thrown.
*/
ALLOCATOR_ERROR_NOT_MY_ADDRESS,
/**
* @brief The allocator has determined that memory is being leaked.
* @details Given the diagnostics within an allocator, the allocator may find
* some memory is still in use upon release or finalization. If this
* happens, the allocator may throw this error.
*/
ALLOCATOR_ERROR_LEAKY_MEMORY,
} allocator_error_t;
/**
* @brief Attempts to acquire some memory buffer from the provided allocator.
* @details This will perform some checks before trying to call the virtual
* function table implementation provided by the allocator. If all
* pre-conditions pass, then the virtual function will be called and
* its result passed back to the caller.
* @param allocator A pointer to the allocator to call @c acquire on.
* @param size The size of the memory buffer to acquire.
* @param out The location where to store the acquired buffer.
* @returns An error if the allocator could not acquire a memory buffer for the
* function caller.
*/
error_t
allocator_acquire(
allocator_mptr_t allocator,
malunal_size_t size,
malunal_mptr_t* out
);
/**
* @brief Attempts to dispose some memory buffer into the provided allocator.
* @details This will perform some checks before trying to call the virtual
* function table implementation provided by the allocator. If all
* pre-conditions pass, then the virtual function will be called and
* its result passed back to the caller.
* @param allocator A pointer to the allocator to call @c dispose on.
* @param address A poitner to the memory buffer to dispose.
* @param size The size of the memory buffer to dispose.
* @returns An error if the allocator could not dispose a memory buffer for the
* function caller.
*/
error_t
allocator_dispose(
allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
);
/**
* @brief Rounds the given size up to the nearest multiple of @c alignment.
* @param size The current size to round up to the nearest multiple of
* the provided @c alignment.
* @param alignment The alignment to round the size to.
* @returns The rounded size.
*/
malunal_size_t
align_to(
malunal_size_t size,
malunal_size_t alignment
);
/**
* @brief Rounds the given size up to the nearest page.
* @details This will give you a size that is rounded up to the next multiple of
* the platform page size. For example, if you provide 4000 and the
* platform page size is 4096, then you will get 4096. Likewise, if you
* provided 5000 and the platform page size is 4096, then you will get
* 8192.
* @param size The current size that will be rounded up to the nearest page
* multiple for the platform.
* @returns The rounded page size.
*/
malunal_size_t
align_to_page(malunal_size_t size);
/**
* @brief Provides the platform page size.
* @details This can be really useful when you plan to allocate a lot of memory.
* Platforms typically round memory requests up to the nearest page and
* give the base address of the virtually allocated block back, but
* placing a guard on the portion of the memory that will not be used.
* By querying the page size for the platform and rounding a memory
* acquisition request up to the nearest page, you can get the full
* block the platform intends to hand back which, significantly helps
* in proper memory tracking.
* @returns The memory page size for the platform.
*/
malunal_size_t
platform_page_size();
/**
* @brief Provides the general libc allocator.
* @details This will provide a general purpose allocator backed by the standard
* library. The functions provided are equivalent to @c malloc,
* @c realloc, and @c free.
* @returns An allocator that interfaces with libc.
*/
allocator_mptr_t
libc_allocator();
/**
* @brief Provides the platform allocator.
* @details This will provide the appropriate platform allocator depending on
* what operating system this library was compiled for.
* @returns One of: @c linux_allocator or @c win32_allocator.
*/
allocator_mptr_t
platform_allocator();
#if MALUNAL_PLATFORM_LINUX
/**
* @brief Provides a linux specific allocator.
* @details This is the linux specific allocator. It uses @c mmap and @c munmap
* to acquire, reacquire, and release memory.
* @returns An allocator that works specifically with linux.
*/
allocator_mptr_t
linux_allocator();
#elif MALUNAL_PLATFORM_WIN32
/**
* @brief Provides a windows specific allocator.
* @details This is the windows specific allocator. It uses @c VirtualAlloc and
* @c VirtualFree to acquire, reacquire, and release memory.
* @returns An allocator that works specifically with windows.
*/
allocator_mptr_t
win32_allocator();
#endif /* Platform specific allocators */
#endif /* MALUNAL_ALLOCATOR_HEADER */
-396
View File
@@ -1,396 +0,0 @@
/**
* @file allocators.h
* @brief This header contains the definition of the memory functions and basic
* allocator object.
* @author John Christman (sorakatadzuma@gmail.com)
* @copyright Malunal Studios, LLC.
*/
#include "malunal/config.h"
#include "malunal/types.h"
#ifndef MALUNAL_ALLOCATORS_HEADER
#define MALUNAL_ALLOCATORS_HEADER
// Assuring that NULL_ADDRESS is correct.
#ifdef NULL_ADDRESS
#undef NULL_ADDRESS
#endif /* NULL_ADDRESS */
/**
* @def NULL_ADDRESS
* @brief Defines a null address.
* @details This is needed for functions to indicate that an address could not
* acquired.
*/
#define NULL_ADDRESS ((void*)0)
// Assuring that ALLOCATOR_SIZE is correct.
#ifdef ALLOCATOR_SIZE
#undef ALLOCATOR_SIZE
#endif /* ALLOCATOR_SIZE */
/**
* @def ALLOCATOR_SIZE
* @brief Defines the size of allocators such that it can exist on the stack
* without the need to heap allocate it.
* @details This is defined based on the number of bytes of a pointer for the
* system and the number of members within the internal allocator
* implementation. It is statically asserted during compile to assure
* it is not wrong.
*/
#define ALLOCATOR_SIZE sizeof(malunal_size_t) * 7
/**
* @brief Defines the basis of all allocators.
* @details This is a stack allocated opaque pointer to the internals. It is not
* meant to be extensible outside of this library, as this library
* should be capable of providing any and all possibly needed allocator
* and allocation strategies required.
*/
typedef struct {
malunal_uint8_t __opaque[ALLOCATOR_SIZE];
} allocator_t;
/**
* @brief A pointer to a mutable allocator.
* @details This is provided to simplify type declarations for functions
* requiring allocators that are meant to be mutable.
*/
typedef allocator_t* allocator_mptr_t;
/**
* @brief A pointer to a immutable allocator view.
* @details This is provided to simplify type declarations for functions
* requiring allocators that are meant to be immutable.
*/
typedef const allocator_t* allocator_iptr_t;
/**
* @brief Defines a set of exception that an allocator may throw.
* @details These are extremely useful for debugging an allocator or catching
* runtime issues that can be fixed.
*/
typedef enum {
/**
* @brief No exception was thrown.
* @details Some allocator functions only return an exception. This indicates
* for those functions that the operation was successful.
*/
ALLOCATION_ERROR_SUCCESS,
/**
* @brief The generic allocator exception.
* @details If the allocator does not know what to throw, or in test cases,
* this may be thrown.
*/
ALLOCATION_ERROR_FAILURE,
/**
* @brief The allocator instance was null.
* @details Allocator functions take it by reference because it is a rather
* large object. So, when the reference is expected to be non-null,
* which is always, this will be thrown.
*/
ALLOCATION_ERROR_NULL_ALLOCATOR,
/**
* @brief The allocator upstream was null.
* @details Allocator implementations may expect for the upstream parameter
* to pointer to a valid allocator. For those allocators that expect
* an upstream, and do not receive it, this will be thrown.
*/
ALLOCATION_ERROR_NULL_UPSTREAM,
/**
* @brief The allocator context was null.
* @details Allocator implementations often expect for the context parameter
* to point to a valid address. For those allocators that expect a
* context, and do not receive it, this will be thrown.
*/
ALLOCATION_ERROR_NULL_CONTEXT,
/**
* @brief The allocator ran out of memory.
* @details By nature of memory being a limited resource, if the system or the
* allocator run out of memory this will be thrown.
*/
ALLOCATION_ERROR_OUT_OF_MEMORY,
/**
* @brief The address being released does not belong to the allocator.
* @details Allocators expect any given address to have been allocated by
* itself. Most allocators can check if it was allocated from its
* memory pool, and if it wasn't this will be thrown.
*/
ALLOCATION_ERROR_NOT_MY_ADDRESS,
/**
* @brief The allocator has determined that memory is being leaked.
* @details Given the diagnostics within an allocator, the allocator may find
* some memory is still in use upon release or finalization. If this
* happens, the allocator may throw this exception.
*/
ALLOCATION_ERROR_LEAKY_MEMORY,
} allocation_error_t;
/**
* @brief Represents the result of an acquisition request.
* @details This has two parts to it: a fulfilled acquisition and an possible
* exception. Which part is put into the result is indicated by the
* @c threw tag.
*/
typedef struct {
/**
* @brief Indicates whether the result contains an allocation or an error.
* @details Allocators will set this to true when an error has occurred, such
* as when there is no more memory to allocate from.
*/
malunal_bool_t threw;
union {
/**
* @brief The success of fulfilling the acquisition request.
* @details If the allocator sucessfully fulfilled the request, this will
* contain the pointer of the allocation.
*/
malunal_mptr_t address;
/**
* @brief The failure of fulfilling the acquisition request.
* @details If the allocator unsuccessfully fulfilled the request, this will
* contain the reason for failure.
*/
allocation_error_t error;
};
} allocation_result_t;
/**
* @brief Gets the upstream allocator of the provided allocator.
* @param allocator A pointer to the allocator.
* @param out A pointer to where to store the upstream allocator.
* @returns An error if the allocator could not provide the upstream allocator;
* otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_upstream(
allocator_mptr_t allocator,
allocator_mptr_t* out
);
/**
* @brief Gets the number of bytes allocated by the allocator.
* @param allocator A pointer to the allocator.
* @param out A pointer to where to store the allocated count.
* @returns An error if the allocator could not provide the number of allocated
* bytes; otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_allocated_bytes(
allocator_mptr_t allocator,
malunal_size_t* out
);
/**
* @brief Gets the number of bytes reserved by the allocator.
* @param allocator A pointer to the allocator.
* @param out A pointer to where to store the reserved count.
* @returns An error if the allocator could not provide the number of reserved
* bytes; otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_reserved_bytes(
allocator_mptr_t allocator,
malunal_size_t* out
);
/**
* @brief Gets the number of times @c acquires was called on the allocator.
* @param allocator A pointer to the allocator.
* @param out A pointer to where to store the count.
* @returns An error if the allocator could not provide the number of times that
* @c acquire was called; otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_acquires_count(
allocator_mptr_t allocator,
malunal_size_t* out
);
/**
* @brief Gets the number of times @c release was called on the allocator.
* @param allocator A pointer to the allocator.
* @param out A pointer to where to store the count.
* @returns An error if the allocator could not provide the number of times that
* @c release was called; otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_releases_count(
allocator_mptr_t allocator,
malunal_size_t* out
);
/**
* @brief Executes an initialize request on an allocator.
* @param allocator A pointer to the allocator to initialize.
* @param upstream A pointer to the upstream for the allocator.
* @param capacity The amount of memory to initially reserve.
* @returns An error if the allocator could not be initialized properly;
* otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_initialize(
allocator_mptr_t allocator,
allocator_mptr_t upstream,
malunal_size_t capacity
);
/**
* @brief Executes a finalize request on an allocator.
* @param allocator A pointer to the allocator to finalize.
* @returns An error if the allocator could not be finalized properly;
* otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_finalize(
allocator_mptr_t allocator
);
/**
* @brief Executes an acquisition request on an allocator.
* @param allocator A pointer to the allocator to use for acquisition.
* @param size The size of the allocation to acquire from the allocator.
* @returns The result of acquisition which indicates if the function threw or
* if the request was fulfilled along with the error or allocation.
*/
allocation_result_t
allocator_acquire(
allocator_mptr_t allocator,
malunal_size_t size
);
/**
* @brief Executes a reacquisition request on an allocator.
* @param allocator A pointer to the allocator to use for reacquisition.
* @param address A pointer to the memory to reacquire from the allocator.
* @param oldsize The size of the old allocation to release.
* @param newsize The size of the new allocation to acquire.
* @returns The result of reacquisition which indicates if the function threw or
* if the request was fulfilled along with the error or allocation.
*/
allocation_result_t
allocator_reacquire(
allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t oldsize,
malunal_size_t newsize
);
/**
* @brief Executes a release request on an allocator.
* @param allocator A pointer to the allocator to use to release the memory.
* @param address A pointer to the memory to release from the allocator.
* @param size The size of the allocation to release.
* @returns An error if the allocator could not release the memory back to the
* allocator; otherwise, @c ALLOCATION_ERROR_SUCCESS.
*/
allocation_error_t
allocator_release(
allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
);
/**
* @brief Rounds the given size up to the nearest multiple of @c alignment.
* @param size The current size to round up to the nearest multiple of
* the provided @c alignment.
* @param alignment The alignment to round the size to.
* @returns The rounded size.
*/
malunal_size_t
align_to(
malunal_size_t size,
malunal_size_t alignment
);
/**
* @brief Rounds the given size up to the nearest page.
* @details This will give you a size that is rounded up to the next multiple of
* the platform page size. For example, if you provide 4000 and the
* platform page size is 4096, then you will get 4096. Likewise, if you
* provided 5000 and the platform page size is 4096, then you will get
* 8192.
* @param size The current size that will be rounded up to the nearest page
* multiple for the platform.
* @returns The rounded page size.
*/
malunal_size_t
align_to_page(malunal_size_t size);
/**
* @brief Provides the platform page size.
* @details This can be really useful when you plan to allocate a lot of memory.
* Platforms typically round memory requests up to the nearest page and
* give the base address of the virtually allocated block back, but
* placing a guard on the portion of the memory that will not be used.
* By querying the page size for the platform and rounding a memory
* acquisition request up to the nearest page, you can get the full
* block the platform intends to hand back which, significantly helps
* in proper memory tracking.
* @returns The memory page size for the platform.
*/
malunal_size_t
platform_page_size();
/**
* @brief Provides the general libc allocator.
* @details This will provide a general purpose allocator backed by the standard
* library. The functions provided are equivalent to @c malloc,
* @c realloc, and @c free.
* @returns An allocator that interfaces with libc.
*/
allocator_t
libc_allocator();
/**
* @brief Provides the platform allocator.
* @details This will provide the appropriate platform allocator depending on
* what operating system this library was compiled for.
* @returns One of: @c linux_allocator or @c win32_allocator.
*/
allocator_t
platform_allocator();
#if MALUNAL_PLATFORM_LINUX
/**
* @brief Provides a linux specific allocator.
* @details This is the linux specific allocator. It uses @c mmap and @c munmap
* to acquire, reacquire, and release memory.
* @returns An allocator that works specifically with linux.
*/
allocator_t
linux_allocator();
#elif MALUNAL_PLATFORM_WIN32
/**
* @brief Provides a windows specific allocator.
* @details This is the windows specific allocator. It uses @c VirtualAlloc and
* @c VirtualFree to acquire, reacquire, and release memory.
* @returns An allocator that works specifically with windows.
*/
allocator_t
win32_allocator();
#endif /* Platform specific allocators */
/**
* @brief Provides an arena allocator.
* @details This will provide appropriate arena allocator functions when the
* instance is created.
* @returns An allocator instance that is populated to be an arena allocator.
*/
allocator_t
arena_allocator();
#endif /* MALUNAL_ALLOCATORS_HEADER */
+122 -5
View File
@@ -5,20 +5,137 @@
* @author John Christman (sorakatadzuma@gmail.com)
* @copyright Malunal Studios, LLC.
*/
#include "../allocators.h"
#include "../allocator.h"
#ifndef MALUNAL_ALLOCATORS_ARENA_HEADER
#define MALUNAL_ALLOCATORS_ARENA_HEADER
/**
* @def MALUNAL_ARENA_REGION_SIZE
* @brief The size of the arena regions.
* @details This is to support variable size regions, allowing larger or smaller
* objects to be allocated into the regions. Under the hood these will
* be virtually mapped by the operating system, so a very large set of
* data will be split across physical pages.
*/
#ifndef MALUNAL_ARENA_REGION_SIZE
#define MALUNAL_ARENA_REGION_SIZE 4096
#endif /* MALUNAL_ARENA_REGION_SIZE */
/**
* @brief Defines a concrete arena allocator.
* @details An arena allocator is a type of large push allocator. It simply
* obtains large regions of memory, rounded up to the next operating
* system page size, and pushes data onto those pages without any
* understanding of how to dispose that data later.
*/
typedef struct {
malunal_size_t __opaque[2];
} arena_allocator_t;
/**
* @brief A pointer to a mutable arena allocator.
* @details This is provided to simplify type declarations for functions
* requiring arena allocators that are meant to be mutable.
*/
typedef arena_allocator_t* arena_allocator_mptr_t;
/**
* @brief A pointer to an immutable arena allocator.
* @details This is provided to simplify type declarations for functions
* requiring arena allocators that are meant to be immutable.
*/
typedef const arena_allocator_t* arena_allocator_iptr_t;
/**
* @brief Obtains the size of the arena allocator.
* @param allocator The arena allocator to obtain the size from.
* @param out A pointer to where the obtained size will be stored.
* @returns An error if the arena allocator could not obtain the size.
*/
error_t
arena_allocator_size(
arena_allocator_iptr_t allocator,
malunal_uint32_t* out
);
/**
* @brief Obtains the used count of the arena allocator.
* @param allocator The arena allocator to obtain the used count from.
* @param out A pointer to where the obtained used count will be stored.
* @returns An error if the arena allocator could not obtain the used.
*/
error_t
arena_allocator_used(
arena_allocator_iptr_t allocator,
malunal_uint32_t* out
);
/**
* @brief Creates an arena instance with the given starting @c capacity,
* placing the result into the @c out variable.
* @param capacity The size of the allocator to start with.
* @param allocator The arena allocator to initialize.
* @returns An error if the arena allocator could not be initialized.
*/
error_t
arena_allocator_init(
malunal_size_t capacity,
arena_allocator_mptr_t allocator
);
/**
* @brief Acquires some buffer from the arena allocator provided, given the
* @c size and the @c out location to place the buffer.
* @param allocator The arena allocator to acquire from.
* @param size The size of the bufffer to acquire.
* @param out Where to store the buffer.
* @returns An error if the arena allocator could not acquire the buffer.
*/
error_t
arena_allocator_acquire(
arena_allocator_mptr_t allocator,
malunal_size_t size,
malunal_mptr_t* out
);
/**
* @brief Disposes some buffer into the arena allocator provided, given the
* @c address to dispose and the @c size of the buffer.
* @param allocator The arena allocator to dispose into.
* @param address The address of the buffer to dispose.
* @param size The size of the buffer to dispose.
* @returns An error if the arena allocator could not dispose the buffer.
*/
error_t
arena_allocator_dispose(
arena_allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
);
/**
* @brief Resets the entire arena without freeing its pages.
* @details This will iterate through each of the pages acquired for the arena
* setting the usage of each page to zero. This will allow the arena to
* overwrite the data within the pages.
* @param arena The arena to reset.
* @returns An exception if the arena could not be reset.
* @param allocator A pointer to the arena allocator to reset.
* @returns An error if the arena allocator could not be reset.
*/
allocation_error_t
arena_reset(allocator_mptr_t arena);
error_t
arena_allocator_reset(
arena_allocator_mptr_t allocator
);
/**
* @brief Frees the memory held by the arena allocator.
* @param allocator A pointer to the arena allocator to free.
* @returns An error if the arena allocator could not be freed.
*/
error_t
arena_allocator_free(
arena_allocator_mptr_t allocator
);
#endif /* MALUNAL_ALLOCATORS_ARENA_HEADER */
+118
View File
@@ -0,0 +1,118 @@
/**
* @file linear.h
* @brief Contains the definition of a linear allocator and all the functions
* needed to utilize it.
* @author John Christman (sorakatadzuma@gmail.com)
* @copyright Malunal Studios, LLC.
*/
#include "../allocator.h"
#ifndef MALUNAL_ALLOCATORS_LINEAR_HEADER
#define MALUNAL_ALLOCATORS_LINEAR_HEADER
/**
* @brief Defines a concrete linear allocator.
* @details Linear allocators, also called bump allocators, simply track small
* acquisitions into the buffer that is provided to it. It does this
* by knowing how much memory
*/
typedef struct {
malunal_size_t __opaque[4];
} linear_allocator_t;
/**
* @brief A pointer to a mutable linear allocator.
* @details This is provided to simplify type declarations for functions
* requiring linear allocators that are meant to be mutable.
*/
typedef linear_allocator_t* linear_allocator_mptr_t;
/**
* @brief A pointer to an immutable linear allocator.
* @details This is provided to simplify type declarations for functions
* requiring linear allocators that are meant to be immutable.
*/
typedef const linear_allocator_t* linear_allocator_iptr_t;
/**
* @brief Obtains the size of the linear allocator.
* @param allocator The linear allocator to obtain the size from.
* @param out A pointer to where the obtained size will be stored.
* @returns An error if the linear allocator could not obtain the size.
*/
error_t
linear_allocator_size(
linear_allocator_iptr_t allocator,
malunal_uint32_t* out
);
/**
* @brief Obtains the used count of the linear allocator.
* @param allocator The linear allocator to obtain the used count from.
* @param out A pointer to where the obtained used count will be stored.
* @returns An error if the linear allocator could not obtain the used.
*/
error_t
linear_allocator_used(
linear_allocator_iptr_t allocator,
malunal_uint32_t* out
);
/**
* @brief Initializes a linear allocator.
* @param size The size of the buffer of the linear allocator.
* @param buffer The pointer to the buffer of the linear allocator.
* @param allocator The linear allocator to initialize.
* @returns An error if the linear allocator could not be initialized.
*/
error_t
linear_allocator_init(
malunal_size_t size,
malunal_mptr_t buffer,
linear_allocator_mptr_t allocator
);
/**
* @brief Acquires some buffer from the linear allocator provided, given the
* @c size and the @c out location to place the buffer.
* @param allocator The linear allocator to acquire from.
* @param size The size of the buffer to acquire.
* @param out Where to store the buffer.
* @returns An error if the linear allocator could not acquire the buffer.
*/
error_t
linear_allocator_acquire(
linear_allocator_mptr_t allocator,
malunal_size_t size,
malunal_mptr_t* out
);
/**
* @brief Disposes some buffer into the linear allocator provided, given the
* @c address to dispose and the @c size of the buffer.
* @param allocator The linear allocator to dispose into.
* @param address The address of the buffer to dispose.
* @param size The size of the buffer to dispose.
* @returns An error if the linear allocator could not dispose the buffer.
* @remarks Technically, linear allocators cannot dispose buffers that came from
* it because it cannot track free space. However, it will assert that
* the address at least came from itself to assure correctness in usage.
*/
error_t
linear_allocator_dispose(
linear_allocator_mptr_t allocator,
malunal_mptr_t address,
malunal_size_t size
);
/**
* @brief Resets the linear allocator.
* @param allocator The linear allocator reset.
* @returns An error if the linear allocator could not be reset.
*/
error_t
linear_allocator_reset(
linear_allocator_mptr_t allocator
);
#endif /* MALUNAL_ALLOCATORS_LINEAR_HEADER */
+129
View File
@@ -0,0 +1,129 @@
/**
* @file pool.h
* @brief Contains the definition of a pool allocator and all the functions
* needed to utilize it.
* @author John Christman
* @copyright Malunal Studios, LLC.
*/
#include "../allocator.h"
#ifndef MALUNAL_ALLOCATORS_POOL_HEADER
#define MALUNAL_ALLOCATORS_POOL_HEADER
/**
* @brief Defines a concrete pool allocator.
* @details A pool allocator is a type of free listing allocator. It obtains
* some underlying memory region and divides it up into chunks that
* can individually be acquired and disposed. These chunks must be
* at least the size of the platform pointer.
* @remarks The chunk size, also known as stride, must be a multiple of two.
* The pool allocator will automatically upsize the stride to a multiple
* of two when initializing the pool.
*/
typedef struct {
malunal_size_t __opaque[7];
} pool_allocator_t;
/**
* @brief A pointer to a mutable pool allocator.
* @details This is provided to simplify type declarations for functions
* requiring pool allocators that are meant to be mutable.
*/
typedef pool_allocator_t* pool_allocator_mptr_t;
/**
* @brief A pointer to an immutable pool allocator.
* @details This is provided to simplify type declarations for functions
* requiring pool allocators that are meant to be immutable.
*/
typedef const pool_allocator_t* pool_allocator_iptr_t;
/**
* @brief Obtains the stride of the pool allocator.
* @param allocator The pool allocator to obtain the stride from.
* @param out A pointer to where the obtained stride will be stored.
* @returns An error if the pool allocator could not provide the element stride.
*/
error_t
pool_allocator_stride(
pool_allocator_iptr_t allocator,
malunal_size_t* out
);
/**
* @brief Obtains the count of the pool allocator.
* @param allocator The pool allocator to obtain the count from.
* @param out A pointer to where the obtained count will be stored.
* @returns An error if the pool allocator could not provide the element count.
*/
error_t
pool_allocator_count(
pool_allocator_iptr_t allocator,
malunal_size_t* out
);
/**
* @brief Obtains the capacity of the pool allocator.
* @param allocator The pool allocator to obtain the capacity of.
* @param out A pointer to where the obtained capacity will be stored.
* @returns An error if the pool allocator could not provide the capacity.
*/
error_t
pool_allocator_capacity(
pool_allocator_iptr_t allocator,
malunal_size_t* out
);
/**
* @brief Initializes a pool allocator instance with the given @c stride,
* @c capacity, and @c upstream allocator.
* @param stride The size of the chunks of the pool allocator.
* @param capacity The size of the buffer of the pool allocator.
* @param upstream The allocator to obtain the memory buffers from.
* @param allocator The pool allocator to initialize.
* @returns An error if the pool allocator could not be initialized.
*/
error_t
pool_allocator_init(
malunal_size_t stride,
malunal_size_t capacity,
allocator_mptr_t upstream,
pool_allocator_mptr_t allocator
);
/**
* @brief Acquires a chunk from the pool allocator provided, given the @c out
* location to place the chunk.
* @param out Where to store the acquired chunk.
* @returns An error if the pool allocator could not acquire a chunk.
*/
error_t
pool_allocator_acquire(
pool_allocator_mptr_t allocator,
malunal_mptr_t* out
);
/**
* @brief Disposes a chunk into the pool allocator provided, given the
* @c address to dispose.
* @param address The address of the buffer to dispose.
* @returns An error if the pool allocator could not dispose a chunk.
*/
error_t
pool_allocator_dispose(
pool_allocator_mptr_t allocator,
malunal_mptr_t address
);
/**
* @brief Frees the memory held by the pool allocator.
* @param allocator A pointer to the pool allocator to free.
* @returns An error if the pool allocator could not be freed.
*/
error_t
pool_allocator_free(
pool_allocator_mptr_t allocator
);
#endif /* MALUNAL_ALLOCATORS_POOL_HEADER */
+128
View File
@@ -0,0 +1,128 @@
/**
* @file stack.h
* @brief Contains the definition of a stack allocator and all the functions
* needed to utilize it.
* @author John Christman (sorakatadzuma@gmail.com)
* @copyright Malunal Studios, LLC.
*/
#include "../allocator.h"
#ifndef MALUNAL_ALLOCATORS_STACK_HEADER
#define MALUNAL_ALLOCATORS_STACK_HEADER
/**
* @brief Defines a concrete stack allocator.
* @details A stack allocator is one that acquires and disposes in a first in
* last out manner. This is useful for temporary stacks and some
* container like objects.
* @remarks When disposing into a stack allocator, the address that is being
* disposed must be the last acquired address or the allocator will
* fail to dispose it.
*/
typedef struct {
malunal_size_t __opaque[6];
} stack_allocator_t;
/**
* @brief A pointer to a mutable stack allocator.
* @details This is provided to simplify type declarations for functions
* requiring stack allocators that are meant to be mutable.
*/
typedef stack_allocator_t* stack_allocator_mptr_t;
/**
* @brief A pointer to an immutable stack allocator.
* @details This is provided to simplify type declarations for functions
* requiring stack allocators that are meant to be immutable.
*/
typedef const stack_allocator_t* stack_allocator_iptr_t;
/**
* @brief Obtains the stride of the stack allocator.
* @param allocator The stack allocator to obtain the stride from.
* @param out A pointer to where the obtained stride will be stored.
* @returns An error if the stack allocator could not provide the element stride.
*/
error_t
stack_allocator_stride(
stack_allocator_iptr_t allocator,
malunal_size_t* out
);
/**
* @brief Obtains the count of the stack allocator.
* @param allocator The stack allocator to obtain the count from.
* @param out A pointer to where the obtained count will be stored.
* @returns An error if the stack allocator could not provide the element count.
*/
error_t
stack_allocator_count(
stack_allocator_iptr_t allocator,
malunal_size_t* out
);
/**
* @brief Obtains the capacity of the stack allocator.
* @param allocator The stack allocator to obtain the capacity of.
* @param out A pointer to where the obtained capacity will be stored.
* @returns An error if the stack allocator could not provide the capacity.
*/
error_t
stack_allocator_capacity(
stack_allocator_iptr_t allocator,
malunal_size_t* out
);
/**
* @brief Initializes a stack allocator instance with the given @c stride,
* @c capacity, and @c upstream allocator.
* @param stride The size of the chunks of the stack allocator.
* @param capacity The size of the buffer of the stack allocator.
* @param upstream The allocator to obtain the memory buffers from.
* @param allocator The stack allocator to initialize.
* @returns An error if the stack allocator could not be initialized.
*/
error_t
stack_allocator_init(
malunal_size_t stride,
malunal_size_t capacity,
allocator_mptr_t upstream,
stack_allocator_mptr_t allocator
);
/**
* @brief Acquires a chunk from the stack allocator provided, given the @c out
* location to place the chunk.
* @param out Where to store the acquired chunk.
* @returns An error if the stack allocator could not acquire a chunk.
*/
error_t
stack_allocator_acquire(
stack_allocator_mptr_t allocator,
malunal_mptr_t* out
);
/**
* @brief Disposes a chunk into the stack allocator provided, given the
* @c address to dispose.
* @param address The address of the buffer to dispose.
* @returns An error if the stack allocator could not dispose a chunk.
*/
error_t
stack_allocator_dispose(
stack_allocator_mptr_t allocator,
malunal_mptr_t address
);
/**
* @brief Frees the memory held by the stack allocator.
* @param allocator A pointer to the stack allocator to free.
* @returns An error if the stack allocator could not be freed.
*/
error_t
stack_allocator_free(
stack_allocator_mptr_t allocator
);
#endif /* MALUNAL_ALLOCATORS_STACK_HEADER */