1 // Copyright 2020 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 <stdint.h>
17 #include <stdlib.h>
18 #include <sdkconfig.h>
19 #include "esp_err.h"
20 
21 /**
22  * @brief Flags to indicate the capabilities of the various memory systems
23  */
24 #define MALLOC_CAP_EXEC             (1<<0)  ///< Memory must be able to run executable code
25 #define MALLOC_CAP_32BIT            (1<<1)  ///< Memory must allow for aligned 32-bit data accesses
26 #define MALLOC_CAP_8BIT             (1<<2)  ///< Memory must allow for 8/16/...-bit data accesses
27 #define MALLOC_CAP_DMA              (1<<3)  ///< Memory must be able to accessed by DMA
28 #define MALLOC_CAP_PID2             (1<<4)  ///< Memory must be mapped to PID2 memory space (PIDs are not currently used)
29 #define MALLOC_CAP_PID3             (1<<5)  ///< Memory must be mapped to PID3 memory space (PIDs are not currently used)
30 #define MALLOC_CAP_PID4             (1<<6)  ///< Memory must be mapped to PID4 memory space (PIDs are not currently used)
31 #define MALLOC_CAP_PID5             (1<<7)  ///< Memory must be mapped to PID5 memory space (PIDs are not currently used)
32 #define MALLOC_CAP_PID6             (1<<8)  ///< Memory must be mapped to PID6 memory space (PIDs are not currently used)
33 #define MALLOC_CAP_PID7             (1<<9)  ///< Memory must be mapped to PID7 memory space (PIDs are not currently used)
34 #define MALLOC_CAP_SPIRAM           (1<<10) ///< Memory must be in SPI RAM
35 #define MALLOC_CAP_INTERNAL         (1<<11) ///< Memory must be internal; specifically it should not disappear when flash/spiram cache is switched off
36 #define MALLOC_CAP_DEFAULT          (1<<12) ///< Memory can be returned in a non-capability-specific memory allocation (e.g. malloc(), calloc()) call
37 #define MALLOC_CAP_IRAM_8BIT        (1<<13) ///< Memory must be in IRAM and allow unaligned access
38 
39 #define MALLOC_CAP_INVALID          (1<<31) ///< Memory can't be used / list end marker
40 
41 /**
42  * @brief Allocate a chunk of memory which has the given capabilities
43  *
44  * Equivalent semantics to libc malloc(), for capability-aware memory.
45  *
46  * In IDF, ``malloc(p)`` is equivalent to ``heap_caps_malloc(p, MALLOC_CAP_8BIT)``.
47  *
48  * @param size Size, in bytes, of the amount of memory to allocate
49  * @param caps        Bitwise OR of MALLOC_CAP_* flags indicating the type
50  *                    of memory to be returned
51  *
52  * @return A pointer to the memory allocated on success, NULL on failure
53  */
54 void *heap_caps_malloc(size_t size, uint32_t caps);
55