On Tuesday, 9 August 2022 at 22:02:37 UTC, Guillaume Piolat wrote:
On Monday, 8 August 2022 at 16:07:54 UTC, wjoe wrote:
your lib would not just be @nogc but @no_allocation.

All image decoders in gamut need to malloc more than just for pixel data. Even STB allocates for format conversion, zlib buffers, 16-bit <-> 8-bit, etc. it's not just pixel data. Single allocation pessimizes the size a lot because since you haven't encoded yet, you need to prepare a buffer for a large worst-case.

I imagined you could allocate internal buffers for encoding/decoding on the stack but your reply suggests otherwise.

However shouldn't a single function call back be enough? Something like

``` D
void[] need_more_ram(size_t amount, void[] old_chunk, void* user)
{
  MyAllocator* a = cast(MyAllocator*)user;
void[] result = a.alloc(amount); // MyAllocator would return an empty slice if amount == 0 if (amount && chunk.length) result[0..chunk.length] = chunk; // it is assumed that on re-allocation amount > chunk.length
  a.free(chunk.ptr);
  return result;
}
```

Reply via email to