feat(heap): add option to enable/disable heap trace function

This commit is contained in:
Dong Heng
2019-11-06 11:36:47 +08:00
parent fa4b8c5586
commit 975a7fd12a
6 changed files with 40 additions and 5 deletions

View File

@ -1,10 +1,16 @@
menu "Heap memory"
config HEAP_DISABLE_IRAM
bool "Disable IRAM as heap memory"
default n
help
Disable IRAM as heap memory, and heap memory only use DRAM.
config HEAP_DISABLE_IRAM
bool "Disable IRAM as heap memory"
default n
help
Disable IRAM as heap memory, and heap memory only use DRAM.
config HEAP_TRACING
bool "Enables heap tracing API"
default n
help
Enables heap tracing API.
endmenu

View File

@ -51,6 +51,7 @@ typedef struct mem_blk {
/**
* Second type memory block.
*/
#ifdef CONFIG_HEAP_TRACING
typedef struct mem_blk2 {
struct mem_blk2 *prev; ///< Point to previous memory block
struct mem_blk2 *next; ///< Point to next memory block
@ -58,6 +59,9 @@ typedef struct mem_blk2 {
const char *file; ///< Which "file" allocate the memory block
size_t line; ///< Which "line" allocate the memory block
} mem2_blk_t;
#else
typedef mem_blk_t mem2_blk_t;
#endif
/**
* User region information.

View File

@ -14,12 +14,15 @@
#pragma once
#include "sdkconfig.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef CONFIG_HEAP_TRACING
typedef enum {
HEAP_TRACE_NONE = 0,
@ -80,6 +83,8 @@ esp_err_t heap_trace_resume(void);
*/
void heap_trace_dump(void);
#endif /* CONFIG_HEAP_TRACING */
#ifdef __cplusplus
}
#endif

View File

@ -14,6 +14,7 @@
#pragma once
#include "sdkconfig.h"
#include <stdbool.h>
#ifdef __cplusplus
@ -21,7 +22,12 @@ extern "C" {
#endif
#define MEM_BLK_TAG 0x80000000 ///< Mark the memory block used
#ifdef CONFIG_HEAP_TRACING
#define MEM_BLK_TRACE 0x80000000 ///< Mark the memory block traced
#else
#define MEM_BLK_TRACE 0x00000000 ///< Mark the memory block traced
#endif
#define _mem_blk_get_ptr(_mem_blk, _offset, _mask) \
((mem_blk_t *)((((uint32_t *)(_mem_blk))[_offset]) & (~_mask)))
@ -47,12 +53,14 @@ static inline size_t mem_blk_head_size(bool trace)
static inline void mem_blk_set_traced(mem2_blk_t *mem_blk, const char *file, size_t line)
{
#ifdef CONFIG_HEAP_TRACING
uint32_t *val = (uint32_t *)mem_blk + 1;
*val |= MEM_BLK_TRACE;
mem_blk->file = file;
mem_blk->line = line | MEM_BLK_TRACE;
#endif
}
static inline void mem_blk_set_untraced(mem2_blk_t *mem_blk)
@ -154,10 +162,12 @@ static inline size_t ptr_size(void *ptr)
return size;
}
#ifdef CONFIG_HEAP_TRACING
static inline size_t mem2_blk_line(mem2_blk_t *mem2_blk)
{
return mem2_blk->line & ~MEM_BLK_TRACE;
}
#endif
#ifdef __cplusplus
}

View File

@ -32,7 +32,9 @@ extern heap_region_t g_heap_region[];
static const char *TAG = "heap_init";
size_t g_heap_region_num;
#ifdef CONFIG_HEAP_TRACING
int __g_heap_trace_mode = HEAP_TRACE_NONE;
#endif
/**
* @brief Initialize regions of memory to the collection of heaps at runtime.
@ -116,7 +118,9 @@ void IRAM_ATTR *_heap_caps_malloc(size_t size, uint32_t caps, const char *file,
_heap_caps_lock(num);
#ifdef CONFIG_HEAP_TRACING
trace = __g_heap_trace_mode == HEAP_TRACE_LEAKS;
#endif
mem_blk_size = ptr2memblk_size(size, trace);

View File

@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_HEAP_TRACING
#include <string.h>
#include "esp_heap_caps.h"
@ -146,3 +150,5 @@ void heap_trace_dump(void)
_heap_caps_unlock(num);
}
}
#endif /* CONFIG_HEAP_TRACING */