Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom per object before-free hook function #947

Open
yairlenga opened this issue Oct 17, 2024 · 0 comments
Open

Add custom per object before-free hook function #947

yairlenga opened this issue Oct 17, 2024 · 0 comments

Comments

@yairlenga
Copy link

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant