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 #include <stdint.h>
16 #include <stdlib.h>
17 #include <stdbool.h>
18 
19 /* multi_heap is a heap implementation for handling multiple
20    heterogenous heaps in a single program.
21 
22    Any contiguous block of memory can be registered as a heap.
23 */
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /** @brief Opaque handle to a registered heap */
30 typedef struct multi_heap_info *multi_heap_handle_t;
31 
32 /** @brief malloc() a buffer in a given heap
33  *
34  * Semantics are the same as standard malloc(), only the returned buffer will be allocated in the specified heap.
35  *
36  * @param heap Handle to a registered heap.
37  * @param size Size of desired buffer.
38  *
39  * @return Pointer to new memory, or NULL if allocation fails.
40  */
41 void *multi_heap_malloc(multi_heap_handle_t heap, size_t size);
42 
43 /** @brief free() a buffer in a given heap.
44  *
45  * Semantics are the same as standard free(), only the argument 'p' must be NULL or have been allocated in the specified heap.
46  *
47  * @param heap Handle to a registered heap.
48  * @param p NULL, or a pointer previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
49  */
50 void multi_heap_free(multi_heap_handle_t heap, void *p);
51 
52 /** @brief realloc() a buffer in a given heap.
53  *
54  * Semantics are the same as standard realloc(), only the argument 'p' must be NULL or have been allocated in the specified heap.
55  *
56  * @param heap Handle to a registered heap.
57  * @param p NULL, or a pointer previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
58  * @param size Desired new size for buffer.
59  *
60  * @return New buffer of 'size' containing contents of 'p', or NULL if reallocation failed.
61  */
62 void *multi_heap_realloc(multi_heap_handle_t heap, void *p, size_t size);
63 
64 
65 /** @brief Return the size that a particular pointer was allocated with.
66  *
67  * @param heap Handle to a registered heap.
68  * @param p Pointer, must have been previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
69  *
70  * @return Size of the memory allocated at this block. May be more than the original size argument, due
71  * to padding and minimum block sizes.
72  */
73 size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p);
74 
75 
76 /** @brief Register a new heap for use
77  *
78  * This function initialises a heap at the specified address, and returns a handle for future heap operations.
79  *
80  * There is no equivalent function for deregistering a heap - if all blocks in the heap are free, you can immediately start using the memory for other purposes.
81  *
82  * @param start Start address of the memory to use for a new heap.
83  * @param size Size (in bytes) of the new heap.
84  *
85  * @return Handle of a new heap ready for use, or NULL if the heap region was too small to be initialised.
86  */
87 multi_heap_handle_t multi_heap_register(void *start, size_t size);
88 
89 
90 /** @brief Associate a private lock pointer with a heap
91  *
92  * The lock argument is supplied to the MULTI_HEAP_LOCK() and MULTI_HEAP_UNLOCK() macros, defined in multi_heap_platform.h.
93  *
94  * The lock in question must be recursive.
95  *
96  * When the heap is first registered, the associated lock is NULL.
97  *
98  * @param heap Handle to a registered heap.
99  * @param lock Optional pointer to a locking structure to associate with this heap.
100  */
101 void multi_heap_set_lock(multi_heap_handle_t heap, void* lock);
102 
103 /** @brief Dump heap information to stdout
104  *
105  * For debugging purposes, this function dumps information about every block in the heap to stdout.
106  *
107  * @param heap Handle to a registered heap.
108  */
109 void multi_heap_dump(multi_heap_handle_t heap);
110 
111 /** @brief Check heap integrity
112  *
113  * Walks the heap and checks all heap data structures are valid. If any errors are detected, an error-specific message
114  * can be optionally printed to stderr. Print behaviour can be overriden at compile time by defining
115  * MULTI_CHECK_FAIL_PRINTF in multi_heap_platform.h.
116  *
117  * @param heap Handle to a registered heap.
118  * @param print_errors If true, errors will be printed to stderr.
119  * @return true if heap is valid, false otherwise.
120  */
121 bool multi_heap_check(multi_heap_handle_t heap, bool print_errors);
122 
123 /** @brief Return free heap size
124  *
125  * Returns the number of bytes available in the heap.
126  *
127  * Equivalent to the total_free_bytes member returned by multi_heap_get_heap_info().
128  *
129  * Note that the heap may be fragmented, so the actual maximum size for a single malloc() may be lower. To know this
130  * size, see the largest_free_block member returned by multi_heap_get_heap_info().
131  *
132  * @param heap Handle to a registered heap.
133  * @return Number of free bytes.
134  */
135 size_t multi_heap_free_size(multi_heap_handle_t heap);
136 
137 /** @brief Return the lifetime minimum free heap size
138  *
139  * Equivalent to the minimum_free_bytes member returned by multi_heap_get_info().
140  *
141  * Returns the lifetime "low water mark" of possible values returned from multi_free_heap_size(), for the specified
142  * heap.
143  *
144  * @param heap Handle to a registered heap.
145  * @return Number of free bytes.
146  */
147 size_t multi_heap_minimum_free_size(multi_heap_handle_t heap);
148 
149 /** @brief Structure to access heap metadata via multi_heap_get_info */
150 typedef struct {
151     size_t total_free_bytes;      ///<  Total free bytes in the heap. Equivalent to multi_free_heap_size().
152     size_t total_allocated_bytes; ///<  Total bytes allocated to data in the heap.
153     size_t largest_free_block;    ///<  Size of largest free block in the heap. This is the largest malloc-able size.
154     size_t minimum_free_bytes;    ///<  Lifetime minimum free heap size. Equivalent to multi_minimum_free_heap_size().
155     size_t allocated_blocks;      ///<  Number of (variable size) blocks allocated in the heap.
156     size_t free_blocks;           ///<  Number of (variable size) free blocks in the heap.
157     size_t total_blocks;          ///<  Total number of (variable size) blocks in the heap.
158     size_t total_bytes;
159 } multi_heap_info_t;
160 
161 /** @brief Return metadata about a given heap
162  *
163  * Fills a multi_heap_info_t structure with information about the specified heap.
164  *
165  * @param heap Handle to a registered heap.
166  * @param info Pointer to a structure to fill with heap metadata.
167  */
168 void multi_heap_get_info(multi_heap_handle_t heap, multi_heap_info_t *info);
169 
170 #ifdef __cplusplus
171 }
172 #endif
173