halide / Halide

a language for fast, portable data-parallel computation
https://halide-lang.org
Other
5.78k stars 1.07k forks source link

HalideBuffer: offer a way to wrap existing memory but take ownership? #8213

Open steven-johnson opened 2 months ago

steven-johnson commented 2 months ago

We had a request to do this at Google, but it's not quite possible now: you could create a Buffer with a custom alloc/free fn pair, where the alloc does nothing and the free releases your memory... but since the free fn is a naked C fn ptr, you may or may not be able to release the memory since you might need a context to do so. (Also, we don't have an API that takes an existing ptr for data and also sets to alloc/free fns so you'd have to hack those in manually.)

Perhaps the simplest answer is to upgrade the alloc/free fns to be std::function to allow for lambdas, but that would slightly increase the size of every Buffer, since sizeof is typically sizeof(void)4 IIRC.

(The user found a workaround -- wrapping the Buffer in a unique_ptr with a custom deallocator -- so this isn't an essential fix.)