Skip to content

Commit

Permalink
add allocator accessors
Browse files Browse the repository at this point in the history
Adds functions to retrieve the functions used for
memory allocation (malloc, realloc, and free).
  • Loading branch information
tertyshnyi authored Oct 22, 2024
1 parent 1d2df58 commit e01c679
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 182 deletions.
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

0 comments on commit e01c679

Please sign in to comment.