skypjack / uvw

Header-only, event based, tiny and easy to use libuv wrapper in modern C++ - now available as also shared/static library!
MIT License
1.84k stars 209 forks source link

[Feature request] A customizable allocate memory function #223

Closed darsvador closed 3 years ago

darsvador commented 3 years ago

UVW provides a uv_alloc_cb to allocate memory with new, but this is inconvenient when I want to manage the buffer more complex or use malloc to allocate memory.

stefanofiorentino commented 3 years ago

Maybe somehow related to #182? (i.e. is it a generalization of that issue?)

darsvador commented 3 years ago

182 is about write operations. This issue is more about read operations.

uvw allocate memory function: https://github.com/skypjack/uvw/blob/77af4a3fc4d932a52652807506fc50d0e58e875c/src/uvw/handle.hpp#L40-L43

uvw read callback: https://github.com/skypjack/uvw/blob/77af4a3fc4d932a52652807506fc50d0e58e875c/src/uvw/stream.h#L131-L134

uvw DataEvent (UDPDataEvent): https://github.com/skypjack/uvw/blob/77af4a3fc4d932a52652807506fc50d0e58e875c/src/uvw/stream.h#L64-L67 https://github.com/skypjack/uvw/blob/77af4a3fc4d932a52652807506fc50d0e58e875c/src/uvw/udp.h#L32-L35

Since the memory allocation function uses operator new, the implementation of DataEvent assumes that unique_ptr uses the default deleter. Because these are not customizable, I can't control the process of allocating memory even if I have a chance to avoid operator new.

skypjack commented 3 years ago

How do you envision setting it up? Because doing it on a per-handle basis seems a little annoying.

darsvador commented 3 years ago

I think the default behavior is enough for most cases. Sometimes when the user wants to buffer reuse such as using a C library which buffer pointer can be then realloc etc, they must define an allocator which provides a deleter and an allocate function. uvw default allocator could be a simple trait of new [] and std::default_delete<char[]>.

skypjack commented 3 years ago

Do you mind creating a PR for that? Not even a completely refined one, but having something that shows how you would intervene would be great if possible.

darsvador commented 3 years ago

OK, I will make a basic implementation of my idea.

darsvador commented 3 years ago

@skypjack I have created a PR of the custom allocator. Please have a look.

skypjack commented 3 years ago

Hi, sorry for being late. I had some personal problems over the weekend and I didn't even turn on my pc. Thank you for the PR. I'm tryng to keep up with all the things I left behind. I'll look into it soon. 👍

skypjack commented 3 years ago

I guess we can close this. Feel free to reopen or comment the issue if you want to discuss it further. Thanks.