-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug_memory.h
46 lines (37 loc) · 1.4 KB
/
debug_memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef H_DEBUG_MEMORY_H
#define H_DEBUG_MEMORY_H
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define DEBUG_MEMORY_DOTTED_VERSION "0.0.1"
#define DEBUG_MEMORY_VERSION 00001
#define DEBUG_MEMORY_VERSION_STRING "00001"
/* Total bytes allocated */
int total_allocated;
/* Memory alignment is important */
typedef union { double d; struct {size_t n; char *file; int line;} s; } Site;
/* linked list */
typedef struct debug_node {Site* site; struct debug_node* next; } debug_node_t;
debug_node_t* debug_root_node;
/* wrapper for malloc, free */
void* debug_malloc(size_t, char*, int);
void* debug_calloc(size_t, size_t, char*, int);
void* debug_realloc(void*, size_t, char*, int);
void debug_free(void*, char*, int);
char* debug_strdup(const char*, char*, int);
char* debug_strndup(const char*, size_t, char*, int);
/* util */
debug_node_t* create_node(Site*);
void add_node(Site*);
void remove_node(Site*);
void print_debug_node(debug_node_t*);
void print_allocated_memory(void);
/* debug_memory.c will not read this to avoid recursive expansion */
#ifndef C_DEBUG_MEMORY_C
#define malloc(n) debug_malloc(n, __FILE__, __LINE__)
#define calloc(c, n) debug_calloc(c, n, __FILE__, __LINE__)
#define realloc(p,n) debug_realloc(p, n, __FILE__, __LINE__)
#define free(p) debug_free(p, __FILE__, __LINE__)
#define strdup(p) debug_strdup(p, __FILE__, __LINE__)
#endif /* C_DEBUG_MEMORY_C */
#endif /* H_DEBUG_MEMORY_H */