Wrapping an allocator to know how many bytes it has allocated

Hi ! I am following ‘Crafting Interpreters’ by bob nystrom and inside the book, there is a function that counts how many bytes were allocated by the VM. I want to use an allocator, but I did not find a way to get how much memory has been allocated when reading all of the function that were proposed in the interface. I don’t want to restrict myself to an allocator that does keep track of the count, so I would like to wrap a generic allocator. I am not familliar at all with interfaces in zig, how would one create such allocator ?

When I was going through that book, I came up with an allocator wrapper to do the bytes tracking. One note: this was written with 0.13 so some things have changed since then. (I should probably do an upgrade…)

This allowed me to set up the backing allocator however I wanted. I could use a Debug/GeneralPurpose allocator or an arena.

The main functions required by the Allocator interface are:
alloc, resize, and free. Then your wrapping allocator can expose an allocator() method that returns the vtable implementation with pointers to your wrapper’s implementations.

1 Like

For some inspiration, have a look at std.testing.FailingAllocator. It implements many different kinds of basic bookkeeping about the allocations it does (and their eventual failure, you probably don’t want to take inspiration from that part… :slight_smile:).

2 Likes

Another example:

(it’s written in a file-as-a-struct way)

7 Likes

Looks like we don’t take multithreading upon consideration.

Let’s suppose that wrapped allocator is already thread-safe.

It does not mean, that we don’t need to guard wrapper (decorator) calculations.

std already has thread-safe wrapper and you can use it as template or directly as wrapper for your wrapper

And in this chain of wrappers we immediately have perfomance hit, because chain of mutexes

Ofcourse we can wrap non thread-safe allocator, but in usual flow(we got allocator from caller) we cannot prevent this chain

2 Likes

This should listed in some sort of example section, these smol allocators are so usefull !! Thanks.

Multithreading is out of the question for now, I have not planned to use it inside the VM. If I ever get to this point, I may give the VM it’s own allocator and use other ones for other tasks. I don’t plan to write huge programs with it yet, so multithreading may not be necessary.

What are chains of mutexes ? Are you pointing at the fact that taking two mutexes (one inside each wrapper) is necessarily slower than having just one, or is there a deeper problem ?

1 Like

exactly

1 Like

I have finished to implement my wrapper, coding it helped me find a subtle issue where I was allocating memory using the wrapped allocator, but freeing it using my wrapper. It was nightmare-ish, I believed for a sec that I was freeing more memory than allocated, without any crash, or double free errors.
I will unsuscribe from this thread, thank you to everybody !!