1 /*
2  * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #pragma once
7 
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <soc/soc_memory_layout.h>
11 #include "multi_heap.h"
12 #include "multi_heap_platform.h"
13 #include "sys/queue.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /* Some common heap registration data structures used
20    for heap_caps_init.c to share heap information with heap_caps.c
21 */
22 
23 #define HEAP_SIZE_MAX (SOC_MAX_CONTIGUOUS_RAM_SIZE)
24 
25 /* Type for describing each registered heap */
26 typedef struct heap_t_ {
27     uint32_t caps[SOC_MEMORY_TYPE_NO_PRIOS]; ///< Capabilities for the type of memory in this heap (as a prioritised set). Copied from soc_memory_types so it's in RAM not flash.
28     intptr_t start;
29     intptr_t end;
30     multi_heap_lock_t heap_mux;
31     multi_heap_handle_t heap;
32     SLIST_ENTRY(heap_t_) next;
33 } heap_t;
34 
35 /* All registered heaps.
36 
37    Forms a single linked list, even though most entries are contiguous.
38    This means at the expense of 4 bytes per heap, new heaps can be
39    added at runtime in a fast & thread-safe way.
40 */
41 extern SLIST_HEAD(registered_heap_ll, heap_t_) registered_heaps;
42 
43 bool heap_caps_match(const heap_t *heap, uint32_t caps);
44 
45 /* return all possible capabilities (across all priorities) for a given heap */
get_all_caps(const heap_t * heap)46 inline static uint32_t get_all_caps(const heap_t *heap)
47 {
48     if (heap->heap == NULL) {
49         return 0;
50     }
51     uint32_t all_caps = 0;
52     for (int prio = 0; prio < SOC_MEMORY_TYPE_NO_PRIOS; prio++) {
53         all_caps |= heap->caps[prio];
54     }
55     return all_caps;
56 }
57 
58 /*
59  Because we don't want to add _another_ known allocation method to the stack of functions to trace wrt memory tracing,
60  these are declared private. The newlib malloc()/realloc() implementation also calls these, so they are declared
61  separately in newlib/syscalls.c.
62 */
63 void *heap_caps_realloc_default(void *p, size_t size);
64 void *heap_caps_malloc_default(size_t size);
65 
66 
67 #ifdef __cplusplus
68 }
69 #endif
70