Skip to content

Commit c226760

Browse files
updates
1 parent 22e6a1a commit c226760

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ If not overridden by the user, for some compilers `O1HEAP_CLZ(x)` will expand to
238238
For other compilers it will default to a slow software implementation,
239239
which is likely to significantly degrade the performance of the library.
240240

241+
#### O1HEAP_TRACE
242+
243+
This option is intended for advanced diagnostics and may be not useful in most applications.
244+
If defined and is nonzero, makes o1heap invoke `extern` trace functions (implemented in the application)
245+
on every allocation and deallocation. Please refer to `o1heap.h` for usage details.
246+
241247
## Development
242248

243249
### Dependencies

o1heap/o1heap.c

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@
1111
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1212
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1313
//
14-
// Copyright (c) 2020 Pavel Kirienko
14+
// Copyright (c) Pavel Kirienko
1515
// Authors: Pavel Kirienko <[email protected]>
1616

17+
// ReSharper disable CppDFANullDereference
18+
1719
#include "o1heap.h"
1820
#include <assert.h>
1921
#include <limits.h>
2022

21-
#ifdef O1HEAP_INCLUDE_CONFIG_HEADER
22-
#include "o1heap_config.h"
23-
#endif
24-
2523
// ---------------------------------------- BUILD CONFIGURATION OPTIONS ----------------------------------------
2624

2725
/// Define this macro to include build configuration header. This is an alternative to the -D compiler flag.
@@ -83,19 +81,18 @@ O1HEAP_PRIVATE uint_fast8_t O1HEAP_CLZ(const size_t x)
8381
}
8482
#endif
8583

86-
/// By defining these symbols, preferably in o1heap_config.h (define O1HEAP_INCLUDE_CONFIG_HEADER to include, see
87-
/// above), trace tools can get events when o1heap memory is allocated or free'ed. For allocations, note that
88-
/// if the allocated memory pointer is NULL an allocation failure has occurred. In this case the size reported is
89-
// the number of bytes requested that the allocator could not provide.
90-
///
91-
/// When using the pointer provided via O1HEAP_TRACE_FREE the pointer memory must not be accessed. This pointer
92-
/// should only be used for its address.
93-
94-
#ifndef O1HEAP_TRACE_ALLOCATE
95-
# define O1HEAP_TRACE_ALLOCATE(handle, pointer, size)
84+
/// If O1HEAP_TRACE is defined and is nonzero, trace tools can get events when o1heap memory is allocated or freed.
85+
/// The corresponding events are delivered by invoking extern functions o1heapTraceAllocate() etc, defined in the
86+
/// application. Please refer to the documentation for those functions for the additional information.
87+
#ifndef O1HEAP_TRACE
88+
# define O1HEAP_TRACE 0
9689
#endif
97-
#ifndef O1HEAP_TRACE_FREE
98-
# define O1HEAP_TRACE_FREE(handle, pointer, size)
90+
#if O1HEAP_TRACE
91+
# define O1HEAP_TRACE_ALLOCATE(handle, pointer, size) o1heapTraceAllocate(handle, pointer, size)
92+
# define O1HEAP_TRACE_FREE(handle, pointer, size) o1heapTraceFree(handle, pointer, size)
93+
#else
94+
# define O1HEAP_TRACE_ALLOCATE(handle, pointer, size) (void) 0
95+
# define O1HEAP_TRACE_FREE(handle, pointer, size) (void) 0
9996
#endif
10097

10198
// ---------------------------------------- INTERNAL DEFINITIONS ----------------------------------------
@@ -259,12 +256,6 @@ O1HEAP_PRIVATE void unbin(O1HeapInstance* const handle, const Fragment* const fr
259256
}
260257
}
261258

262-
#ifdef O1HEAP_TRACE
263-
// The user must implement these functions or linking will fail.
264-
extern void o1heapAllocateTrace(O1HeapInstance* const handle, void* const allocated_memory, size_t size_bytes);
265-
extern void o1heapFreeTrace(O1HeapInstance* const handle, void* const freed_memory, size_t size_bytes);
266-
#endif
267-
268259
// ---------------------------------------- PUBLIC API IMPLEMENTATION ----------------------------------------
269260

270261
O1HeapInstance* o1heapInit(void* const base, const size_t size)
@@ -391,10 +382,14 @@ void* o1heapAllocate(O1HeapInstance* const handle, const size_t amount)
391382

392383
out = ((char*) frag) + O1HEAP_ALIGNMENT;
393384
O1HEAP_TRACE_ALLOCATE(handle, out, frag->header.size);
394-
} else {
385+
}
386+
else
387+
{
395388
O1HEAP_TRACE_ALLOCATE(handle, out, amount);
396389
}
397-
} else {
390+
}
391+
else
392+
{
398393
O1HEAP_TRACE_ALLOCATE(handle, out, amount);
399394
}
400395

o1heap/o1heap.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
1212
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1313
//
14-
// Copyright (c) 2020 Pavel Kirienko
14+
// Copyright (c) Pavel Kirienko
1515
// Authors: Pavel Kirienko <[email protected]>
1616
//
1717
// READ THE DOCUMENTATION IN README.md.
@@ -116,6 +116,19 @@ bool o1heapDoInvariantsHold(const O1HeapInstance* const handle);
116116
/// If the handle pointer is NULL, the behavior is undefined.
117117
O1HeapDiagnostics o1heapGetDiagnostics(const O1HeapInstance* const handle);
118118

119+
/// Advanced diagnostic hooks; not used by default.
120+
/// If O1HEAP_TRACE is not defined or is zero, these functions should be left unimplemented.
121+
/// Iff O1HEAP_TRACE is defined and is nonzero, the library will emit trace events by invoking these functions,
122+
/// which are to be defined in the application (or linking will fail).
123+
///
124+
/// For allocations, note that if the allocated memory pointer is NULL, an allocation failure has occurred.
125+
/// In this case, the size reported is the number of bytes requested that the allocator could not provide.
126+
///
127+
/// When using the pointer provided via o1heapTraceFree(), the pointer memory must not be accessed.
128+
/// This pointer should only be used for its address.
129+
extern void o1heapTraceAllocate(O1HeapInstance* const handle, void* const allocated_memory, size_t size);
130+
extern void o1heapTraceFree(O1HeapInstance* const handle, void* const freed_memory, size_t size);
131+
119132
#ifdef __cplusplus
120133
}
121134
#endif

0 commit comments

Comments
 (0)