NetBurner 3.1
debugalloc.h
1 /*NB_REVISION*/
2 
3 /*NB_COPYRIGHT*/
4 
5 #ifndef _DEBUG_ALLOC_H_
6 #define _DEBUG_ALLOC_H_
7 
8 /*
9  ******************************************************************************
10  *
11  * Definitions
12  *
13  ******************************************************************************
14  */
15 /*
16  * Memory Allocation Debugging
17  * Should be defined if needed, unnecessary burden to release code
18  *
19  */
20 /* #define NB_DEBUG_ALLOC_SUPPORTED ( 1 ) */
21 
22 /*
23  * Memory Allocation Features
24  *
25  */
26 /* All activities are displayed with iprintf */
27 /* #define NB_DEBUG_ALLOC_VERBOSE ( 1 ) */
28 
29 /* Log all activities as well */
30 /* #define NB_DEBUG_ALLOC_LOG_ALL ( 1 ) */
31 
32 /*
33  * Entry log size
34  * Size of allocLogEntry per entry
35  * Additional two for ...LOG_ALL
36  * Overflow is reported but the process continues
37  */
38 #define NB_DEBUG_ALLOC_LOG_SIZE (2048)
39 
40 /* Guardian size in bytes before and after returned allocated memory */
41 #define NB_DEBUG_ALLOC_GUARD_SIZE (64)
42 
43 /* Guardian value filled and checked */
44 #define NB_DEBUG_ALLOC_GUARD_VALUE (0xA5)
45 
46 #ifdef __cplusplus
47 extern "C"
48 {
49 #endif
50  /*
51  ******************************************************************************
52  *
53  * Debug malloc, calloc, realloc and free "C" Library Interface
54  *
55  ******************************************************************************
56  */
57 
58  /*
59  ******************************************************************************
60 
61  Debug logger and optionally guardians:
62  malloc
63  calloc
64  realloc
65  free
66 
67  Parameters:
68  byteCount - Memory needed in bytes
69  elementCount - Elements of byteCount bytes (calloc)
70  ptr - Previously allocated memory
71  caller - Function calling, best choice __FUNCTION__
72  line - Line number of call, best choice __LINE__
73 
74  Return:
75  > 0 Address of allocated memory, 0 problems
76 
77  Notes:
78  calloc zeros memory allocated
79  realloc can extend or truncate memory, contents are the same as ptr
80  realloc if problems does not change or deallocate memory
81  Will not free a pointer not logged
82 
83  ******************************************************************************
84  */
85  void *mallocDebug(size_t byteCount, const char *caller, int line);
86  void *callocDebug(size_t elementCount, size_t byteCount, const char *caller, int line);
87  void *reallocDebug(void *ptr, size_t byteCount, const char *caller, int line);
88  void freeDebug(void *ptr, const char *caller, int line);
89 
90  /*
91  ******************************************************************************
92 
93  Display using iprintf log and all allocated log
94 
95  Parameters:
96  None
97 
98  Return:
99  None
100 
101  Notes:
102  None
103 
104  ******************************************************************************
105  */
106  void printAllocDebugLog(void);
107  void printAllocDebugLogAll(void);
108 
109 /*
110  ******************************************************************************
111  *
112  * Definitions
113  *
114  ******************************************************************************
115  */
116 #ifdef NB_DEBUG_ALLOC_SUPPORTED
117 #define NBMALLOC(bYtEcOuNt) mallocDebug(bYtEcOuNt, __FUNCTION__, __LINE__);
118 #define NBCALLOC(eLeMeNtCoUnT, bYtEcOuNt) callocDebug(eLeMeNtCoUnT, bYtEcOuNt, __FUNCTION__, __LINE__);
119 #define NBREALLOC(pTr, bYtEcOuNt) reallocDebug(pTr, bYtEcOuNt, __FUNCTION__, __LINE__);
120 #define NBFREE(pTr) freeDebug(pTr, __FUNCTION__, __LINE__);
121 #else /* #ifdef NB_DEBUG_ALLOC_SUPPORTED */
122 #define NBMALLOC(bYtEcOuNt) malloc(bYtEcOuNt);
123 #define NBCALLOC(eLeMeNtCoUnT, bYtEcOuNt) calloc(eLeMeNtCoUnT, bYtEcOuNt);
124 #define NBREALLOC(pTr, bYtEcOuNt) realloc(pTr, bYtEcOuNt);
125 #define NBFREE(pTr) free(pTr);
126 #endif /* #ifdef NB_DEBUG_ALLOC_SUPPORTED */
127 
128 #ifdef __cplusplus
129 };
130 #endif
131 #endif /* #ifndef _DEBUG_ALLOC_H_ */