Skip to content

Commit

Permalink
Your commit message describing the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Oct 20, 2024
1 parent 1d2df58 commit 0620624
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 181 deletions.
60 changes: 60 additions & 0 deletions include/stumpless/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,64 @@ void *
} /* extern "C" */
# endif

/**
* 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);

#endif /* __STUMPLESS_MEMORY_H */
18 changes: 15 additions & 3 deletions include/test/helper/memory_counter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,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 @@ -106,5 +106,17 @@ PREFIX##_memory_counter_free( void *mem ) { \
#define ASSERT_NO_LEAK( PREFIX ) \
ASSERT_EQ( PREFIX##_memory_counter.alloc_total, \
PREFIX##_memory_counter.free_total )

static void* PREFIX##_get_current_malloc() {
return PREFIX##_memory_counter.previous_malloc;
}

static void* PREFIX##_get_current_realloc() {
return PREFIX##_memory_counter.previous_realloc;
}

static void PREFIX##_get_current_free() {
return PREFIX##_memory_counter.previous_free;
}

#endif /* __STUMPLESS_TEST_HELPER_MEMORY_COUNTER_HPP */
Loading

0 comments on commit 0620624

Please sign in to comment.