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 memory function accessors #468

Merged
merged 6 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions include/stumpless/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,66 @@ void *
( *stumpless_set_realloc( void * ( *realloc_func ) ( void *, size_t) ) )
( void *, size_t );

/**
* Retrieves the current malloc function used by the library.
*
* **Thread Safety: MT-Safe**
* This function is thread-safe as it does not modify any global state.
* It simply retrieves a pointer to the current memory allocation function.
*
* **Async Signal Safety: AS-Safe**
* This function is safe to call from signal handlers since it does not
* cause any side effects or modify state.
*
* **Async Cancel Safety: AC-Safe**
* This function is safe to call from threads that may be asynchronously
* cancelled as it does not perform any blocking operations.
*
* @return A pointer to the current malloc function.
*/
STUMPLESS_PUBLIC_FUNCTION
void *(*stumpless_get_malloc(void))(size_t size);

/**
* Retrieves the current free function used by the library.
*
* **Thread Safety: MT-Safe**
* This function is thread-safe as it does not modify any global state.
* It simply retrieves a pointer to the current memory deallocation function.
*
* **Async Signal Safety: AS-Safe**
* This function is safe to call from signal handlers since it does not
* cause any side effects or modify state.
*
* **Async Cancel Safety: AC-Safe**
* This function is safe to call from threads that may be asynchronously
* cancelled as it does not perform any blocking operations.
*
* @return A pointer to the current free function.
*/
STUMPLESS_PUBLIC_FUNCTION
void (*stumpless_get_free(void))(void *ptr);

/**
* Retrieves the current realloc function used by the library.
*
* **Thread Safety: MT-Safe**
* This function is thread-safe as it does not modify any global state.
* It simply retrieves a pointer to the current memory reallocation function.
*
* **Async Signal Safety: AS-Safe**
* This function is safe to call from signal handlers since it does not
* cause any side effects or modify state.
*
* **Async Cancel Safety: AC-Safe**
* This function is safe to call from threads that may be asynchronously
* cancelled as it does not perform any blocking operations.
*
* @return A pointer to the current realloc function.
*/
STUMPLESS_PUBLIC_FUNCTION
void *(*stumpless_get_realloc(void))(void *ptr, size_t size);

# ifdef __cplusplus
} /* extern "C" */
# endif
Expand Down
9 changes: 5 additions & 4 deletions include/test/helper/memory_counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <cstddef>
#include <cstdlib>
#include <stumpless.h>
#include <stumpless/memory.h>

struct memory_counter {
size_t malloc_count;
Expand All @@ -41,9 +42,9 @@ PREFIX##_memory_counter.alloc_total = 0; \
PREFIX##_memory_counter.realloc_count = 0; \
PREFIX##_memory_counter.free_count = 0; \
PREFIX##_memory_counter.free_total = 0; \
PREFIX##_memory_counter.previous_malloc = malloc; \
PREFIX##_memory_counter.previous_realloc = realloc; \
PREFIX##_memory_counter.previous_free = free; \
PREFIX##_memory_counter.previous_malloc = stumpless_get_malloc(); \
PREFIX##_memory_counter.previous_realloc = stumpless_get_realloc(); \
PREFIX##_memory_counter.previous_free = stumpless_get_free(); \
stumpless_set_malloc( PREFIX##_memory_counter_malloc ); \
stumpless_set_realloc( PREFIX##_memory_counter_realloc ); \
stumpless_set_free( PREFIX##_memory_counter_free );
Expand Down Expand Up @@ -107,4 +108,4 @@ PREFIX##_memory_counter_free( void *mem ) { \
ASSERT_EQ( PREFIX##_memory_counter.alloc_total, \
PREFIX##_memory_counter.free_total )

#endif /* __STUMPLESS_TEST_HELPER_MEMORY_COUNTER_HPP */
#endif /* __STUMPLESS_TEST_HELPER_MEMORY_COUNTER_HPP */
Loading
Loading