Skip to content

malloc user callbacks for the proxy lib proposal #1048

Open
@bratpiorka

Description

@bratpiorka

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions