Custom allocator for just one object?

From googling around, I guess the answer is NO, but I thought I would still ask or find alternate suggestions etc..

So lets say I want a struct Packet {} which on a networking device is basically data that comes in / sent out on the ethernet interfaces. And the packets usually come from specific pools (like the linux high memory). So if I have mapped in the packet high mem and doing some pool management of my own, can I have just my Packet {} structures in the code to be allocated using that high mem allocator ?

Rgds,
Gopa.

Yes, you can. Box allocates objects using the default allocator. You can make struct YourOwnBox<T>(*mut T) that allocates the pointer using some other allocator. Implement Drop and Deref/DerefMut for it.

Alternatively, if you already have a Packet allocated somewhere, you can use *mut Packet or cast that pointer to &Packet or &mut Packet accordingly, if you know it's non-null and won't be freed unexpectedly while that reference is usable.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.