r/cprogramming • • 14d ago

A question about arenas.

I worked in C for years . i have used arenas a lot in the past. Today i have to modify an old c program , nothing difficult but i found something that made me think. Who developed the code ( very well written and documented ) used an arena for allocating variable.

What 's seems to me strange is that he did not used an heap memory area ( malloc ) but it used a char [] buffer and used it for its arena . At that point he could obviously allocate directly variable in stack instead he grabs all its variable from that buffer . Why a choice like that ? i thought for the CPU cache L1/L2 and so on ( all the "arena" will likely be in cache of the cpu ) but it is only an ipotesys and i was asking to you , in your opinion , the reason of a such choice .
Just curiosity .

Thanks

PP

6 Upvotes

20 comments sorted by

3

u/CS_70 14d ago

Hm, arenas are about allocating all in one go? Where it is really is a orthogonal concern, and he may have chosen the stack because of some other propery (like the compiler, the target architecture etc)

0

u/Ok_Outlandishness906 14d ago

i don't think, it is written for hpux, standard c code, moved on aix

1

u/CS_70 14d ago

Maybe the guy just liked the stack. :)

7

u/zhivago 14d ago

Not enough context to answer, I think.

3

u/markand67 14d ago

My initial guess is that it can still freely choose whatever to allocate by small parts which are foreigners to each others. The fact that its on the stack or the heap doesn't matter its usage. The idea beneath the arena is just to avoid fragmentation and have a large amount direct accessible.

Stack variables may have some benefits as you can place them where you want with the linker. Just make sure that alignment is correct

1

u/Key_River7180 14d ago

Maybe to have fine control? A char is 1 byte so it makes it easier to address stuff, but it requires casts here and there so idk...

1

u/Low_Lawyer_5684 14d ago

Arena is often used like that to keep all your allocations in one place to prevent leaks. I am not sure about your particular source code, but I've seen code which was executing a bytecode (scripting language) in multiple virtual machines. Once code is finished - whole VM's arena was freed, no matter if bytecode crashed or forgot to deallocate something.

In games they use arenas for cache locality

The third important reason is copy/paste programming.

1

u/Electronic-Yam-69 14d ago

Maybe they were self taught and never learned about heap allocations.

1

u/MagicWolfEye 14d ago

Parts of that char array can still be in cache. Memory on the stack is not faster than anywhere else.

1

u/GoblinsGym 14d ago

The stack is usually "hot", and likely to stay in L1 cache if you don't overload it with large data structures. Smallish data structures, e.g. context records are ok.

The arena is more likely to end up in L2 / L3 / DRAM.

1

u/GoblinsGym 14d ago

Not on C, but there can be advantages to preallocate the arena in BSS segment.

The OS will allocate virtual space, but not attach physical memory yet. If you are lucky, it will use huge pages, reducing TLB overhead.

In my case (Delphi), another advantage is that this sits in the low part of the address space, so I can use 32 bit pointers / indexes for space savings. Using 64 bit pointers for everything is a waste of space for my purposes (symbol table etc).

1

u/IDatedSuccubi 14d ago

I'd say we need to see the code, but malloc has overhead and just a function-local buffer there is cleaner than stack stuff, maybe?

1

u/Plastic_Fig9225 13d ago

Enabling flexibility to use heap, stack, or statically allocated memory.

1

u/W31337 13d ago

If arena is what I think it is.

You want to free everything at the same time. The whole thing of an arena is that you don't need to track allocation and it all gets freed at the end. Making it a stack item is smart because the arena needs to be relevant and thus in scope, and when the scope ends the memory gets freed automatically.

My 2cts (and I could be very wrong)

1

u/VadumSemantics 7d ago

Maybe reliability? Could be they wanted enough memory to operate without malloc() possibly running out of memory & returning null. If it is like char foo[4096] or whatever, that buffer would be always available or the program wouldn't be load.

0

u/braaaaaaainworms 14d ago

char[] buffer on stack or on heap?

1

u/Ok_Outlandishness906 14d ago

stack, otherwise it would have used malloc i believe

1

u/braaaaaaainworms 14d ago

he might not have malloc or the code was written for an embedded system without malloc

0

u/MikeSeth 14d ago

Arenas eliminate a lot of complexity and problems associated with malloc. Malloc is complex and optimized for a general case. There are scenarios where you want to allocate a large set of data, control it precisely and free it as a batch. Arenas will give you better performance and safety in this case. In some use cases like computer games and compilers they're pretty much the standard issue for bulk memory management.

0

u/Patchmaster42 14d ago

Depending on how old this code is, it used to be that heap management was not done well. If you did vast numbers of alloc() and free() calls, the heap could become fragmented into lots of small pieces. There were situations where the heap started doing garbage collection and never returned.

If you were doing a small number of allocations over a very long time period, you were better off rolling your own mini heap, giving you complete control.