Discussion about this post

User's avatar
Trần Thành Long's avatar

A couple of questions:

1. People usually make a scratch arena locals to a thread using something like __declspec(thread). What if you're on a platform or project that can't use thread-local storage? I usually see people handle this by passing around a "thread context" Care to comment on that?

2. The push and pop operator on the current arena implementation aren't atomic, which mean they aren't thread-safe. How do you go about dealing with memory management for multithreading?

3. Do you think the idea of ArenaTemp can be made more abstract and applied to other kinds of allocators? For example, I may have a generic heap allocator that keeps a free list and allows for out-of-order alloc and free allocations. I can still create an ArenaTemp for this allocator by storing the free list in the ArenaTempBegin and restoring it in the ArenaTempEnd function. The same also goes for a pool allocator, for example. I can still store the current free list and reset it later.

Expand full comment
James F. Bellinger's avatar

For code you don't control or don't want to modify, if you're able to control malloc, you can go one further and use a "default arena" controlled by a thread-local stack structure as a "malloc override" -- have malloc forward to your arena allocator. This doesn't really work if the control does a lot of free'ing, but for "I'm calling it and am then done", great.

The idea of "many stacks" is chronically underused. Often I see a parameter being passed through many layers of call stack. I replace it with a thread-local stack variable. It's more maintainable and saves code space.

I'm doing embedded programming, and have been under 1KB free code space for a good six months, but hey, in that time I've probably extracted 10KB of code space to implement new features. Getting rid of needless call stack gets rid of PUSHes and frees up registers for intermediate functions.

Another place thinking about the stack could help is "asynchronous programming". The whole async transform is a comical, slow, and disastrous avoidance of the fact that they could just treat their call stack as data to yield back freely.

Expand full comment
45 more comments...

No posts