🟩 Heap Allocation, Custom allocators, Virtual Memory, Buffer, Segment
SaneCppMemory.h is library tracking and limiting runtime / dynamic allocations through the use of custom allocators.
All libraries are designed to let you use your favorite externally provided string / vector classes BUT if you've been sold on using also the Containers library then here you will find scoped allocator / arena infrastructure used by them.
Tests/InteropSTL/*.cpp for an example of externally provided Container classes.The "owned" String class is here and not in Strings library because it needs allocator support.
| Class | Description |
|---|---|
| SC::Buffer | An heap allocated byte buffer that can optionally use an inline buffer. |
| SC::Memory | Centralized functions to allocate, reallocate and deallocate memory. |
| SC::VirtualMemory | Reserves a contiguous slice of virtual memory committing just a portion of it. |
| SC::Globals | Customizable thread-local and global variables for memory handling. |
| SC::String | A non-modifiable owning string with associated encoding. |
| SC::SmallString | String with compile time configurable inline storage (small string optimization) |
🟩 Usable
The library is solid. The Buffer implementation has been evolved and fine tuned to be minimal but effective.
Memory library helps tracking and limit runtime / dynamic allocations through the use of custom allocators. A classic dynamically expandable binary buffer SC::Buffer is provided and it's largely shared to form the more object model oriented SC::Vector class from Containers Library.
All allocations throughout all downstream dependant libraries are centrally tracked by the SC::Globals class, that also allows re-defining custom thread-local allocators.
Such allocators can be just fixed buffers, regular heap memory or reserved SC::VirtualMemory using only limited amounts of Physical memory.
An heap allocated byte buffer that can optionally use an inline buffer.
Example:
A non-modifiable owning string with associated encoding.
SC::String is (currently) implemented as a SC::Vector with the associated string encoding. A SC::StringSpan can be obtained from it calling SC::String::view method but it's up to the user making sure that the usage of such SC::StringSpan doesn't exceed lifetime of the SC::String it originated from (but thankfully Address Sanitizer will catch the issue if it goes un-noticed).
String with compile time configurable inline storage (small string optimization)
| N | number of chars to reserve in inline storage |
Centralized functions to allocate, reallocate and deallocate memory.
Reserves a contiguous slice of virtual memory committing just a portion of it.
This class is useful on 64-bit systems where the address space is so large that it's feasible reserving large chunks of memory to commit and de-commit (shrink) as needed.
Reservation ensures that the returned address will not change and will be sized in multiples of system page size.
Customizable thread-local and global variables for memory handling.
This class holds pointers to systems that must be globally available, like the memory allocator. It allows "stacking" different Globals through a push / pop mechanism, connecting them through a linked list. The Default allocator is automatically setup and uses standard malloc, realloc, free for allocations.
Example (Fixed Allocator):
Example (Virtual Allocator):
Example (Memory dump):
These blogs have been written before the split from Foundation into the Memory library:
These blog posts have been written after the split from foundation:
🟦 Complete Features:
💡 Unplanned Features:
| Type | Lines Of Code | Comments | Sum |
|---|---|---|---|
| Headers | 433 | 398 | 831 |
| Sources | 1037 | 219 | 1256 |
| Sum | 1470 | 617 | 2087 |