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