Get size of mem to free by free

Mark J Twain via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 5 13:54:59 PDT 2016


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;
>         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.


More information about the Digitalmars-d-learn mailing list