If I get a MemoryBlock from a declare, If create a Binary stream from it, can I assume that all sequential writes to it be continuous in memory? Or would I have to use a pointer and/or structure overlays?
The reason I ask is that writing to a BinaryStream backed by a Memoryblock the size of the Memoryblock increases automatically as needed… but under the hood I don’t know that the data is actually contiguous in memory… it very well might not be as all the memory is not reserved at once… But for most things that would not matter for a MemoryBlock I create via New …
But it could for one from a declare where things need to go to specific locations in memory. Since such a Memoryblock as a size of -1 the framework does know it’s from a declare, so it might make sure the data does get fragmented under the hood, even if it can be for one created with new…
Am I overthinking this and being over cautious?
Does anyone know what the behavior is in this case and if is the same Xplatform?
I think a deeper problem is mixing memory allocators. It’s the memory allocator’s job to ensure that blocks are contiguous. Xojo is using its own allocator (accessed via New or by changing the MemoryBlock.Size property), while pointers returned from a declare refer to memory that was probably allocated using some variant of malloc and realloc.
A block should not be resized by an allocator that didn’t create it. This is a problem because using a BinaryStream to write to an auto-expanding memoryblock necessarily resizes the block.
If the API offers a declare for resizing then you could duplicate the BinaryStream functionality by resizing whenever you run out of room. Otherwise, the size of the blocks returned from declares should be considered fixed.
[quote=460341:@Andrew Lambert]
If the API offers a declare for resizing then you could duplicate the BinaryStream functionality by resizing whenever you run out of room. Otherwise, the size of the blocks returned from declares should be considered fixed.[/quote]
I actually don’t want more memory allocated when backed by a binary stream and planed to keep track as to not write too much… but as the memoryblock size is -1, how could the BinaryStream know when it needs to resize? Logically it should not, so it should just keep writing contiguously…
But I don’t know if this scenario was considered when the framework code for binary streams backed by a memoryblock was written! It’s not exactly a common usage.