Today I implemented the reference allocator for resources that are going to be freed sooner or later.
I also created an Allocator class with an alloc method that takes one enum parameter. Depending on this parameter the resource is allocated with one of the two different allocators I've implemented so far.
The problem with this is that it's really ugly to call somethin like:
Obj* new_obj = nullptr;
Allocator::alloc(&new_obj, STACK_TEMP);
each time an object is created. So I overloaded the "new" constructor of the instantiable classes to allocate memory at a custom address obtained with the allocators instead of a malloc. The code looks like this:
Obj* new_obj = new (STACK_TEMP) Obj();
which is exacly like a normal constructor, but specifies where to get the memory for the allocation.
I'm not happy with copy constructors though, because they often call copy constructors on each pointer inside the class, and there is no way to tell where to allocate memory for these. Right now I'm passing the enum as a parameter, so you can chose where to store all the dependent pointers, but this is both ugly and doesn't allow you to differentiate between different pointers:
Obj* new_obj = new (STACK_TEMP) Obj(old_obj, STACK_TEMP);
Nessun commento:
Posta un commento