HeadlinesBriefing HeadlinesBriefing.com

Handle-based Memory Management In C And C

Hacker News •
×

Nov 28, 2018: The author added an update on preventing handle collisions using a per-slot generation counter. The original post discusses dynamic memory management in C and C++, replacing raw and smart pointers with index-handles. The author explains that in large codebases (0.5 to 1 million lines of code), heavy use of smart pointers for small objects leads to dog-slow performance due to cache misses from pointer indirection.

Other issues include memory fragmentation and fake memory leaks, where forgotten smart pointers prevent freeing underlying memory. The core philosophy involves centralizing memory management into systems like rendering or physics, grouping items of the same type into arrays, and returning index-handles instead of pointers to the outside world. Only a few bits are used for the array index, with remaining bits for safety checks.

Converting a handle to a pointer should only happen when absolutely necessary, and pointers should never be the owner of an item's memory. This approach works best with data-oriented architecture and reduces garbage collector pressure in higher-level languages. The author emphasizes that user-level code should avoid direct memory allocation functions and minimize pointer use, keeping direct memory manipulation inside centralized systems for easier debugging and optimization.

Source: Hacker News · Summarized by HeadlinesBriefing