dimanche 25 août 2019

Efficiently managing a vector of (objects containing a vector)

I am looking at a custom memory allocator that uses std::vector to store small objects. My question is that when we have a vector that contains objects that themselves have a vector data member:

1) How are they stored? Is it contiguous block of memory?

2) How should it be managed efficiently? Reserving memory for inside vectors will help I suppose. Any other suggestions?

My main concern is that if the vector inside one of the object fills up to capacity, it will need to move to bigger space and hence the whole vector<object> chain will have to reallocate itself.

class SmallObjAllocator{

void* Allocate(){}; //Add to appropriate FixedAllocator or create new FixedAllocator in pool_

//other stuff...

std:vector<FixedAllocator> pool_;

};


class FixedAllocator{

void* Allocate(){}; //Add new chunk to chunk_ vector if needed

//other stuff...

vector<Chunk> chunks_

};

Aucun commentaire:

Enregistrer un commentaire