13
13
#ifndef __XDBG_H__
14
14
#define __XDBG_H__
15
15
16
- /* ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
17
- /* */
18
- /* ██╗ ██╗██████╗ ██████╗ ██████╗ */
19
- /* ╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝ */
20
- /* ╚███╔╝ ██║ ██║██████╔╝██║ ███╗ */
21
- /* ██╔██╗ ██║ ██║██╔══██╗██║ ██║ */
22
- /* ██╔╝ ██╗██████╔╝██████╔╝╚██████╔╝ */
23
- /* ╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚═════╝ */
24
- /* */
25
- /* ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
26
-
27
16
#ifdef __cplusplus
28
17
extern "C" {
29
18
#endif // __cplusplus
30
19
31
- /*****************************************************************************/
32
- /* INCLUDES */
33
- /*****************************************************************************/
34
-
35
- #include <ctype.h> // size_t
36
- #include <stdarg.h> // va_list, va_start, va_end, __VA_ARGS__
37
- #include <stdbool.h> // true, false, bool
38
- #include <stdio.h> // Includes the standard I/O library for functions like `printf`
39
- #include <stdlib.h> // Includes the standard library for functions like `malloc`, `free`, and `realloc`.
40
- #include <string.h> // memcpy
41
-
42
- /*****************************************************************************/
43
- /* ENABLE */
44
- /*****************************************************************************/
20
+ #include <ctype.h>
21
+ #include <stdarg.h>
22
+ #include <stdbool.h>
23
+ #include <stdio.h>
24
+ #include <stdlib.h>
25
+ #include <string.h>
45
26
46
27
#if defined(DEBUG ) || defined(_DEBUG )
47
28
#define XDBG_ENABLE
@@ -51,10 +32,6 @@ extern "C" {
51
32
// #define XDBG_ENABLE
52
33
#endif // XDBG_ENABLE
53
34
54
- /*****************************************************************************/
55
- /* MACROS */
56
- /*****************************************************************************/
57
-
58
35
#ifdef XDBG_ENABLE
59
36
// #define malloc(size) xdbg_malloc(size, __FILE__, __LINE__, __func__)
60
37
// #define realloc(size) xdbg_realloc(pointer,size, __FILE__, __LINE__,
@@ -71,6 +48,7 @@ extern "C" {
71
48
#define XDBG_FINALIZE ()
72
49
#endif // XDBG_ENABLE
73
50
51
+ // ANSI escape codes for logging
74
52
#define XDBG_ANSI_RED "\x1b[0;91m"
75
53
#define XDBG_ANSI_GREEN "\x1b[0;92m"
76
54
#define XDBG_ANSI_YELLOW "\x1b[0;93m"
@@ -81,106 +59,51 @@ extern "C" {
81
59
#define XDBG_ANSI_BOLD "\x1b[1m"
82
60
#define XDBG_ANSI_RESET "\x1b[0m"
83
61
84
- /*****************************************************************************/
85
- /* API */
86
- /*****************************************************************************/
87
-
88
- /**
89
- * @brief Initializes the xdbg memory tracker.
90
- *
91
- * @param file The source file where this function is called (use __FILE__).
92
- * @param line The line number of the call site (use __LINE__).
93
- * @param function The function name of the caller (use __func__).
94
- */
62
+ // Initializes the xdbg memory tracker and allocates resources. This is
63
+ // the first function that should be called.
95
64
extern void xdbg_initialize (const char * file , unsigned int line ,
96
65
const char * function );
97
66
98
- /**
99
- * @brief Clears all memory tracking information.
100
- *
101
- * @param file The source file where this function is called.
102
- * @param line The line number of the call site.
103
- * @param function The function name of the caller.
104
- */
67
+ // Clears all memory tracking information and frees all allocated
68
+ // resources. This should be called at the very end.
105
69
extern void xdbg_finalize (const char * file , unsigned int line ,
106
70
const char * function );
107
71
108
- /**
109
- * @brief Reports currently tracked allocations.
110
- *
111
- * @param file The source file where this function is called.
112
- * @param line The line number of the call site.
113
- * @param function The function name of the caller.
114
- */
72
+ // Reports currently tracked allocations along with relevant information such as
73
+ // address, file, line, caller function, memory operations and size.
115
74
extern void xdbg_report (const char * file , unsigned int line ,
116
75
const char * function );
117
76
118
- /**
119
- * @brief Allocates memory and tracks the allocation.
120
- *
121
- * @param size The number of bytes to allocate.
122
- * @param file The source file where this function is called.
123
- * @param line The line number of the call site.
124
- * @param function The function name of the caller.
125
- * @return Pointer to the allocated memory, or NULL on failure.
126
- */
77
+ // Allocates memory and tracks the allocation.
78
+ // Return pointer to the allocated memory, or NULL on failure.
127
79
extern void * xdbg_malloc (size_t size , const char * file , unsigned int line ,
128
80
const char * function );
129
81
130
- /**
131
- * @brief Allocates zero-initialized memory and tracks the allocation.
132
- *
133
- * @param number Number of elements to allocate.
134
- * @param size Size of each element in bytes.
135
- * @param file The source file where this function is called.
136
- * @param line The line number of the call site.
137
- * @param function The function name of the caller.
138
- * @return Pointer to the allocated memory, or NULL on failure.
139
- */
82
+ // Allocates zero-initialized memory and tracks the allocation.
83
+ // Return pointer to the allocated memory, or NULL on failure.
140
84
extern void * xdbg_calloc (size_t number , size_t size , const char * file ,
141
85
unsigned int line , const char * function );
142
86
143
- /**
144
- * @brief Reallocates memory and tracks the new allocation.
145
- *
146
- * @param pointer Pointer to the previously allocated memory.
147
- * @param size New size in bytes.
148
- * @param file The source file where this function is called.
149
- * @param line The line number of the call site.
150
- * @param function The function name of the caller.
151
- * @return Pointer to the reallocated memory, or NULL on failure.
152
- */
87
+ // Reallocates memory and tracks the new allocation.
88
+ // Return pointer to the reallocated memory, or NULL on failure.
153
89
extern void * xdbg_realloc (void * pointer , size_t size , const char * file ,
154
90
unsigned int line , const char * function );
155
91
156
- /**
157
- * @brief Frees memory and removes it from the tracking system.
158
- *
159
- * @param pointer Pointer to the memory block to free.
160
- * @param file The source file where this function is called.
161
- * @param line The line number of the call site.
162
- * @param function The function name of the caller.
163
- */
92
+ // Wrapper around free() that tracks valid free operations and reports invalid
93
+ // free operations.
164
94
extern void xdbg_free (void * pointer , const char * file , unsigned int line ,
165
95
const char * function );
166
96
167
- /**
168
- * @brief Reports all memory allocations that were not freed.
169
- *
170
- * Prints detailed information about memory leaks.
171
- */
172
- extern void xdbg_report_leaks (void ); // TODO: Implement
173
-
174
- /**
175
- * @brief Resets all memory tracking state.
176
- *
177
- * Useful for test environments where tracking needs to be cleared between test
178
- * cases.
179
- */
180
- extern void xdbg_reset_memory_tracker (void ); // TODO: Implement
181
-
182
- extern void xdbg_printf (const char * file , unsigned int line ,
183
- const char * function , const char * format , ...);
97
+ // TODO: Implement
98
+ // Reports all memory allocations that were not freed.
99
+ // Prints detailed information about memory leaks.
100
+ extern void xdbg_report_leaks (void );
101
+
102
+ // TODO: Implement
103
+ // Resets all memory tracking state.
104
+ // Useful for test environments where tracking needs to be cleared between test
105
+ // cases.
106
+ extern void xdbg_reset_memory_tracker (void );
184
107
185
108
#ifdef __cplusplus
186
109
}
0 commit comments