1 /*
2  * SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #pragma once
7 
8 #include "sdkconfig.h"
9 
10 #ifdef CONFIG_HEAP_TASK_TRACKING
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 // This macro controls how much space is provided for partitioning the per-task
17 // heap allocation info according to one or more sets of heap capabilities.
18 #define NUM_HEAP_TASK_CAPS 4
19 
20 /** @brief Structure to collect per-task heap allocation totals partitioned by selected caps */
21 typedef struct {
22     TaskHandle_t task;                ///< Task to which these totals belong
23     size_t size[NUM_HEAP_TASK_CAPS];  ///< Total allocations partitioned by selected caps
24     size_t count[NUM_HEAP_TASK_CAPS]; ///< Number of blocks partitioned by selected caps
25 } heap_task_totals_t;
26 
27 /** @brief Structure providing details about a block allocated by a task */
28 typedef struct {
29     TaskHandle_t task;                ///< Task that allocated the block
30     void *address;                    ///< User address of allocated block
31     uint32_t size;                    ///< Size of the allocated block
32 } heap_task_block_t;
33 
34 /** @brief Structure to provide parameters to heap_caps_get_per_task_info
35  *
36  * The 'caps' and 'mask' arrays allow partitioning the per-task heap allocation
37  * totals by selected sets of heap region capabilities so that totals for
38  * multiple regions can be accumulated in one scan.  The capabilities flags for
39  * each region ANDed with mask[i] are compared to caps[i] in order; the
40  * allocations in that region are added to totals->size[i] and totals->count[i]
41  * for the first i that matches.  To collect the totals without any
42  * partitioning, set mask[0] and caps[0] both to zero.  The allocation totals
43  * are returned in the 'totals' array of heap_task_totals_t structs.  To allow
44  * easily comparing the totals array between consecutive calls, that array can
45  * be left populated from one call to the next so the order of tasks is the
46  * same even if some tasks have freed their blocks or have been deleted.  The
47  * number of blocks prepopulated is given by num_totals, which is updated upon
48  * return.  If there are more tasks with allocations than the capacity of the
49  * totals array (given by max_totals), information for the excess tasks will be
50  * not be collected.  The totals array pointer can be NULL if the totals are
51  * not desired.
52  *
53  * The 'tasks' array holds a list of handles for tasks whose block details are
54  * to be returned in the 'blocks' array of heap_task_block_t structs.  If the
55  * tasks array pointer is NULL, block details for all tasks will be returned up
56  * to the capacity of the buffer array, given by max_blocks.  The function
57  * return value tells the number of blocks filled into the array.  The blocks
58  * array pointer can be NULL if block details are not desired, or max_blocks
59  * can be set to zero.
60  */
61 typedef struct {
62     int32_t caps[NUM_HEAP_TASK_CAPS]; ///< Array of caps for partitioning task totals
63     int32_t mask[NUM_HEAP_TASK_CAPS]; ///< Array of masks under which caps must match
64     TaskHandle_t *tasks;              ///< Array of tasks whose block info is returned
65     size_t num_tasks;                 ///< Length of tasks array
66     heap_task_totals_t *totals;       ///< Array of structs to collect task totals
67     size_t *num_totals;               ///< Number of task structs currently in array
68     size_t max_totals;                ///< Capacity of array of task totals structs
69     heap_task_block_t *blocks;        ///< Array of task block details structs
70     size_t max_blocks;                ///< Capacity of array of task block info structs
71 } heap_task_info_params_t;
72 
73 /**
74  * @brief Return per-task heap allocation totals and lists of blocks.
75  *
76  * For each task that has allocated memory from the heap, return totals for
77  * allocations within regions matching one or more sets of capabilities.
78  *
79  * Optionally also return an array of structs providing details about each
80  * block allocated by one or more requested tasks, or by all tasks.
81  *
82  * @param params Structure to hold all the parameters for the function
83  * (@see heap_task_info_params_t).
84  * @return Number of block detail structs returned (@see heap_task_block_t).
85  */
86 extern size_t heap_caps_get_per_task_info(heap_task_info_params_t *params);
87 
88 #ifdef __cplusplus
89 }
90 #endif
91 
92 #endif // CONFIG_HEAP_TASK_TRACKING
93