Add custom per object before-free hook function
Is it possible to define a custom function that will be called whenever allocated block is called ?
Motivation: I have existing project, where large objects with fixed arrays in them are allocated (objects can be >1mb). I would like to replace some of the fixed size array with malloced arrays. I hope to be able to attach custom free function to those objects, which will free the malloced arrays, when the main object is freed. I cannot change all the code base to replace the free, with ‘custom-free’ - to much work. I can fix the allocators - they are few of them.
Conceptually a destructor, similar to c++ delete or the gcc/clang cleanup attribute extension, for dynamically allocated memory.
Basically, I want to replace:
Struct big {
Double x,y,Z ;
Double a[1000000] ; // max value, but usually much smaller.
}
With:
Struct big {
Double x,y,Z ;
Double *a ; // max value, but usually much smaller.
}
allocator will be:
Struct big *alloc-big()
{
Struct big *p= callow(…);
Mi-set-free(p, free-big); // free big will be called when free(p) is called
P->a = calloc(actual size);
}