s_alloc is a very simple memory allocator, for building systems with a limited memory budget. Allowing the developer to choose how much memory it will be able to allocate upfront.
#include "static_allocator.h"To link the library you will need to include a few flags:
# if the library folder is in the root of the project:
-I./Static-Allocator/include -L./Static-Allocator/lib -lstatic_allocator
#if the library folder is in some 'lib' folder, inside the project:
-I./bin/Static-Allocator/include -L./bin/Static-Allocator/lib -lstatic_allocatorInitializes avmem_global and avmem_rover. DOES NOT ALLOCATE SPACE FOR s_pool.
- Parameters:
mem_sizeThe size in bytes
Allocates the specified number of bytes, with a possible additional cost of 8 bytes for the header. Return null (0) if no big enough space is available.
- Parameters:
sizeThe size in bytes - Returns: A pointer to the allocated memory.
Liberates the specific pointer's memory, making it available for reallocation, but beware of fragmentation. This function will not automatically merge neighboring free blocks, unless if "STATIC_ALLOCATOR_MERGE_AFTER_FREEING" is defined. This function will not check if it is pointing at a valid header, so tread carefully.
- Parameters:
ptrPointer to be freed
Merge will scan and combine neighboring regions of memory, if they are both free. If it reaches the block neighboring the hover, it will effectively resize it to its combined size.
Attempts to merge a specific block to its right neighbor.
- Parameters:
ptrTo merge - Returns: 1 if it could merge, 0 if could not.
Verifies if the pointer is within the memory pool range.
- Parameters:
ptrPointer to be verified - Returns: 1 if it is, 0 if it is not.
Verifies if the pointer has a known header.
- Parameters:
ptrPointer to be verified - Returns: 1 if it is, 0 if it is not.
Gets the size of this allocated block declared in the header.
- Parameters:
ptrPointer to be verified - Returns: size declared in the header
Verifies the block's header, if it is declared as used or free.
- Parameters:
ptrPointer to be verified - Returns: 1 if it is free, 0 if it is being used
Counts how many blocks there are. (free or not)
- Returns: how many exist.
Returns the address of the first block. (returns 0 if there is no block)
- Returns: a pointer to the first block. (or 0 if there is no block)
Returns the address of the next block. (returns 0 if there is no block ahead)
- Parameters:
ptrCurrent pointer - Returns: a pointer to the next blocks. (or 0 if it reached the end)
Verifies if the specified block is valid before freeing, then free's it, if applicable.
- Parameters:
ptrPointer to be verified / freed - Returns: 1 if it was freed successifully. 0 if not.
Initializes important fields in a memory_unit. Usefull for when creating a memory_unit on the stack.
- Parameters:
rootPointer of the unit to be initalized
Allocates a new unit and attaches it to the specified root, following the specific path. Creates new directories if the specified path does not exist yet. Each segment must be separated by a forward slash: '/'
- Parameters:
rootPointer of the main unit;pathSequence of directory names separated by a forward slash '/';sizeSize of the allocated size of the final unit - Returns: a pointer to the allocated memory
Liberates the specific pointer's (and its children) memory, making it available for reallocation. (prone to fragmentation) If you free a directory, every subdirectory and 'file' will also be freed.
- Parameters:
unitPointer of the unit to be freed
Finds and returns a memory_unit on the specified path.
- Parameters:
rootPointer to the main unit;pathSequence of directory names separated by a forward slash '/' - Returns: a pointer to the unit found, or null (0) if none were found
Reads the header of the specified block. WARNING: if this block was not allocated by the directory system, it may cause segfault.
- Parameters:
ptrPointer to block of memory - Returns: a pointer to the header of the specified block.
Reads the block of the specified header.
- Parameters:
unitPointer to the header - Returns: a pointer to the block of the specified header
static_memory_size: The initial and total size of the memory pool.avmem_global: The general available memory size, including fragmented.avmem_rover: The remaining size of the rover, i.e. unfragmented memory size.s_pool: The main pool of memory.
Macros that once used during compilation, will produce specific effects on the code.
If defined, will print a message to the console, whenever a memory related function is called.
If defined, will attempt to merge ALL blocks of memory every time free(void* ptr) is called.
Defines the size of the memory pool in bytes, not allowing s_pool to be manually declared.
If defined, will print a message to the console, whenever a memory directory related function is called.
If defined, will force the compiler to use the system allocator (<stdlib.h> malloc, free).
Macros that can be used anytime, anywhere.
Initializes the memory system.
This library is licensed under the MIT License. You can read more about it at https://opensource.org/licenses/MIT.