|
memory_arena
|
Functions for recording, retrieving, and printing memory usage statistics. More...
Files | |
| file | arena_stats.c |
| Arena statistics and diagnostics module. | |
| file | arena_stats.h |
| Runtime statistics tracking and diagnostics for memory arenas. | |
Data Structures | |
| struct | s_arena_stats |
| Structure holding runtime statistics for an arena. More... | |
Typedefs | |
| typedef struct s_arena_stats | t_arena_stats |
| Structure holding runtime statistics for an arena. | |
Functions | |
| t_arena_stats | arena_get_stats (const t_arena *arena) |
| Return a snapshot of the arena's internal statistics. | |
| void | arena_print_stats (t_arena *arena, FILE *stream) |
| Print formatted diagnostics about the arena to a stream. | |
| void | arena_stats_record_growth (t_arena_stats *stats, size_t size) |
Append a new growth event to the growth_history array. | |
| void | arena_stats_reset (t_arena_stats *stats) |
| Reset the given statistics structure to zero. | |
Functions for recording, retrieving, and printing memory usage statistics.
This group provides introspection into arena usage patterns:
Useful for profiling, visualization, or custom allocator analytics.
| struct s_arena_stats |
Structure holding runtime statistics for an arena.
Tracks all major allocation events, usage metrics, and debug counters. Fields are updated automatically by internal arena operations.
Definition at line 53 of file arena_stats.h.
| typedef struct s_arena_stats t_arena_stats |
#include <arena_stats.h>
Structure holding runtime statistics for an arena.
Tracks all major allocation events, usage metrics, and debug counters. Fields are updated automatically by internal arena operations.
| t_arena_stats arena_get_stats | ( | const t_arena * | arena | ) |
#include <arena_stats.h>
Return a snapshot of the arena's internal statistics.
Retrieve a snapshot of the current arena statistics.
| arena | Pointer to the arena. |
t_arena_stats structure.This function returns a copy of the t_arena_stats structure from the specified arena. The statistics include information such as:
This is useful for diagnostics, profiling, logging, or exporting memory usage metrics to external tools.
The returned stats are safe to use outside the arena and will not be affected by future changes to the arena state.
Thread-safe: acquires the arena lock before reading the data.
| arena | Pointer to the arena to inspect. |
arena is NULL, returns a zero-initialized t_arena_stats structure.growth_history pointer in the returned struct still points to internal memory managed by the arena. Do not modify or free it manually.Definition at line 218 of file arena_stats.c.
References ARENA_LOCK, and ARENA_UNLOCK.
| void arena_print_stats | ( | t_arena * | arena, |
| FILE * | stream | ||
| ) |
#include <arena_stats.h>
Print formatted diagnostics about the arena to a stream.
Print detailed diagnostics about the given arena to a stream.
| arena | Pointer to the arena to inspect. |
| stream | Stream to write the output to (e.g., stdout, stderr). |
This function outputs a formatted summary of the current arena state to the specified FILE* stream. It includes memory layout, allocation statistics, debug metadata, and growth history (if available).
Sections reported:
This function is useful for:
| arena | Pointer to the arena to inspect. |
| stream | Output stream to write the diagnostics to (e.g., stdout, stderr, or a log file). |
arena or stream is NULL, the function does nothing. Definition at line 74 of file arena_stats.c.
| void arena_stats_record_growth | ( | t_arena_stats * | stats, |
| size_t | new_size | ||
| ) |
#include <arena_stats.h>
Append a new growth event to the growth_history array.
Record a new arena growth event in the statistics.
| stats | Pointer to the stats structure to update. |
| size | The new size of the arena after a growth. |
This function appends a new size value to the growth_history array inside the provided t_arena_stats structure. It tracks each time the arena grows dynamically (via arena_grow()), enabling tools and diagnostics to analyze memory usage trends over time.
Internally:
growth_history array to make room for the new entry.new_size to the end of the list.growth_history_count.If memory reallocation fails, the function logs a debug message via ALOG (if debug logging is enabled) and exits silently without recording the growth.
| stats | Pointer to the t_arena_stats structure to update. |
| new_size | The new arena size (in bytes) after a growth operation. |
arena_grow(). The growth_history buffer is dynamically managed and grows linearly.stats is NULL. Memory allocation failure is non-fatal but results in data loss for this event.Definition at line 164 of file arena_stats.c.
References ALOG, s_arena_stats::growth_history, and s_arena_stats::growth_history_count.
| void arena_stats_reset | ( | t_arena_stats * | stats | ) |
#include <arena_stats.h>
Reset the given statistics structure to zero.
Reset all allocation statistics in an arena stats structure.
| stats | Pointer to the stats structure to reset. |
This function clears all fields in a t_arena_stats structure, effectively resetting the arena's statistical tracking to an initial state.
It resets:
This is typically called during arena initialization or reuse.
| stats | Pointer to the t_arena_stats structure to reset. |
stats is NULL, the function does nothing.Definition at line 187 of file arena_internal.c.
References s_arena_stats::alloc_id_counter, s_arena_stats::allocations, s_arena_stats::bytes_allocated, s_arena_stats::failed_allocations, s_arena_stats::growth_history, s_arena_stats::growth_history_count, s_arena_stats::last_alloc_id, s_arena_stats::last_alloc_offset, s_arena_stats::last_alloc_size, s_arena_stats::live_allocations, s_arena_stats::peak_usage, s_arena_stats::reallocations, s_arena_stats::shrinks, and s_arena_stats::wasted_alignment_bytes.