Get size of mem to free by free
H.Loom via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Aug 5 14:12:59 PDT 2016
On Friday, 5 August 2016 at 21:07:02 UTC, H.Loom wrote:
> On Friday, 5 August 2016 at 20:54:59 UTC, Mark "J" Twain wrote:
>> On Friday, 5 August 2016 at 20:43:12 UTC, H.Loom wrote:
>>> On Friday, 5 August 2016 at 19:55:22 UTC, Mark "J" Twain
>>> wrote:
>>>> [...]
>>>
>>> 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(size);
>>> 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.
>>
>> Yeah, I ended up just storing the sizes in the ptr on malloc.
>> Essentially the same but I don't have to keep a global around
>> and deal with that.
>
> you cannot get rid of a global variable to store the sizes.
> Even if it's hidden in an API you didn't write, in which case
> it's just hidden.
>
> Have you looked at experimental allocators ?
More especially
https://github.com/dlang/phobos/blob/master/std/experimental/allocator/building_blocks/stats_collector.d
instead of using malloc/free you can use Mallocator (this is the
same).
Maybe that StatsCollector!Mallocator == win for you.
More information about the Digitalmars-d-learn
mailing list