I've been designing the memory management module for my game engine, including things like custom allocators and when it came to allocating memory for strings I kinda got stuck. What I mean by strings in this context is things like UI text, Items description, dialogues etc, so basically strings themselves are immutable, but they can stop being useful and just get freed.
With the string (and basically any object type with such allocation patterns) allocator I'd like to mostly get rid of the malloc/free invocations and so I'm wondering what kind of allocator is suitable for this jobs. Another thing that worries me is fragmentation, because string have variable length by nature
How do game engines handle strings like this in the first place?
EDIT
Firstly I'd like to address some of the questions that came up.
The strings themselves would be loaded from an asset file.
By 'stop being useful' I mean a situation where player interacts with some kind of item that has a description associated with it and we'd like to show the description on screen.
To do that, the text would get loaded, displayed and freed some time after the player closes the description window (the delay is here because there the player could reopen the window again in a brief second for whatever reason).
Such allocations would occur very often, because we don't need all the resources at all times, just a specific subset of them and my idea was to create an allocator for that purpose. It would malloc
a large enough block of memory and store the strings (or any resource of such allocation pattern) in it, marking the free spots as the string get freed.
That's when 'what about fragmentation' came up in my mind, because some strings might actually be stored a bit longer than others, leading to fragmentation inside the allocator.
But after reading some of the comments I figured that what I actually need is separate allocators for long-living strings/resources, such as UI elements, and for short-lived elements, like the dialogues and descriptions What I also realized thanks to @Moo-Juice is that I could introduce something like resource-groups, that would hold the group of resources needed at that time, for example when entering some location, we'd load all the things that are contained within that location. Thanks for the insight!
One thing remains a puzzle for me tho.
I've looked around Doom 3 source code, where I found a class called idStrPool
which uses new
/delete
to allocate/deallocate strings.
Can anybody comment on that? After reading some books and articles I was convinced, that such a thing is pure evil in a game engine, yet here it its.
A use case I see for this is like handling input from the player, i.e. when the player types something and we want to capture it.
Can anyone comment on that?
Edit 2
Ok, I see that guys from idSoftware have overrided the operators in Heap.h