Skip to content

Commit b234b9b

Browse files
authored
Merge pull request #4 from ragibasif/refactor
clean up header and readme
2 parents 1619d66 + f0a9341 commit b234b9b

File tree

2 files changed

+31
-118
lines changed

2 files changed

+31
-118
lines changed

README.md

-10
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,11 @@
22

33
`xdbg` is a debugger for dynamically allocated memory that tracks and logs every `malloc`, `realloc`, `calloc`, and `free` and displays them to standard output to help find memory leaks, memory corruption, overflows.
44

5-
> [!note]
6-
> This library is mostly complete. By that I mean the required and intended functionalities are implemented, however there are still some bugs that need to be fixed.
7-
8-
> [!warning]
9-
>
10-
> - **WIP!**
11-
> - **NOT THREAD SAFE!** (_yet_)
12-
> - Exclusively developed and tested on MacOS M1 (_for now_)
13-
145
- Memory Debugging – Detect leaks, corruption, overflows.
156
- Minimal Overhead – As light as possible for fast debugging.
167
- Toggle debugging
178
- Track the file, line, and function where memory was allocated.
189
- Logs and reports each action: `malloc`, `realloc`, `calloc`, and `free`.
19-
- Uses a linked lists to maintain order of allocation.
2010

2111
## Usage
2212

xdbg.h

+31-108
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,16 @@
1313
#ifndef __XDBG_H__
1414
#define __XDBG_H__
1515

16-
/* ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
17-
/* */
18-
/* ██╗ ██╗██████╗ ██████╗ ██████╗ */
19-
/* ╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝ */
20-
/* ╚███╔╝ ██║ ██║██████╔╝██║ ███╗ */
21-
/* ██╔██╗ ██║ ██║██╔══██╗██║ ██║ */
22-
/* ██╔╝ ██╗██████╔╝██████╔╝╚██████╔╝ */
23-
/* ╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚═════╝ */
24-
/* */
25-
/* ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' */
26-
2716
#ifdef __cplusplus
2817
extern "C" {
2918
#endif // __cplusplus
3019

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>
4526

4627
#if defined(DEBUG) || defined(_DEBUG)
4728
#define XDBG_ENABLE
@@ -51,10 +32,6 @@ extern "C" {
5132
// #define XDBG_ENABLE
5233
#endif // XDBG_ENABLE
5334

54-
/*****************************************************************************/
55-
/* MACROS */
56-
/*****************************************************************************/
57-
5835
#ifdef XDBG_ENABLE
5936
// #define malloc(size) xdbg_malloc(size, __FILE__, __LINE__, __func__)
6037
// #define realloc(size) xdbg_realloc(pointer,size, __FILE__, __LINE__,
@@ -71,6 +48,7 @@ extern "C" {
7148
#define XDBG_FINALIZE()
7249
#endif // XDBG_ENABLE
7350

51+
// ANSI escape codes for logging
7452
#define XDBG_ANSI_RED "\x1b[0;91m"
7553
#define XDBG_ANSI_GREEN "\x1b[0;92m"
7654
#define XDBG_ANSI_YELLOW "\x1b[0;93m"
@@ -81,106 +59,51 @@ extern "C" {
8159
#define XDBG_ANSI_BOLD "\x1b[1m"
8260
#define XDBG_ANSI_RESET "\x1b[0m"
8361

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.
9564
extern void xdbg_initialize(const char *file, unsigned int line,
9665
const char *function);
9766

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.
10569
extern void xdbg_finalize(const char *file, unsigned int line,
10670
const char *function);
10771

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.
11574
extern void xdbg_report(const char *file, unsigned int line,
11675
const char *function);
11776

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.
12779
extern void *xdbg_malloc(size_t size, const char *file, unsigned int line,
12880
const char *function);
12981

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.
14084
extern void *xdbg_calloc(size_t number, size_t size, const char *file,
14185
unsigned int line, const char *function);
14286

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.
15389
extern void *xdbg_realloc(void *pointer, size_t size, const char *file,
15490
unsigned int line, const char *function);
15591

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.
16494
extern void xdbg_free(void *pointer, const char *file, unsigned int line,
16595
const char *function);
16696

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);
184107

185108
#ifdef __cplusplus
186109
}

0 commit comments

Comments
 (0)