logoalt Hacker News

gebdevtoday at 5:35 PM1 replyview on HN

This is a great example of how ADTs can be implemented in C by emulating classes, despite the loss in brevity.

For the first item on reference counting, batched memory management is a possible alternative that still fits the C style. The use of something like an arena allocator approximates a memory lifetime, which can be a powerful safety tool. When you free the allocator, all pages are freed at once. Not only is this less error prone, but it can decrease performance. There’s no need to allocate and free each reference counted pointer, nor store reference counts, when one can free the entire allocator after argument parsing is done.

This also decreases fallible error handling: The callee doesn’t need to free anything because the allocator is owned by the caller.

Of course, the use of allocators does not make sense in every setting, but for common lifetimes such as: once per frame, the length of a specific algorithm, or even application scope, it’s an awesome tool!


Replies

lelanthrantoday at 7:20 PM

> This is a great example of how ADTs can be implemented in C by emulating classes, despite the loss in brevity.

I don't see it that way, mostly because ADTs don't require automatic destructors or GC, etc, but also because I never considered a unique/shared pointer type to be an abstract data type

> When you free the allocator, all pages are freed at once. Not only is this less error prone, but it can decrease performance.

How does it decrease performance? My experience with arenas is that they increase performance at the cost of a little extra memory usage.