stclib / STC

A modern, user friendly, generic, type-safe and fast C99 container library: String, Vector, Sorted and Unordered Map and Set, Deque, Forward List, Smart Pointers, Bitset and Random numbers.
MIT License
1.34k stars 73 forks source link

Support & Documentation for Critical Systems (determinism + avoid heap fragmentation) #66

Closed therealdreg closed 1 year ago

therealdreg commented 1 year ago

Hello @tylov! I find this library to be quite impressive. For individuals like myself who program critical embedded systems, we must utilize deterministic code (+avoid heap fragmentation), which means we cannot employ malloc/calloc.

Typically, we resort to using "pools" and similar constructs, or allocate everything at the program's main.

I believe it would be beneficial to incorporate support for such scenarios.

I've reviewed your code, and several approaches come to mind. It would be valuable to document the most optimal method for this scenario (README)

tylov commented 1 year ago

Thanks. I'll try to mention this in the docs.

All STC containers except for linked lists can be preallocated with reserve(), and that includes sorted maps, queues and deques! This is uncommon, as in c++ these containers normally allocates elements (as node or segments) incrementally, and does not provide reserve.

ADD: In case you have a pool of memory already allocated, you can #define i_allocator pool, before including and reserving space for each container. In this case you must have created the following function / macros: