Skip to content

Commit

Permalink
Updated based on PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
thirtytwobits committed Apr 10, 2024
1 parent 8a9b964 commit 22e6a1a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
41 changes: 25 additions & 16 deletions o1heap/o1heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <assert.h>
#include <limits.h>

#ifdef O1HEAP_INCLUDE_CONFIG_HEADER
#include "o1heap_config.h"
#endif

// ---------------------------------------- BUILD CONFIGURATION OPTIONS ----------------------------------------

/// Define this macro to include build configuration header. This is an alternative to the -D compiler flag.
Expand Down Expand Up @@ -79,6 +83,21 @@ O1HEAP_PRIVATE uint_fast8_t O1HEAP_CLZ(const size_t x)
}
#endif

/// By defining these symbols, preferably in o1heap_config.h (define O1HEAP_INCLUDE_CONFIG_HEADER to include, see
/// above), trace tools can get events when o1heap memory is allocated or free'ed. For allocations, note that
/// if the allocated memory pointer is NULL an allocation failure has occurred. In this case the size reported is
// the number of bytes requested that the allocator could not provide.
///
/// When using the pointer provided via O1HEAP_TRACE_FREE the pointer memory must not be accessed. This pointer
/// should only be used for its address.

#ifndef O1HEAP_TRACE_ALLOCATE
# define O1HEAP_TRACE_ALLOCATE(handle, pointer, size)
#endif
#ifndef O1HEAP_TRACE_FREE
# define O1HEAP_TRACE_FREE(handle, pointer, size)
#endif

// ---------------------------------------- INTERNAL DEFINITIONS ----------------------------------------

#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
Expand Down Expand Up @@ -371,22 +390,14 @@ void* o1heapAllocate(O1HeapInstance* const handle, const size_t amount)
frag->header.used = true;

out = ((char*) frag) + O1HEAP_ALIGNMENT;
#ifdef O1HEAP_TRACE
o1heapAllocateTrace(handle, out, frag->header.size);
#endif
O1HEAP_TRACE_ALLOCATE(handle, out, frag->header.size);
} else {
O1HEAP_TRACE_ALLOCATE(handle, out, amount);
}
} else {
O1HEAP_TRACE_ALLOCATE(handle, out, amount);
}

#ifdef O1HEAP_TRACE
if (NULL == out)
{
// we didn't successfully allocate so call the trace method with a null pointer to indicate allocation
// failure. The size will be the requested size so the trace can include this information in a
// failed allocation event.
o1heapAllocateTrace(handle, out, amount);
}
#endif

// Update the diagnostics.
if (O1HEAP_LIKELY(handle->diagnostics.peak_request_size < amount))
{
Expand Down Expand Up @@ -420,9 +431,7 @@ void o1heapFree(O1HeapInstance* const handle, void* const pointer)
O1HEAP_ASSERT(frag->header.size <= handle->diagnostics.capacity);
O1HEAP_ASSERT((frag->header.size % FRAGMENT_SIZE_MIN) == 0U);

#ifdef O1HEAP_TRACE
o1heapFreeTrace(handle, pointer, frag->header.size);
#endif
O1HEAP_TRACE_FREE(handle, pointer, frag->header.size);

// Even if we're going to drop the fragment later, mark it free anyway to prevent double-free.
frag->header.used = false;
Expand Down
26 changes: 0 additions & 26 deletions o1heap/o1heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,6 @@ extern "C" {
/// The guaranteed alignment depends on the platform pointer width.
#define O1HEAP_ALIGNMENT (sizeof(void*) * 4U)

/// Define O1HEAP_TRACE to enable two extern symbols your application can implement when integrating with trace
/// tooling:
///
/// Invoked from o1heapAllocate with a pointer to the newly allocated memory and the size of the
/// memory region. Note that size_bytes is not the size of the memory requested but the size of
/// the memory fragment reserved.
///
/// if allocated_memory is NULL an allocation failure has occurred. In this case size_bytes is the
/// number of bytes requested that the allocator could not provide.
///
/// void o1heapAllocateTrace(O1HeapInstance* const handle, void* const allocated_memory, const size_t size_bytes);
///
///
/// Invoked from o1heapFree with a pointer to the previously allocated memory and the size of the
/// memory region released. Note that size_bytes is not the size of the memory originally requested
/// but the size of the memory fragment that was released.
///
/// freed_memory must not be accessed. It is provided for it's address only.
///
/// void o1heapFreeTrace(O1HeapInstance* const handle, void* const freed_memory, size_t size_bytes);
///
/// If you define this macro you _must_ implement these methods or the link stage of your build will fail.
///
/// #define O1HEAP_TRACE


/// The definition is private, so the user code can only operate on pointers. This is done to enforce encapsulation.
typedef struct O1HeapInstance O1HeapInstance;

Expand Down

0 comments on commit 22e6a1a

Please sign in to comment.