John Burton
3 posts
None
Use of allocators
I am implementing allocators in my program. I use C++ (But not so much 'modern' c++).
I have implemented a number of memory allocators which return memory mostly based on the required lifetime of the data so I don't in general need to do anything to 'free' allocations, I instead discard the whole memory block at certain times in the program.

In general this works well. My program is quite a bit faster than just using new/delete or malloc/free.

However imagine I have a function "get_computer_name" which gets the name of the computer using win32 api and returns it as a string. (Pretend it's different every time so I can't just store it once). It needs to allocate some memory to store the name. A lot of the time the lifetime of this data is just the calling function (as it just logs the name, or appends it to a larger string) so I can use a StackAllocator but sometimes I want full heap allocation.

So I pass in an allocator telling it where to allocate the string. All is good, it works well.

However, this is leading to an explosion of allocator parameters. Pretty much every function in my code is ending up taking an allocator as a parameter because either _it_ does allocation, or it calls a function that does so it needs to forward the allocator.

I don't really want most of the functions in my program to take an allocator as a parameter, it seems like clutter in the code...

Has anyone got any good ideas how to avoid passing allocators _everywhere_ in my code in an elegant way?
Mikael Johansson
99 posts / 1 project
Use of allocators
So the name only has to live within the function, but the function is called often enough for alloc/free to be a bottleneck? Is that right?
In that case, and if I cant send a pointer to the memory to be used, I would make a const struct within the function, like this:

typedef struct
{
char* theData;
int numberOfChars;
int sizeOfTheData;
{
TheStruct;

Then make it static in the function:

void TheFunction
{
static TheStruct theStruct = {};
}

Then just refill that when needed, and realloc it if the char array I want to put in it is to big.
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
Use of allocators
Edited by Ginger Bill on
I have an allocator system which I use everywhere in my C and C++ code.
https://github.com/gingerBill/gb/blob/master/gb.h#L1051

I store the allocators in the corresponding types if they need them but otherwise, I pass them to any function that needs them. If you don't to have to keep passing them around, you could use a context based system you pass around (by value or pointer) which can also store extra data (this is what I do in my language Odin, and the compiler).

C++ does allow the user to have default arguments so you could have a default_allocator() function as the default.
John Burton
3 posts
None
Use of allocators
Thanks both :)

I think that the combination of a default parameter for when it doesn't matter, and not worrying too much about passing allocators around will be fine.