Hey all,
I'm struggling a bit with defining the approach of handling allocation for the mesh instances in my 3d scene class. Some remarks:
- I want to be able to identify them using a unique numeric ID (ideally the vector/array index)
- don't need a full memory pool functionality, because the size in bytes for a single mesh instance object can vary
- looking for a not overcomplex but in balance easy solution, can always improve later on
So far I've come up with 4 different options.
Question; what would you do/ what other options do you see?
Option 1:
Class members:
std::vector<C3dMeshInst> mMeshInstances>
std::vector<uint> mFreeList;
Initial loading:
- reserve(nrMeshInst in scene + some margin)
- load up all initial instances
- add remaining IDs in mFreelist
Add inst:
- Get ID from free list
- Assignment/copy, new mesh inst to free ID
Remove inst:
- 'reset' object in vector, assignment/copy = C3dMeshInst();
- add ID (vector index) to freelist
Option 2:
Class members:
C3dMeshInst *mMeshInstances;
std::vector<uint> mFreeList;
Initial:
- mMeshInstances = new C3dMeshInst(nrMeshInst in scene + some margin)
- load up all initial instances
- add remaining IDs in mFreeList
Add inst:
- Get ID from freelist
- Assignment/copy, new mesh inst to free ID
Remove inst:
- 'reset', by assignment/copy = C3dMeshInst();
- add ID (vector index) to freelist
Option 3:
Class members:
std::vector<C3dMeshInst*> mMeshInstances;
std::vector<uint> mFreeList;
Initial:
- resize mMeshInstances, nrMeshInst in scene + margin
- load up all initial instances, using new C3dMeshInst and assigning pointers in vector
Add inst:
- get vector index from freelist
- 'new' the new mesh inst, assign to pointer in vector
Remove inst:
- 'delete' the mesh inst
- add vector index to mFreeList
Option 4:
see 3, but:
- without freelist
- when removing just move .back() to deleted index
- when adding just push_back new pointer
- downside: ID is no longer guaranteed from vector index (might need ID var in C3dMeshInst class)