-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmem_debug_ex.h
More file actions
87 lines (73 loc) · 2.68 KB
/
Copy pathmem_debug_ex.h
File metadata and controls
87 lines (73 loc) · 2.68 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef __MEM_DEBUG_H__
#define __MEM_DEBUG_H__
//#define CONFIG_MEMORY_DEBUG 1 // Simple mem error check
//#define CONFIG_MEMORY_DEBUG 2 // Detail mem error check
#define CONFIG_MEMORY_DEBUG 3 // Detail mem error check in multitask environment
#define PLATFORM_SYS_RT_THREAD 1
#if defined(PLATFORM_SYS_ABSTRACT_LAYER)
#include "sys_al/sys_al_ex.h"
#define PLATFORM_MALLOC(x) SYS_Malloc(x)
#define PLATFORM_FREE(x) SYS_Free(x)
#define PLATFORM_REALLOC(x, y) LIBC_Realloc(x, y)
#define PLATFORM_CALLOC(x, y) LIBC_Calloc(x, y)
#define PLATFORM_STRDUP(x) LIBC_Strdup(x)
#else
#if defined(PLATFORM_SYS_RT_THREAD)
#include <rtthread.h>
#define PLATFORM_MALLOC(x) rt_malloc(x)
#define PLATFORM_FREE(x) rt_free(x)
#define PLATFORM_REALLOC(x, y) rt_realloc(x, y)
#define PLATFORM_CALLOC(x, y) rt_calloc(x, y)
#define PLATFORM_STRDUP(x) rt_strdup(x)
#else
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define PLATFORM_MALLOC(x) malloc(x)
#define PLATFORM_FREE(x) free(x)
#define PLATFORM_REALLOC(x, y) realloc(x, y)
#define PLATFORM_CALLOC(x, y) calloc(x, y)
#define PLATFORM_STRDUP(x) strdup(x)
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
// Here we write our own secure malloc that will be monitoring memory heap
#if (CONFIG_MEMORY_DEBUG > 0)
#define MALLOC(nbytes) \
MEMD_Alloc((nbytes), __FILE__, __LINE__)
#define NEW(p) ((p) = MALLOC((long)sizeof *(p)))
#define FREE(ptr) ((void)(MEMD_Free((ptr), \
__FILE__, __LINE__), (ptr) = 0))
#define CALLOC(count, nbytes) \
MEMD_Calloc((count), (nbytes), __FILE__, __LINE__)
#define NEW0(p) ((p) = CALLOC(1, (long)sizeof *(p)))
#define REALLOC(ptr, nbytes) MEMD_Realloc((ptr), \
(nbytes), __FILE__, __LINE__)
#define STRDUP(str) \
MEMD_Strdup((str), __FILE__, __LINE__)
#define MEMDEBUG_INIT() MEMD_Init()
#define MEMDEBUG_CHECK() MEMD_Check()
void* MEMD_Alloc(long nbytes, const char *file, int line);
void MEMD_Free(void *ptr, const char *file, int line);
void* MEMD_Calloc(long count, long nbytes, const char *file, int line);
void* MEMD_Realloc(void *ptr, long nbytes, const char *file, int line);
void* MEMD_Strdup(const char *str, const char *file, int line);
void MEMD_Init(void);
void MEMD_Check( void );
#else
#define MALLOC(x) PLATFORM_MALLOC(x)
#define NEW(p) ((p) = PLATFORM_MALLOC(sizeof *(p)))
#define FREE(x) PLATFORM_FREE(x)
#define CALLOC(x, y) PLATFORM_CALLOC(x, y)
#define NEW0(p) ((p) = PLATFORM_CALLOC(1, sizeof *(p)))
#define REALLOC(x, y) PLATFORM_REALLOC(x, y)
#define STRDUP(x) PLATFORM_STRDUP(x)
#define MEMDEBUG_INIT() ((void)0)
#define MEMDEBUG_CHECK() ((void)0)
#endif // CONFIG_MEMORY_DEBUG
#ifdef __cplusplus
}
#endif
#endif // __MEM_DEBUG_H__