1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __MEM_STATS_H__
6 #define __MEM_STATS_H__
7 
8 #include "linkkit/infra/infra_list.h"
9 #include "linkkit/infra/infra_log.h"
10 
11 #if defined(__UBUNTU_SDK_DEMO__)
12 #include <execinfo.h>
13 #endif
14 
15 typedef struct {
16     void *buf;
17     int buflen;
18     char *func;
19     int line;
20 #if defined(_PLATFORM_IS_LINUX_)
21     char **bt_symbols;
22     int bt_level;
23 #endif
24     list_head_t list;
25     void *mem_table;
26 } OS_malloc_record;
27 
28 #define MEM_MAGIC (0x1234)
29 
30 #define LITE_calloc(num, size, ...) \
31     LITE_malloc_internal(__func__, __LINE__, (num * size), ##__VA_ARGS__)
32 #define LITE_malloc(size, ...) \
33     LITE_malloc_internal(__func__, __LINE__, size, ##__VA_ARGS__)
34 #define LITE_realloc(ptr, size, ...) \
35     LITE_realloc_internal(__func__, __LINE__, ptr, size, ##__VA_ARGS__)
36 #define LITE_free(ptr)                                                       \
37     do {                                                                     \
38         if (!ptr) {                                                          \
39             log_warning("utils", "%s == NULL! LITE_free(%s) aborted.", #ptr, \
40                         #ptr);                                               \
41             break;                                                           \
42         }                                                                    \
43                                                                              \
44         LITE_free_internal((void *)ptr);                                     \
45         ptr = NULL;                                                          \
46     } while (0)
47 
48 void *LITE_malloc_internal(const char *f, const int l, int size, ...);
49 void *LITE_realloc_internal(const char *f, const int l, void *ptr, int size,
50                             ...);
51 void LITE_free_internal(void *ptr);
52 void LITE_dump_malloc_free_stats(int level);
53 void **LITE_get_mem_mutex(void);
54 
55 #endif /* __MEM_STATS_H__ */
56