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

malloc user callbacks for the proxy lib proposal #1048

Open
bratpiorka opened this issue Jan 20, 2025 · 0 comments
Open

malloc user callbacks for the proxy lib proposal #1048

bratpiorka opened this issue Jan 20, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@bratpiorka
Copy link
Contributor

bratpiorka commented Jan 20, 2025

Malloc user callbacks for the proxy lib proposal.

Rationale

Some users are interested in performing additional actions each time the app launches with the UMF proxy library, which utilizes malloc/free functions. These actions may include printing messages, updating statistics, adding user-defined metadata to the allocation, or modifying the default behaviors of the UMF based on callback parameters. These objectives can be achieved by adding an API that allows the registration of callbacks to be invoked by the UMF.

Description

This API proposal introduces functions to the UMF Proxy Library to register user-defined callbacks and define their signatures. Each function managed by the UMF Proxy Library will trigger Pre and Post callbacks, which are called before and after the UMF performs the desired action, respectively. Each callback will also receive a pointer to user-defined data, which can be unique for each callback. Users can "clear" a callback by setting NULL as a parameter to the umfSetProxyLibHandler*Pre/Post functions, which may be important to do before the application closes to not crash any system malloc/free calls.

Note that, where applicable, the parameters passed to the callbacks can be both read and written, allowing users to modify, for example, the size of the allocation in mallocPre or to shift the pointer in mallocPost.

Question: Currently, the callbacks are not "chained," meaning that for a particular Pre/Post action, only one callback can be defined. Should we allow multiple callbacks to be set for a single Pre/Post action?

API Changes

// callback signatures - malloc
void (*umf_proxy_lib_handler_malloc_pre_t)(void *user_data, size_t *size)
void (*umf_proxy_lib_handler_malloc_post_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)

// aligned alloc
void (*umf_proxy_lib_handler_aligned_malloc_pre_t)(void *user_data, umf_memory_pool_handle_t *pool, size_t *size, size_t *alignment)
void (*umf_proxy_lib_handler_aligned_malloc_post_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)

// free
(*umf_proxy_lib_handler_free_pre_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)
(*umf_proxy_lib_handler_free_post_t)(void *user_data, void **ptr, umf_memory_pool_handle_t pool)

// TODO calloc, realloc, malloc_usable_size

// setters

void umfSetProxyLibHandlerMallocPre(umf_proxy_lib_handler_malloc_pre_t handler, void *user_data);
void umfSetProxyLibHandlerMallocPost(umf_proxy_lib_handler_malloc_post_t handler, void *user_data);

void umfSetProxyLibHandlerAlignedMallocPre(umf_proxy_lib_handler_aligned_malloc_pre_t handler, void *user_data);
void umfSetProxyLibHandlerAlignedMallocPost(umf_proxy_lib_handler_aligned_malloc_post_t handler, void *user_data);

// etc ...

POC: https://github.com/oneapi-src/unified-memory-framework/pull/978/files

@bratpiorka bratpiorka added the enhancement New feature or request label Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant