hnisula / ducklib

A library made out of ducks
1 stars 0 forks source link

Improve memory allocation code #18

Closed hnisula closed 2 years ago

hnisula commented 2 years ago

source_location ~seemed nice but as there is no support (at least in MSVC) for having a default parameter with variadic arguments it was not useful, yet.~ could perhaps also be used.

hnisula commented 2 years ago

If construction and allocation is separated then there is no need for arguments with variadic templates as well as the source_location default parameter. It will, however, be more verbose. Parameter tooltips will also be supported.

hnisula commented 2 years ago

Preferably something simple and not over-engineered. Putting more responsibility on the user is probably the way to go. Using placement new is a bit more verbose but perhaps the simplest and clearest way to do it.

IAlloc& alloc = DefaultAlloc();
MyObject obj = new (alloc.Allocate(sizeof(MyObject), alignof(MyObject)) MyObject(...);

MyObject obj = new (alloc.Allocate<MyObject>()) MyObject(...);

MyObject* ptr = DefaultAlloc().Allocate<MyObject>();
MyObject obj = new (ptr) MyObject(...);

// Macro, as the current implementation? Could be optional as the tracking will still be done.
MyObject obj = DL_NEW(DefAlloc(), MyObject, ...);
hnisula commented 2 years ago

An easy way to construct an array object from a simple array would be nice.

UPDATE: What happens when operations that normally dynamically increase the size can not because it is an external array with unknown allocator? Possibly hand them the allocator as well to add another case where this is possible?

It quickly becomes more complex. The idea was to easily offer array operations on an existing array with minimal overhead and interference (i.e. no construction or deletion). The user should be notified or could even have an exception thrown at them if this is done.