On Friday, 5 August 2016 at 19:55:22 UTC, Mark "J" Twain wrote:
I use malloc to allocate some memory, then free it later. For monitoring purposes, I would like to know how much is free'ed by free by just knowing the object. Or, rather, given a ptr allocated by malloc, bet the block size it allocated from the ptr alone.

Some C compilers have special intrinsics and such for this, does D have any ability? If not, any hacks? I just need something that works.

You can wrap the C memory allocations functions with a version identifier, e.g


version(stat)
{
    __gshared size_t[size_t] sizes;
}

version(stat)
{
    auto malloc(size_t size)
    {
        auto result = std.c.stdlib.malloc;
        sizes[result] = size;
        return result;
    }
}
else alias malloc = std.c.stdlib.malloc;

version(stat)
{
    void free(void* ptr)
    {
        std.c.stdlib.free(ptr);
        sizes.remove(ptr);
    }
}
else alias free = std.c.stdlib.free;


that a bit a DIY but this would work.

Reply via email to