A collection of C utilities: aligned memory allocation with multiple backends, error handling, dynamic arrays, clamp functions, OpenGL helpers and more.
Portable C library for aligned memory allocation.
Supports Windows, POSIX, C11 and fallback using standard malloc.
am_aligned_realloc(pointer, 0)is not equivalent toam_aligned_free(pointer)- it returnsNULLwithout releasing the memory block.
In C11 the size passed toam_aligned_mallocmust be a multiple of alignment. The library automatically rounds up the size when using the C11 backend.
In C23 this requirement is removed. To avoid unnecessary overhead, the library does not round the size when compiled for C23 support. If your code relies on rounding, ensure the size is a multiple of alignment explicitly.
Flexible assert macros:
assert_m(condition, message)- assertion with a string message.assert_mf(condition, format, ...)- assertion with format (requires C99 or GCC/clang extensions).static_assert_m(condition, message)- compile-time checks (requires C11 with a string literal or C89 but text will not be accurate).
assert_mandassert_mfare disabled whenNDEBUGis defined.static_assert_mis not affected byNDEBUG.
Inline clamping functions.
Supported types: int64_t, uint64_t, size_t, float, double, long double.
For narrower integers (for example int32_t) you can safely cast to the corresponding supported type; otherwise an implicit conversion will occur, which isn't recommended.
Register a cleanup function with optional argument to be called at program exit via atexit.
- Supports one registered function with a single void * argument. Subsequent calls with a new function pointer are ignored.
- The argument pointer can be updated at any time by calling function without a function pointer.
- At least one of the pointers must be non-
NULL(enforced in debug builds). - Requires C99.
Print an error message to stderr and exit with EXIT_FAILURE.
ep_exit_print(format_pointer, ...)- prints a formatted message and exits.ep_exit_print_free(free_flag, format_pointer, ...)- prints formatted message, freesformat_pointeriffree_flagis true andformat_pointeris notNULLand then exit.
format_pointermust be non-NULL(enforced in debug builds).
Dynamic array utilities with automatic resizing and memory management.
da_dynamic_array_ensure_capacity- ensures the array has at leastneededcapacity, growing by a factor of 1.5; if that allocation fails, it attempts to allocate exactlyneededelements and if both allocations fail, returnsfalse.da_dynamic_array_ensure_capacity_list- same asda_dynamic_array_ensure_capacitybut operates on multiple dynamic arrays simultaneously (all resized to the same capacity) to simplify consistent resizing of related arrays. Requires an array ofDA_Dynamic_Array_Liststructures, each specifying the data pointer and element size for the corresponding array.da_dynamic_array_shrink- shrinks capacity to fitamountelements (ifamountsmaller thanbase_amount, capacity is set tobase_amount). If bothamountandbase_amountare0, the array is freed and capacity is zeroed.da_dynamic_array_free- frees the array and nullifies the pointer, size, and capacity.
All functions perform bounds checking and handle allocation failures gracefully.
Requires C99.
Stack-like error message storage and retrieval.
woem_push- formats and stores an error message;woem_push_raw- stores a pre-allocated error message;woem_pop- retrieves the most recent error message and removes it from storage; returns NULL if none exists and also returns flag indicating memory ownership.woem_shrink- shrinks the dynamic storage to the exact number of messages (optimises memory usage).woem_clear- frees all stored messages and resets the storage.
Memory ownership
woem_pushallocate memory internally.woem_push_rawtakes ownership of the passed pointer.woem_popreturns a flag and a pointer, if the flag istruethe caller is responsible for callingfreeon returned pointer.woem_clearfrees all remaining messages.
OpenGL error checking.
gl_errors_clean- clear all pending OpenGL errors.gl_errors_check- check for errors, returnstrueif none, otherwise stores errors viawoem_push.gl_get_error_string- returns a string description for a given OpenGL error code.
Requires OpenGL and GLEW.
Safe wrappers for all OpenGL functions that may set an error state; zero overhead in release builds.
glCreateShader_wrappedglShaderSource_wrappedglCompileShader_wrappedglCreateProgram_wrappedglAttachShader_wrappedglLinkProgram_wrappedglDeleteShader_wrappedglDeleteProgram_wrapped
Functions return
bool. In release builds they alwaystrue; in debug they validate arguments and return result of OpenGL error checking.
Requires OpenGL and GLEW.
License: Apache 2.0