libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_allocation_tracker.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblAllocationTracker, counters, and related API
3 * \ingroup allocators
4 *
5 * \author 2023 Falco Girgis
6 * \copyright MIT License
7 */
8
9#ifndef GIMBAL_ALLOCATION_TRACKER_H
10#define GIMBAL_ALLOCATION_TRACKER_H
11
12#include "../core/gimbal_typedefs.h"
13#include "gimbal/core/gimbal_stack_frame.h"
14
15#define GBL_SELF_TYPE GblAllocationTracker
16
18
19//! Counters for memory allocation and event statistics
20typedef struct GblAllocationCounters {
21 size_t allocEvents; //!< Total number of allocations
22 size_t reallocEvents; //!< Total number of reallocations
23 size_t freeEvents; //!< Total number of frees
24 size_t bytesAllocated; //!< Total number of bytes allocated
25 size_t bytesFreed; //!< Total number of bytes freed
26 ptrdiff_t bytesActive; //!< Current number of bytes active
27 ptrdiff_t allocsActive; //!< Current number of allocations active
28} GblAllocationCounters;
29
30//! Structure used for tracking allocation events and statistics
31typedef struct GblAllocationTracker {
32 size_t maxAllocations; //!< Maximum number of active allocations
33 size_t maxBytes; //!< Maximum number of allocated bytes
34 size_t maxAllocationSize; //!< Maximum size of a single allocation
35 GblAllocationCounters counters; //!< Current allocation counters
36} GblAllocationTracker;
37
38/*! \name Lifetime Management
39 * \brief Methods for creating and destroying
40 * \relatesalso GblAllocationTracker
41 * @{
42 */
43//! Creates a GblAllocationTracker and returns a pointer to it
45//! Destroys the given GblAllocation tracker, returning the result
47//! @}
48
49/*! \name Allocation Events
50 * \brief Methods for capturing allocation events
51 * \relatesalso GblAllocationTracker
52 * @{
53 */
54//! To be called every time an allocation has been requested, to track the event.
56 const void* pPtr,
57 size_t size,
58 size_t align,
59 const char* pDbg,
60 GblSourceLocation srcLoc) GBL_NOEXCEPT;
61//! To be called every time a reallocation has been requested, to track the event
63 const void* pExisting,
64 const void* pNew,
65 size_t newSize,
66 size_t newAlign,
67 GblSourceLocation srcLoc) GBL_NOEXCEPT;
68//! To be called every time a free has been requested, to track the event
70 const void* pPtr,
71 GblSourceLocation srcLoc) GBL_NOEXCEPT;
72//! @}
73
74/*! \name Utilities
75 * \brief General and other methods
76 * \relatesalso GblAllocationTracker
77 * @{
78 */
79//! Returns GBL_TRUE if the given pointer points to an active, tracked allocation
81//! Dumps all of the active allocations and their context information to the log
82
84//! @}
85
86/*! \name Counter Operations
87 * \brief Methods involving GblAllocationCountes
88 * \relatesalso GblAllocationTracker
89 * @{
90 */
91//! Saves the current value of GblAllocationTracker::counters to the given structure
93 GblAllocationCounters* pCount) GBL_NOEXCEPT;
94//! Takes the difference between GblAllocationTracker::counters and pSrc, storing the result in the pDst
96 const GblAllocationCounters* pSrc,
97 GblAllocationCounters* pDst) GBL_NOEXCEPT;
98//! @}
100
101#undef GBL_SELF_TYPE
102
103#endif // GIMBAL_ALLOCATION_TRACKER_H
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_EXPORT
#define GBL_SELF_TYPE
Definition gimbal_md5.h:22
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
Counters for memory allocation and event statistics.
ptrdiff_t bytesActive
Current number of bytes active.
size_t allocEvents
Total number of allocations.
ptrdiff_t allocsActive
Current number of allocations active.
size_t bytesFreed
Total number of bytes freed.
size_t freeEvents
Total number of frees.
size_t reallocEvents
Total number of reallocations.
size_t bytesAllocated
Total number of bytes allocated.
Structure used for tracking allocation events and statistics.
GBL_EXPORT GBL_RESULT GblAllocationTracker_allocEvent(GBL_SELF, const void *pPtr, size_t size, size_t align, const char *pDbg, GblSourceLocation srcLoc) GBL_NOEXCEPT
To be called every time an allocation has been requested, to track the event.
GBL_EXPORT GBL_RESULT GblAllocationTracker_freeEvent(GBL_SELF, const void *pPtr, GblSourceLocation srcLoc) GBL_NOEXCEPT
To be called every time a free has been requested, to track the event.
GBL_EXPORT GblBool GblAllocationTracker_validatePointer(GBL_CSELF, const void *pPtr) GBL_NOEXCEPT
Returns GBL_TRUE if the given pointer points to an active, tracked allocation.
GBL_EXPORT GBL_RESULT GblAllocationTracker_logActive(GBL_CSELF) GBL_NOEXCEPT
Dumps all of the active allocations and their context information to the log.
GblAllocationCounters counters
Current allocation counters.
GBL_EXPORT GBL_RESULT GblAllocationTracker_destroy(GBL_SELF) GBL_NOEXCEPT
Destroys the given GblAllocation tracker, returning the result.
GBL_EXPORT GblAllocationTracker * GblAllocationTracker_create(GblContext *pCtx) GBL_NOEXCEPT
Creates a GblAllocationTracker and returns a pointer to it.
GBL_EXPORT void GblAllocationTracker_captureCounters(GBL_CSELF, GblAllocationCounters *pCount) GBL_NOEXCEPT
Saves the current value of GblAllocationTracker::counters to the given structure.
size_t maxAllocationSize
Maximum size of a single allocation.
GBL_EXPORT void GblAllocationTracker_diffCounters(GBL_CSELF, const GblAllocationCounters *pSrc, GblAllocationCounters *pDst) GBL_NOEXCEPT
Takes the difference between GblAllocationTracker::counters and pSrc, storing the result in the pDst.
size_t maxAllocations
Maximum number of active allocations.
size_t maxBytes
Maximum number of allocated bytes.
GBL_EXPORT GBL_RESULT GblAllocationTracker_reallocEvent(GBL_SELF, const void *pExisting, const void *pNew, size_t newSize, size_t newAlign, GblSourceLocation srcLoc) GBL_NOEXCEPT
To be called every time a reallocation has been requested, to track the event.