1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <soc/soc_memory_layout.h>
19 #include "multi_heap.h"
20 #include "multi_heap_platform.h"
21 #include "sys/queue.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /* Some common heap registration data structures used
28    for heap_caps_init.c to share heap information with heap_caps.c
29 */
30 
31 #define HEAP_SIZE_MAX (SOC_MAX_CONTIGUOUS_RAM_SIZE)
32 
33 /* Type for describing each registered heap */
34 typedef struct heap_t_ {
35     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.
36     intptr_t start;
37     intptr_t end;
38     multi_heap_lock_t heap_mux;
39     multi_heap_handle_t heap;
40     SLIST_ENTRY(heap_t_) next;
41 } heap_t;
42 
43 /* All registered heaps.
44 
45    Forms a single linked list, even though most entries are contiguous.
46    This means at the expense of 4 bytes per heap, new heaps can be
47    added at runtime in a fast & thread-safe way.
48 */
49 extern SLIST_HEAD(registered_heap_ll, heap_t_) registered_heaps;
50 
51 bool heap_caps_match(const heap_t *heap, uint32_t caps);
52 
53 /* return all possible capabilities (across all priorities) for a given heap */
get_all_caps(const heap_t * heap)54 inline static IRAM_ATTR uint32_t get_all_caps(const heap_t *heap)
55 {
56     if (heap->heap == NULL) {
57         return 0;
58     }
59     uint32_t all_caps = 0;
60     for (int prio = 0; prio < SOC_MEMORY_TYPE_NO_PRIOS; prio++) {
61         all_caps |= heap->caps[prio];
62     }
63     return all_caps;
64 }
65 
66 /*
67  Because we don't want to add _another_ known allocation method to the stack of functions to trace wrt memory tracing,
68  these are declared private. The newlib malloc()/realloc() implementation also calls these, so they are declared
69  separately in newlib/syscalls.c.
70 */
71 void *heap_caps_realloc_default(void *p, size_t size);
72 void *heap_caps_malloc_default(size_t size);
73 
74 
75 #ifdef __cplusplus
76 }
77 #endif
78