vendredi 21 octobre 2016

Delete a vector without deleting it's data. Possible?

So, this may seem like a strange idea at first, and requires some explanation. Nevertheless: Is it possible to delete a a vector, but keep it's data without copying it?

Here is some pseudo code for an example:

std::vector<uint8_t>* buffer = fileUtils.inlate(someFile);

As a required background: fileUtils.inflate() above will create a vector, and resize it as required because the final uncompressed size of some compressed file is not known in advance. A vector does that efficiently.

Later on, some other object needs to be allocated from the raw uint_8 data, like:

SomeObject* obj = createObjectFromRawData(buffer-data());

After the lifetime of object obj ends, it should be deleted. Obviously, we can't just delete the object and the buffer data without knowing the vector that owns the data.

Now, before you go on and say I'm doing it wrong and what the fuzz about this: This is about performance optimization. The obvious solution would be to copy buffer->data() into some chunk of memory and just pass a uint_8*. But that is exactly what I don't want: For sake of performance I want to avoid another copy of potentially large memory.

So a possible answer should rather consider how to use a move constructor for std::vector to transfer ownership of the data section. Or something else which I didn't consider yet.

Aucun commentaire:

Enregistrer un commentaire