[Heap Trace Standalone] do not allocate memory until init is called

This commit is contained in:
Chip Weinberger
2023-05-25 22:05:07 -07:00
parent d00e7b5af8
commit fc1e8b0365
3 changed files with 33 additions and 26 deletions

View File

@@ -6,6 +6,7 @@
#include <string.h>
#include <sdkconfig.h>
#include <inttypes.h>
#include "esp_log.h"
#define HEAP_TRACE_SRCFILE /* don't warn on inclusion here */
#include "esp_heap_trace.h"
@@ -17,6 +18,8 @@
#include "esp_memory_utils.h"
#include "sys/queue.h"
static const char* TAG = "heaptrace";
#define STACK_DEPTH CONFIG_HEAP_TRACING_STACK_DEPTH
#if CONFIG_HEAP_TRACING_STANDALONE
@@ -80,15 +83,10 @@ static size_t r_get_idx;
#if CONFIG_HEAP_TRACE_HASH_MAP
/* Define struct: linked list of records used in hash map */
TAILQ_HEAD(heap_trace_hash_list_struct_t, heap_trace_record_t);
typedef struct heap_trace_hash_list_struct_t heap_trace_hash_list_t;
static
#if CONFIG_HEAP_TRACE_HASH_MAP_IN_EXT_RAM
EXT_RAM_BSS_ATTR
#endif
heap_trace_hash_list_t hash_map[(size_t)CONFIG_HEAP_TRACE_HASH_MAP_SIZE]; // Buffer used for hashmap entries
// We use a hash_map to make locating a record by memory address very fast.
// Key: addr // the memory address returned by malloc, calloc, realloc
// Value: hash_map[hash(key)] // a list of records ptrs, which contains the relevant record.
static heap_trace_record_list_t* hash_map; // array of lists
static size_t total_hashmap_hits;
static size_t total_hashmap_miss;
@@ -140,6 +138,19 @@ esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t
return ESP_ERR_INVALID_ARG;
}
#if CONFIG_HEAP_TRACE_HASH_MAP
if (hash_map == NULL) {
uint32_t map_size = sizeof(heap_trace_record_list_t) * CONFIG_HEAP_TRACE_HASH_MAP_SIZE;
#if CONFIG_HEAP_TRACE_HASH_MAP_IN_EXT_RAM
ESP_LOGI(TAG, "hashmap: allocating %" PRIu32 " bytes (PSRAM)\n", map_size);
hash_map = heap_caps_calloc(1, map_size, MALLOC_CAP_SPIRAM);
#else
ESP_LOGI(TAG, "hashmap: allocating %" PRIu32 " bytes (Internal RAM)\n", map_size);
hash_map = heap_caps_calloc(1, map_size, MALLOC_CAP_INTERNAL);
#endif // CONFIG_HEAP_TRACE_HASH_MAP_IN_EXT_RAM
}
#endif // CONFIG_HEAP_TRACE_HASH_MAP
records.buffer = record_buffer;
records.capacity = num_records;