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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

therealdreg opened this issue · comments

commented

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)

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:

  • pool_malloc()
  • pool_realloc()
  • pool_calloc()
  • pool_free()