mirror of
https://github.com/0xFEEDC0DE64/arduino-esp32.git
synced 2025-07-01 13:00:59 +02:00
Initial Esp32c3 Support (#5060)
This commit is contained in:
402
tools/sdk/esp32c3/include/heap/include/esp_heap_caps.h
Normal file
402
tools/sdk/esp32c3/include/heap/include/esp_heap_caps.h
Normal file
@ -0,0 +1,402 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "multi_heap.h"
|
||||
#include <sdkconfig.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Flags to indicate the capabilities of the various memory systems
|
||||
*/
|
||||
#define MALLOC_CAP_EXEC (1<<0) ///< Memory must be able to run executable code
|
||||
#define MALLOC_CAP_32BIT (1<<1) ///< Memory must allow for aligned 32-bit data accesses
|
||||
#define MALLOC_CAP_8BIT (1<<2) ///< Memory must allow for 8/16/...-bit data accesses
|
||||
#define MALLOC_CAP_DMA (1<<3) ///< Memory must be able to accessed by DMA
|
||||
#define MALLOC_CAP_PID2 (1<<4) ///< Memory must be mapped to PID2 memory space (PIDs are not currently used)
|
||||
#define MALLOC_CAP_PID3 (1<<5) ///< Memory must be mapped to PID3 memory space (PIDs are not currently used)
|
||||
#define MALLOC_CAP_PID4 (1<<6) ///< Memory must be mapped to PID4 memory space (PIDs are not currently used)
|
||||
#define MALLOC_CAP_PID5 (1<<7) ///< Memory must be mapped to PID5 memory space (PIDs are not currently used)
|
||||
#define MALLOC_CAP_PID6 (1<<8) ///< Memory must be mapped to PID6 memory space (PIDs are not currently used)
|
||||
#define MALLOC_CAP_PID7 (1<<9) ///< Memory must be mapped to PID7 memory space (PIDs are not currently used)
|
||||
#define MALLOC_CAP_SPIRAM (1<<10) ///< Memory must be in SPI RAM
|
||||
#define MALLOC_CAP_INTERNAL (1<<11) ///< Memory must be internal; specifically it should not disappear when flash/spiram cache is switched off
|
||||
#define MALLOC_CAP_DEFAULT (1<<12) ///< Memory can be returned in a non-capability-specific memory allocation (e.g. malloc(), calloc()) call
|
||||
#define MALLOC_CAP_IRAM_8BIT (1<<13) ///< Memory must be in IRAM and allow unaligned access
|
||||
#define MALLOC_CAP_RETENTION (1<<14)
|
||||
|
||||
#define MALLOC_CAP_INVALID (1<<31) ///< Memory can't be used / list end marker
|
||||
|
||||
/**
|
||||
* @brief callback called when a allocation operation fails, if registered
|
||||
* @param size in bytes of failed allocation
|
||||
* @param caps capabillites requested of failed allocation
|
||||
* @param function_name function which generated the failure
|
||||
*/
|
||||
typedef void (*esp_alloc_failed_hook_t) (size_t size, uint32_t caps, const char * function_name);
|
||||
|
||||
/**
|
||||
* @brief registers a callback function to be invoked if a memory allocation operation fails
|
||||
* @param callback caller defined callback to be invoked
|
||||
* @return ESP_OK if callback was registered.
|
||||
*/
|
||||
esp_err_t heap_caps_register_failed_alloc_callback(esp_alloc_failed_hook_t callback);
|
||||
|
||||
/**
|
||||
* @brief Allocate a chunk of memory which has the given capabilities
|
||||
*
|
||||
* Equivalent semantics to libc malloc(), for capability-aware memory.
|
||||
*
|
||||
* In IDF, ``malloc(p)`` is equivalent to ``heap_caps_malloc(p, MALLOC_CAP_8BIT)``.
|
||||
*
|
||||
* @param size Size, in bytes, of the amount of memory to allocate
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory to be returned
|
||||
*
|
||||
* @return A pointer to the memory allocated on success, NULL on failure
|
||||
*/
|
||||
void *heap_caps_malloc(size_t size, uint32_t caps);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Free memory previously allocated via heap_caps_malloc() or heap_caps_realloc().
|
||||
*
|
||||
* Equivalent semantics to libc free(), for capability-aware memory.
|
||||
*
|
||||
* In IDF, ``free(p)`` is equivalent to ``heap_caps_free(p)``.
|
||||
*
|
||||
* @param ptr Pointer to memory previously returned from heap_caps_malloc() or heap_caps_realloc(). Can be NULL.
|
||||
*/
|
||||
void heap_caps_free( void *ptr);
|
||||
|
||||
/**
|
||||
* @brief Reallocate memory previously allocated via heap_caps_malloc() or heap_caps_realloc().
|
||||
*
|
||||
* Equivalent semantics to libc realloc(), for capability-aware memory.
|
||||
*
|
||||
* In IDF, ``realloc(p, s)`` is equivalent to ``heap_caps_realloc(p, s, MALLOC_CAP_8BIT)``.
|
||||
*
|
||||
* 'caps' parameter can be different to the capabilities that any original 'ptr' was allocated with. In this way,
|
||||
* realloc can be used to "move" a buffer if necessary to ensure it meets a new set of capabilities.
|
||||
*
|
||||
* @param ptr Pointer to previously allocated memory, or NULL for a new allocation.
|
||||
* @param size Size of the new buffer requested, or 0 to free the buffer.
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory desired for the new allocation.
|
||||
*
|
||||
* @return Pointer to a new buffer of size 'size' with capabilities 'caps', or NULL if allocation failed.
|
||||
*/
|
||||
void *heap_caps_realloc( void *ptr, size_t size, uint32_t caps);
|
||||
|
||||
/**
|
||||
* @brief Allocate a aligned chunk of memory which has the given capabilities
|
||||
*
|
||||
* Equivalent semantics to libc aligned_alloc(), for capability-aware memory.
|
||||
* @param alignment How the pointer received needs to be aligned
|
||||
* must be a power of two
|
||||
* @param size Size, in bytes, of the amount of memory to allocate
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory to be returned
|
||||
*
|
||||
* @return A pointer to the memory allocated on success, NULL on failure
|
||||
*
|
||||
*
|
||||
*/
|
||||
void *heap_caps_aligned_alloc(size_t alignment, size_t size, uint32_t caps);
|
||||
|
||||
/**
|
||||
* @brief Used to deallocate memory previously allocated with heap_caps_aligned_alloc
|
||||
*
|
||||
* @param ptr Pointer to the memory allocated
|
||||
* @note This function is deprecated, plase consider using heap_caps_free() instead
|
||||
*/
|
||||
void __attribute__((deprecated)) heap_caps_aligned_free(void *ptr);
|
||||
|
||||
/**
|
||||
* @brief Allocate a aligned chunk of memory which has the given capabilities. The initialized value in the memory is set to zero.
|
||||
*
|
||||
* @param alignment How the pointer received needs to be aligned
|
||||
* must be a power of two
|
||||
* @param n Number of continuing chunks of memory to allocate
|
||||
* @param size Size, in bytes, of a chunk of memory to allocate
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory to be returned
|
||||
*
|
||||
* @return A pointer to the memory allocated on success, NULL on failure
|
||||
*
|
||||
*/
|
||||
void *heap_caps_aligned_calloc(size_t alignment, size_t n, size_t size, uint32_t caps);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Allocate a chunk of memory which has the given capabilities. The initialized value in the memory is set to zero.
|
||||
*
|
||||
* Equivalent semantics to libc calloc(), for capability-aware memory.
|
||||
*
|
||||
* In IDF, ``calloc(p)`` is equivalent to ``heap_caps_calloc(p, MALLOC_CAP_8BIT)``.
|
||||
*
|
||||
* @param n Number of continuing chunks of memory to allocate
|
||||
* @param size Size, in bytes, of a chunk of memory to allocate
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory to be returned
|
||||
*
|
||||
* @return A pointer to the memory allocated on success, NULL on failure
|
||||
*/
|
||||
void *heap_caps_calloc(size_t n, size_t size, uint32_t caps);
|
||||
|
||||
/**
|
||||
* @brief Get the total size of all the regions that have the given capabilities
|
||||
*
|
||||
* This function takes all regions capable of having the given capabilities allocated in them
|
||||
* and adds up the total space they have.
|
||||
*
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
*
|
||||
* @return total size in bytes
|
||||
*/
|
||||
|
||||
size_t heap_caps_get_total_size(uint32_t caps);
|
||||
|
||||
/**
|
||||
* @brief Get the total free size of all the regions that have the given capabilities
|
||||
*
|
||||
* This function takes all regions capable of having the given capabilities allocated in them
|
||||
* and adds up the free space they have.
|
||||
*
|
||||
* Note that because of heap fragmentation it is probably not possible to allocate a single block of memory
|
||||
* of this size. Use heap_caps_get_largest_free_block() for this purpose.
|
||||
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
*
|
||||
* @return Amount of free bytes in the regions
|
||||
*/
|
||||
size_t heap_caps_get_free_size( uint32_t caps );
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get the total minimum free memory of all regions with the given capabilities
|
||||
*
|
||||
* This adds all the low water marks of the regions capable of delivering the memory
|
||||
* with the given capabilities.
|
||||
*
|
||||
* Note the result may be less than the global all-time minimum available heap of this kind, as "low water marks" are
|
||||
* tracked per-region. Individual regions' heaps may have reached their "low water marks" at different points in time. However
|
||||
* this result still gives a "worst case" indication for all-time minimum free heap.
|
||||
*
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
*
|
||||
* @return Amount of free bytes in the regions
|
||||
*/
|
||||
size_t heap_caps_get_minimum_free_size( uint32_t caps );
|
||||
|
||||
/**
|
||||
* @brief Get the largest free block of memory able to be allocated with the given capabilities.
|
||||
*
|
||||
* Returns the largest value of ``s`` for which ``heap_caps_malloc(s, caps)`` will succeed.
|
||||
*
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
*
|
||||
* @return Size of largest free block in bytes.
|
||||
*/
|
||||
size_t heap_caps_get_largest_free_block( uint32_t caps );
|
||||
|
||||
|
||||
/**
|
||||
* @brief Get heap info for all regions with the given capabilities.
|
||||
*
|
||||
* Calls multi_heap_info() on all heaps which share the given capabilities. The information returned is an aggregate
|
||||
* across all matching heaps. The meanings of fields are the same as defined for multi_heap_info_t, except that
|
||||
* ``minimum_free_bytes`` has the same caveats described in heap_caps_get_minimum_free_size().
|
||||
*
|
||||
* @param info Pointer to a structure which will be filled with relevant
|
||||
* heap metadata.
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
*
|
||||
*/
|
||||
void heap_caps_get_info( multi_heap_info_t *info, uint32_t caps );
|
||||
|
||||
|
||||
/**
|
||||
* @brief Print a summary of all memory with the given capabilities.
|
||||
*
|
||||
* Calls multi_heap_info on all heaps which share the given capabilities, and
|
||||
* prints a two-line summary for each, then a total summary.
|
||||
*
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
*
|
||||
*/
|
||||
void heap_caps_print_heap_info( uint32_t caps );
|
||||
|
||||
/**
|
||||
* @brief Check integrity of all heap memory in the system.
|
||||
*
|
||||
* Calls multi_heap_check on all heaps. Optionally print errors if heaps are corrupt.
|
||||
*
|
||||
* Calling this function is equivalent to calling heap_caps_check_integrity
|
||||
* with the caps argument set to MALLOC_CAP_INVALID.
|
||||
*
|
||||
* @param print_errors Print specific errors if heap corruption is found.
|
||||
*
|
||||
* @return True if all heaps are valid, False if at least one heap is corrupt.
|
||||
*/
|
||||
bool heap_caps_check_integrity_all(bool print_errors);
|
||||
|
||||
/**
|
||||
* @brief Check integrity of all heaps with the given capabilities.
|
||||
*
|
||||
* Calls multi_heap_check on all heaps which share the given capabilities. Optionally
|
||||
* print errors if the heaps are corrupt.
|
||||
*
|
||||
* See also heap_caps_check_integrity_all to check all heap memory
|
||||
* in the system and heap_caps_check_integrity_addr to check memory
|
||||
* around a single address.
|
||||
*
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
* @param print_errors Print specific errors if heap corruption is found.
|
||||
*
|
||||
* @return True if all heaps are valid, False if at least one heap is corrupt.
|
||||
*/
|
||||
bool heap_caps_check_integrity(uint32_t caps, bool print_errors);
|
||||
|
||||
/**
|
||||
* @brief Check integrity of heap memory around a given address.
|
||||
*
|
||||
* This function can be used to check the integrity of a single region of heap memory,
|
||||
* which contains the given address.
|
||||
*
|
||||
* This can be useful if debugging heap integrity for corruption at a known address,
|
||||
* as it has a lower overhead than checking all heap regions. Note that if the corrupt
|
||||
* address moves around between runs (due to timing or other factors) then this approach
|
||||
* won't work and you should call heap_caps_check_integrity or
|
||||
* heap_caps_check_integrity_all instead.
|
||||
*
|
||||
* @note The entire heap region around the address is checked, not only the adjacent
|
||||
* heap blocks.
|
||||
*
|
||||
* @param addr Address in memory. Check for corruption in region containing this address.
|
||||
* @param print_errors Print specific errors if heap corruption is found.
|
||||
*
|
||||
* @return True if the heap containing the specified address is valid,
|
||||
* False if at least one heap is corrupt or the address doesn't belong to a heap region.
|
||||
*/
|
||||
bool heap_caps_check_integrity_addr(intptr_t addr, bool print_errors);
|
||||
|
||||
/**
|
||||
* @brief Enable malloc() in external memory and set limit below which
|
||||
* malloc() attempts are placed in internal memory.
|
||||
*
|
||||
* When external memory is in use, the allocation strategy is to initially try to
|
||||
* satisfy smaller allocation requests with internal memory and larger requests
|
||||
* with external memory. This sets the limit between the two, as well as generally
|
||||
* enabling allocation in external memory.
|
||||
*
|
||||
* @param limit Limit, in bytes.
|
||||
*/
|
||||
void heap_caps_malloc_extmem_enable(size_t limit);
|
||||
|
||||
/**
|
||||
* @brief Allocate a chunk of memory as preference in decreasing order.
|
||||
*
|
||||
* @attention The variable parameters are bitwise OR of MALLOC_CAP_* flags indicating the type of memory.
|
||||
* This API prefers to allocate memory with the first parameter. If failed, allocate memory with
|
||||
* the next parameter. It will try in this order until allocating a chunk of memory successfully
|
||||
* or fail to allocate memories with any of the parameters.
|
||||
*
|
||||
* @param size Size, in bytes, of the amount of memory to allocate
|
||||
* @param num Number of variable paramters
|
||||
*
|
||||
* @return A pointer to the memory allocated on success, NULL on failure
|
||||
*/
|
||||
void *heap_caps_malloc_prefer( size_t size, size_t num, ... );
|
||||
|
||||
/**
|
||||
* @brief Allocate a chunk of memory as preference in decreasing order.
|
||||
*
|
||||
* @param ptr Pointer to previously allocated memory, or NULL for a new allocation.
|
||||
* @param size Size of the new buffer requested, or 0 to free the buffer.
|
||||
* @param num Number of variable paramters
|
||||
*
|
||||
* @return Pointer to a new buffer of size 'size', or NULL if allocation failed.
|
||||
*/
|
||||
void *heap_caps_realloc_prefer( void *ptr, size_t size, size_t num, ... );
|
||||
|
||||
/**
|
||||
* @brief Allocate a chunk of memory as preference in decreasing order.
|
||||
*
|
||||
* @param n Number of continuing chunks of memory to allocate
|
||||
* @param size Size, in bytes, of a chunk of memory to allocate
|
||||
* @param num Number of variable paramters
|
||||
*
|
||||
* @return A pointer to the memory allocated on success, NULL on failure
|
||||
*/
|
||||
void *heap_caps_calloc_prefer( size_t n, size_t size, size_t num, ... );
|
||||
|
||||
/**
|
||||
* @brief Dump the full structure of all heaps with matching capabilities.
|
||||
*
|
||||
* Prints a large amount of output to serial (because of locking limitations,
|
||||
* the output bypasses stdout/stderr). For each (variable sized) block
|
||||
* in each matching heap, the following output is printed on a single line:
|
||||
*
|
||||
* - Block address (the data buffer returned by malloc is 4 bytes after this
|
||||
* if heap debugging is set to Basic, or 8 bytes otherwise).
|
||||
* - Data size (the data size may be larger than the size requested by malloc,
|
||||
* either due to heap fragmentation or because of heap debugging level).
|
||||
* - Address of next block in the heap.
|
||||
* - If the block is free, the address of the next free block is also printed.
|
||||
*
|
||||
* @param caps Bitwise OR of MALLOC_CAP_* flags indicating the type
|
||||
* of memory
|
||||
*/
|
||||
void heap_caps_dump(uint32_t caps);
|
||||
|
||||
/**
|
||||
* @brief Dump the full structure of all heaps.
|
||||
*
|
||||
* Covers all registered heaps. Prints a large amount of output to serial.
|
||||
*
|
||||
* Output is the same as for heap_caps_dump.
|
||||
*
|
||||
*/
|
||||
void heap_caps_dump_all(void);
|
||||
|
||||
/**
|
||||
* @brief Return the size that a particular pointer was allocated with.
|
||||
*
|
||||
* @param ptr Pointer to currently allocated heap memory. Must be a pointer value previously
|
||||
* returned by heap_caps_malloc,malloc,calloc, etc. and not yet freed.
|
||||
*
|
||||
* @note The app will crash with an assertion failure if the pointer is not valid.
|
||||
*
|
||||
* @return Size of the memory allocated at this block.
|
||||
*
|
||||
*/
|
||||
size_t heap_caps_get_allocated_size( void *ptr );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
92
tools/sdk/esp32c3/include/heap/include/esp_heap_caps_init.h
Normal file
92
tools/sdk/esp32c3/include/heap/include/esp_heap_caps_init.h
Normal file
@ -0,0 +1,92 @@
|
||||
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#pragma once
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "soc/soc_memory_layout.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Initialize the capability-aware heap allocator.
|
||||
*
|
||||
* This is called once in the IDF startup code. Do not call it
|
||||
* at other times.
|
||||
*/
|
||||
void heap_caps_init(void);
|
||||
|
||||
/**
|
||||
* @brief Enable heap(s) in memory regions where the startup stacks are located.
|
||||
*
|
||||
* On startup, the pro/app CPUs have a certain memory region they use as stack, so we
|
||||
* cannot do allocations in the regions these stack frames are. When FreeRTOS is
|
||||
* completely started, they do not use that memory anymore and heap(s) there can
|
||||
* be enabled.
|
||||
*/
|
||||
void heap_caps_enable_nonos_stack_heaps(void);
|
||||
|
||||
/**
|
||||
* @brief Add a region of memory to the collection of heaps at runtime.
|
||||
*
|
||||
* Most memory regions are defined in soc_memory_layout.c for the SoC,
|
||||
* and are registered via heap_caps_init(). Some regions can't be used
|
||||
* immediately and are later enabled via heap_caps_enable_nonos_stack_heaps().
|
||||
*
|
||||
* Call this function to add a region of memory to the heap at some later time.
|
||||
*
|
||||
* This function does not consider any of the "reserved" regions or other data in soc_memory_layout, caller needs to
|
||||
* consider this themselves.
|
||||
*
|
||||
* All memory within the region specified by start & end parameters must be otherwise unused.
|
||||
*
|
||||
* The capabilities of the newly registered memory will be determined by the start address, as looked up in the regions
|
||||
* specified in soc_memory_layout.c.
|
||||
*
|
||||
* Use heap_caps_add_region_with_caps() to register a region with custom capabilities.
|
||||
*
|
||||
* @param start Start address of new region.
|
||||
* @param end End address of new region.
|
||||
*
|
||||
* @return ESP_OK on success, ESP_ERR_INVALID_ARG if a parameter is invalid, ESP_ERR_NOT_FOUND if the
|
||||
* specified start address doesn't reside in a known region, or any error returned by heap_caps_add_region_with_caps().
|
||||
*/
|
||||
esp_err_t heap_caps_add_region(intptr_t start, intptr_t end);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Add a region of memory to the collection of heaps at runtime, with custom capabilities.
|
||||
*
|
||||
* Similar to heap_caps_add_region(), only custom memory capabilities are specified by the caller.
|
||||
*
|
||||
* @param caps Ordered array of capability masks for the new region, in order of priority. Must have length
|
||||
* SOC_MEMORY_TYPE_NO_PRIOS. Does not need to remain valid after the call returns.
|
||||
* @param start Start address of new region.
|
||||
* @param end End address of new region.
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK on success
|
||||
* - ESP_ERR_INVALID_ARG if a parameter is invalid
|
||||
* - ESP_ERR_NO_MEM if no memory to register new heap.
|
||||
* - ESP_ERR_INVALID_SIZE if the memory region is too small to fit a heap
|
||||
* - ESP_FAIL if region overlaps the start and/or end of an existing region
|
||||
*/
|
||||
esp_err_t heap_caps_add_region_with_caps(const uint32_t caps[], intptr_t start, intptr_t end);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
98
tools/sdk/esp32c3/include/heap/include/esp_heap_task_info.h
Normal file
98
tools/sdk/esp32c3/include/heap/include/esp_heap_task_info.h
Normal file
@ -0,0 +1,98 @@
|
||||
// Copyright 2018 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#pragma once
|
||||
|
||||
#ifdef CONFIG_HEAP_TASK_TRACKING
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// This macro controls how much space is provided for partitioning the per-task
|
||||
// heap allocation info according to one or more sets of heap capabilities.
|
||||
#define NUM_HEAP_TASK_CAPS 4
|
||||
|
||||
/** @brief Structure to collect per-task heap allocation totals partitioned by selected caps */
|
||||
typedef struct {
|
||||
TaskHandle_t task; ///< Task to which these totals belong
|
||||
size_t size[NUM_HEAP_TASK_CAPS]; ///< Total allocations partitioned by selected caps
|
||||
size_t count[NUM_HEAP_TASK_CAPS]; ///< Number of blocks partitioned by selected caps
|
||||
} heap_task_totals_t;
|
||||
|
||||
/** @brief Structure providing details about a block allocated by a task */
|
||||
typedef struct {
|
||||
TaskHandle_t task; ///< Task that allocated the block
|
||||
void *address; ///< User address of allocated block
|
||||
uint32_t size; ///< Size of the allocated block
|
||||
} heap_task_block_t;
|
||||
|
||||
/** @brief Structure to provide parameters to heap_caps_get_per_task_info
|
||||
*
|
||||
* The 'caps' and 'mask' arrays allow partitioning the per-task heap allocation
|
||||
* totals by selected sets of heap region capabilities so that totals for
|
||||
* multiple regions can be accumulated in one scan. The capabilities flags for
|
||||
* each region ANDed with mask[i] are compared to caps[i] in order; the
|
||||
* allocations in that region are added to totals->size[i] and totals->count[i]
|
||||
* for the first i that matches. To collect the totals without any
|
||||
* partitioning, set mask[0] and caps[0] both to zero. The allocation totals
|
||||
* are returned in the 'totals' array of heap_task_totals_t structs. To allow
|
||||
* easily comparing the totals array between consecutive calls, that array can
|
||||
* be left populated from one call to the next so the order of tasks is the
|
||||
* same even if some tasks have freed their blocks or have been deleted. The
|
||||
* number of blocks prepopulated is given by num_totals, which is updated upon
|
||||
* return. If there are more tasks with allocations than the capacity of the
|
||||
* totals array (given by max_totals), information for the excess tasks will be
|
||||
* not be collected. The totals array pointer can be NULL if the totals are
|
||||
* not desired.
|
||||
*
|
||||
* The 'tasks' array holds a list of handles for tasks whose block details are
|
||||
* to be returned in the 'blocks' array of heap_task_block_t structs. If the
|
||||
* tasks array pointer is NULL, block details for all tasks will be returned up
|
||||
* to the capacity of the buffer array, given by max_blocks. The function
|
||||
* return value tells the number of blocks filled into the array. The blocks
|
||||
* array pointer can be NULL if block details are not desired, or max_blocks
|
||||
* can be set to zero.
|
||||
*/
|
||||
typedef struct {
|
||||
int32_t caps[NUM_HEAP_TASK_CAPS]; ///< Array of caps for partitioning task totals
|
||||
int32_t mask[NUM_HEAP_TASK_CAPS]; ///< Array of masks under which caps must match
|
||||
TaskHandle_t *tasks; ///< Array of tasks whose block info is returned
|
||||
size_t num_tasks; ///< Length of tasks array
|
||||
heap_task_totals_t *totals; ///< Array of structs to collect task totals
|
||||
size_t *num_totals; ///< Number of task structs currently in array
|
||||
size_t max_totals; ///< Capacity of array of task totals structs
|
||||
heap_task_block_t *blocks; ///< Array of task block details structs
|
||||
size_t max_blocks; ///< Capacity of array of task block info structs
|
||||
} heap_task_info_params_t;
|
||||
|
||||
/**
|
||||
* @brief Return per-task heap allocation totals and lists of blocks.
|
||||
*
|
||||
* For each task that has allocated memory from the heap, return totals for
|
||||
* allocations within regions matching one or more sets of capabilities.
|
||||
*
|
||||
* Optionally also return an array of structs providing details about each
|
||||
* block allocated by one or more requested tasks, or by all tasks.
|
||||
*
|
||||
* @param params Structure to hold all the parameters for the function
|
||||
* (@see heap_task_info_params_t).
|
||||
* @return Number of block detail structs returned (@see heap_task_block_t).
|
||||
*/
|
||||
extern size_t heap_caps_get_per_task_info(heap_task_info_params_t *params);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // CONFIG_HEAP_TASK_TRACKING
|
154
tools/sdk/esp32c3/include/heap/include/esp_heap_trace.h
Normal file
154
tools/sdk/esp32c3/include/heap/include/esp_heap_trace.h
Normal file
@ -0,0 +1,154 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#pragma once
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include <stdint.h>
|
||||
#include <esp_err.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_HEAP_TRACING) && !defined(HEAP_TRACE_SRCFILE)
|
||||
#warning "esp_heap_trace.h is included but heap tracing is disabled in menuconfig, functions are no-ops"
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_HEAP_TRACING_STACK_DEPTH
|
||||
#define CONFIG_HEAP_TRACING_STACK_DEPTH 0
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
HEAP_TRACE_ALL,
|
||||
HEAP_TRACE_LEAKS,
|
||||
} heap_trace_mode_t;
|
||||
|
||||
/**
|
||||
* @brief Trace record data type. Stores information about an allocated region of memory.
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t ccount; ///< CCOUNT of the CPU when the allocation was made. LSB (bit value 1) is the CPU number (0 or 1).
|
||||
void *address; ///< Address which was allocated
|
||||
size_t size; ///< Size of the allocation
|
||||
void *alloced_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which allocated the memory.
|
||||
void *freed_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which freed the memory (all zero if not freed.)
|
||||
} heap_trace_record_t;
|
||||
|
||||
/**
|
||||
* @brief Initialise heap tracing in standalone mode.
|
||||
*
|
||||
* This function must be called before any other heap tracing functions.
|
||||
*
|
||||
* To disable heap tracing and allow the buffer to be freed, stop tracing and then call heap_trace_init_standalone(NULL, 0);
|
||||
*
|
||||
* @param record_buffer Provide a buffer to use for heap trace data. Must remain valid any time heap tracing is enabled, meaning
|
||||
* it must be allocated from internal memory not in PSRAM.
|
||||
* @param num_records Size of the heap trace buffer, as number of record structures.
|
||||
* @return
|
||||
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
||||
* - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
|
||||
* - ESP_OK Heap tracing initialised successfully.
|
||||
*/
|
||||
esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records);
|
||||
|
||||
/**
|
||||
* @brief Initialise heap tracing in host-based mode.
|
||||
*
|
||||
* This function must be called before any other heap tracing functions.
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
|
||||
* - ESP_OK Heap tracing initialised successfully.
|
||||
*/
|
||||
esp_err_t heap_trace_init_tohost(void);
|
||||
|
||||
/**
|
||||
* @brief Start heap tracing. All heap allocations & frees will be traced, until heap_trace_stop() is called.
|
||||
*
|
||||
* @note heap_trace_init_standalone() must be called to provide a valid buffer, before this function is called.
|
||||
*
|
||||
* @note Calling this function while heap tracing is running will reset the heap trace state and continue tracing.
|
||||
*
|
||||
* @param mode Mode for tracing.
|
||||
* - HEAP_TRACE_ALL means all heap allocations and frees are traced.
|
||||
* - HEAP_TRACE_LEAKS means only suspected memory leaks are traced. (When memory is freed, the record is removed from the trace buffer.)
|
||||
* @return
|
||||
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
||||
* - ESP_ERR_INVALID_STATE A non-zero-length buffer has not been set via heap_trace_init_standalone().
|
||||
* - ESP_OK Tracing is started.
|
||||
*/
|
||||
esp_err_t heap_trace_start(heap_trace_mode_t mode);
|
||||
|
||||
/**
|
||||
* @brief Stop heap tracing.
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
||||
* - ESP_ERR_INVALID_STATE Heap tracing was not in progress.
|
||||
* - ESP_OK Heap tracing stopped..
|
||||
*/
|
||||
esp_err_t heap_trace_stop(void);
|
||||
|
||||
/**
|
||||
* @brief Resume heap tracing which was previously stopped.
|
||||
*
|
||||
* Unlike heap_trace_start(), this function does not clear the
|
||||
* buffer of any pre-existing trace records.
|
||||
*
|
||||
* The heap trace mode is the same as when heap_trace_start() was
|
||||
* last called (or HEAP_TRACE_ALL if heap_trace_start() was never called).
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
||||
* - ESP_ERR_INVALID_STATE Heap tracing was already started.
|
||||
* - ESP_OK Heap tracing resumed.
|
||||
*/
|
||||
esp_err_t heap_trace_resume(void);
|
||||
|
||||
/**
|
||||
* @brief Return number of records in the heap trace buffer
|
||||
*
|
||||
* It is safe to call this function while heap tracing is running.
|
||||
*/
|
||||
size_t heap_trace_get_count(void);
|
||||
|
||||
/**
|
||||
* @brief Return a raw record from the heap trace buffer
|
||||
*
|
||||
* @note It is safe to call this function while heap tracing is running, however in HEAP_TRACE_LEAK mode record indexing may
|
||||
* skip entries unless heap tracing is stopped first.
|
||||
*
|
||||
* @param index Index (zero-based) of the record to return.
|
||||
* @param[out] record Record where the heap trace record will be copied.
|
||||
* @return
|
||||
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
||||
* - ESP_ERR_INVALID_STATE Heap tracing was not initialised.
|
||||
* - ESP_ERR_INVALID_ARG Index is out of bounds for current heap trace record count.
|
||||
* - ESP_OK Record returned successfully.
|
||||
*/
|
||||
esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record);
|
||||
|
||||
/**
|
||||
* @brief Dump heap trace record data to stdout
|
||||
*
|
||||
* @note It is safe to call this function while heap tracing is running, however in HEAP_TRACE_LEAK mode the dump may skip
|
||||
* entries unless heap tracing is stopped first.
|
||||
*
|
||||
*
|
||||
*/
|
||||
void heap_trace_dump(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
190
tools/sdk/esp32c3/include/heap/include/multi_heap.h
Normal file
190
tools/sdk/esp32c3/include/heap/include/multi_heap.h
Normal file
@ -0,0 +1,190 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/* multi_heap is a heap implementation for handling multiple
|
||||
heterogenous heaps in a single program.
|
||||
|
||||
Any contiguous block of memory can be registered as a heap.
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief Opaque handle to a registered heap */
|
||||
typedef struct multi_heap_info *multi_heap_handle_t;
|
||||
|
||||
/**
|
||||
* @brief allocate a chunk of memory with specific alignment
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param size size in bytes of memory chunk
|
||||
* @param alignment how the memory must be aligned
|
||||
*
|
||||
* @return pointer to the memory allocated, NULL on failure
|
||||
*/
|
||||
void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment);
|
||||
|
||||
/** @brief malloc() a buffer in a given heap
|
||||
*
|
||||
* Semantics are the same as standard malloc(), only the returned buffer will be allocated in the specified heap.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param size Size of desired buffer.
|
||||
*
|
||||
* @return Pointer to new memory, or NULL if allocation fails.
|
||||
*/
|
||||
void *multi_heap_malloc(multi_heap_handle_t heap, size_t size);
|
||||
|
||||
/** @brief free() a buffer aligned in a given heap.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param p NULL, or a pointer previously returned from multi_heap_aligned_alloc() for the same heap.
|
||||
* @note This function is deprecated, consider using multi_heap_free() instead
|
||||
*/
|
||||
void __attribute__((deprecated)) multi_heap_aligned_free(multi_heap_handle_t heap, void *p);
|
||||
|
||||
/** @brief free() a buffer in a given heap.
|
||||
*
|
||||
* Semantics are the same as standard free(), only the argument 'p' must be NULL or have been allocated in the specified heap.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param p NULL, or a pointer previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
|
||||
*/
|
||||
void multi_heap_free(multi_heap_handle_t heap, void *p);
|
||||
|
||||
/** @brief realloc() a buffer in a given heap.
|
||||
*
|
||||
* Semantics are the same as standard realloc(), only the argument 'p' must be NULL or have been allocated in the specified heap.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param p NULL, or a pointer previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
|
||||
* @param size Desired new size for buffer.
|
||||
*
|
||||
* @return New buffer of 'size' containing contents of 'p', or NULL if reallocation failed.
|
||||
*/
|
||||
void *multi_heap_realloc(multi_heap_handle_t heap, void *p, size_t size);
|
||||
|
||||
|
||||
/** @brief Return the size that a particular pointer was allocated with.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param p Pointer, must have been previously returned from multi_heap_malloc() or multi_heap_realloc() for the same heap.
|
||||
*
|
||||
* @return Size of the memory allocated at this block. May be more than the original size argument, due
|
||||
* to padding and minimum block sizes.
|
||||
*/
|
||||
size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p);
|
||||
|
||||
|
||||
/** @brief Register a new heap for use
|
||||
*
|
||||
* This function initialises a heap at the specified address, and returns a handle for future heap operations.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @param start Start address of the memory to use for a new heap.
|
||||
* @param size Size (in bytes) of the new heap.
|
||||
*
|
||||
* @return Handle of a new heap ready for use, or NULL if the heap region was too small to be initialised.
|
||||
*/
|
||||
multi_heap_handle_t multi_heap_register(void *start, size_t size);
|
||||
|
||||
|
||||
/** @brief Associate a private lock pointer with a heap
|
||||
*
|
||||
* The lock argument is supplied to the MULTI_HEAP_LOCK() and MULTI_HEAP_UNLOCK() macros, defined in multi_heap_platform.h.
|
||||
*
|
||||
* The lock in question must be recursive.
|
||||
*
|
||||
* When the heap is first registered, the associated lock is NULL.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param lock Optional pointer to a locking structure to associate with this heap.
|
||||
*/
|
||||
void multi_heap_set_lock(multi_heap_handle_t heap, void* lock);
|
||||
|
||||
/** @brief Dump heap information to stdout
|
||||
*
|
||||
* For debugging purposes, this function dumps information about every block in the heap to stdout.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
*/
|
||||
void multi_heap_dump(multi_heap_handle_t heap);
|
||||
|
||||
/** @brief Check heap integrity
|
||||
*
|
||||
* Walks the heap and checks all heap data structures are valid. If any errors are detected, an error-specific message
|
||||
* can be optionally printed to stderr. Print behaviour can be overriden at compile time by defining
|
||||
* MULTI_CHECK_FAIL_PRINTF in multi_heap_platform.h.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param print_errors If true, errors will be printed to stderr.
|
||||
* @return true if heap is valid, false otherwise.
|
||||
*/
|
||||
bool multi_heap_check(multi_heap_handle_t heap, bool print_errors);
|
||||
|
||||
/** @brief Return free heap size
|
||||
*
|
||||
* Returns the number of bytes available in the heap.
|
||||
*
|
||||
* Equivalent to the total_free_bytes member returned by multi_heap_get_heap_info().
|
||||
*
|
||||
* Note that the heap may be fragmented, so the actual maximum size for a single malloc() may be lower. To know this
|
||||
* size, see the largest_free_block member returned by multi_heap_get_heap_info().
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @return Number of free bytes.
|
||||
*/
|
||||
size_t multi_heap_free_size(multi_heap_handle_t heap);
|
||||
|
||||
/** @brief Return the lifetime minimum free heap size
|
||||
*
|
||||
* Equivalent to the minimum_free_bytes member returned by multi_heap_get_info().
|
||||
*
|
||||
* Returns the lifetime "low water mark" of possible values returned from multi_free_heap_size(), for the specified
|
||||
* heap.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @return Number of free bytes.
|
||||
*/
|
||||
size_t multi_heap_minimum_free_size(multi_heap_handle_t heap);
|
||||
|
||||
/** @brief Structure to access heap metadata via multi_heap_get_info */
|
||||
typedef struct {
|
||||
size_t total_free_bytes; ///< Total free bytes in the heap. Equivalent to multi_free_heap_size().
|
||||
size_t total_allocated_bytes; ///< Total bytes allocated to data in the heap.
|
||||
size_t largest_free_block; ///< Size of largest free block in the heap. This is the largest malloc-able size.
|
||||
size_t minimum_free_bytes; ///< Lifetime minimum free heap size. Equivalent to multi_minimum_free_heap_size().
|
||||
size_t allocated_blocks; ///< Number of (variable size) blocks allocated in the heap.
|
||||
size_t free_blocks; ///< Number of (variable size) free blocks in the heap.
|
||||
size_t total_blocks; ///< Total number of (variable size) blocks in the heap.
|
||||
} multi_heap_info_t;
|
||||
|
||||
/** @brief Return metadata about a given heap
|
||||
*
|
||||
* Fills a multi_heap_info_t structure with information about the specified heap.
|
||||
*
|
||||
* @param heap Handle to a registered heap.
|
||||
* @param info Pointer to a structure to fill with heap metadata.
|
||||
*/
|
||||
void multi_heap_get_info(multi_heap_handle_t heap, multi_heap_info_t *info);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user