I am looking for a good memory pool implementation in C.
it should include the following:
- Anti fragmentation.
- Be super fast 🙂
- Ability to “bundle” several allocations from different sizes under some identifier and delete all the allocations with the given identifier.
- Thread safe
I think the excellent
talloc, developed as part of samba might be what you’re looking for. The part I find most interesting is that any pointer returned from talloc is a valid memory context. Their example is:In response to your particular points:
(1) Not sure what anti-fragmentation is in this case. In C you’re not going to get compacting garbage collection anyway, so I think your choices are somewhat limited.
(2) It advertises being only 4% slower than plain
malloc(3), which is quite fast.(3) See example above.
(4) It is thread safe as long as different threads use different contexts & the underlying malloc is thread safe.