|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 ARM Limited. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 5 | + * not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +/** |
| 19 | + * \file nsdynmem_tracker_lib.h |
| 20 | + * \brief Dynamical Memory Tracker library API |
| 21 | + * Provides tracking and tracing of dynamic memory blocks |
| 22 | + */ |
| 23 | + |
| 24 | +#ifndef NSDYNMEM_TRACKER_LIB_H_ |
| 25 | +#define NSDYNMEM_TRACKER_LIB_H_ |
| 26 | +#ifdef __cplusplus |
| 27 | +extern "C" { |
| 28 | +#endif |
| 29 | + |
| 30 | +#if NSDYNMEM_TRACKER_ENABLED==1 |
| 31 | + |
| 32 | +// Memory block structure with caller information |
| 33 | +typedef struct ns_dyn_mem_tracker_lib_mem_blocks_s { |
| 34 | + void *block; /**< Allocated memory block */ |
| 35 | + void *caller_addr; /**< Caller address */ |
| 36 | + uint32_t size; /**< Allocation size */ |
| 37 | + uint32_t total_size; /**< Total allocation size for all allocations */ |
| 38 | + uint32_t lifetime; /**< Memory block lifetime in steps (e.g. seconds) */ |
| 39 | + uint32_t ref_count; /**< Reference count */ |
| 40 | + const char *function; /**< Caller function */ |
| 41 | + uint16_t line; /**< Caller line in module */ |
| 42 | + bool permanent : 1; /**< Permanent memory block */ |
| 43 | + bool permanent_printed : 1; /**< Permanent memory block printed */ |
| 44 | +} ns_dyn_mem_tracker_lib_mem_blocks_t; |
| 45 | + |
| 46 | +// Extended memory block structure that is used if same caller allocates multiple memory blocks |
| 47 | +typedef struct ns_dyn_mem_tracker_lib_mem_blocks_ext_s { |
| 48 | + void *block; /**< Allocated memory block */ |
| 49 | + void *caller_addr; /**< Caller address */ |
| 50 | + uint32_t size; /**< Allocation size */ |
| 51 | +} ns_dyn_mem_tracker_lib_mem_blocks_ext_t; |
| 52 | + |
| 53 | +// Allocator information structure |
| 54 | +typedef struct ns_dyn_mem_tracker_lib_allocators_s { |
| 55 | + void *caller_addr; /**< Caller address */ |
| 56 | + uint32_t alloc_count; /**< Number of allocations */ |
| 57 | + uint32_t total_memory; /**< Total memory used by allocations */ |
| 58 | + uint32_t min_lifetime; /**< Shortest lifetime among the allocations */ |
| 59 | + const char *function; /**< Function name string */ |
| 60 | + uint16_t line; /**< Module line */ |
| 61 | +} ns_dyn_mem_tracker_lib_allocators_t; |
| 62 | + |
| 63 | +// Memory block array allocator / array size increase allocator |
| 64 | +typedef ns_dyn_mem_tracker_lib_mem_blocks_t *ns_dyn_mem_tracker_lib_alloc_mem_blocks(ns_dyn_mem_tracker_lib_mem_blocks_t *blocks, uint16_t *mem_blocks_count); |
| 65 | +// Extended memory block array allocator / array size increase allocator |
| 66 | +typedef ns_dyn_mem_tracker_lib_mem_blocks_ext_t *ns_dyn_mem_tracker_lib_alloc_mem_blocks_ext(ns_dyn_mem_tracker_lib_mem_blocks_ext_t *blocks, uint32_t *mem_blocks_count); |
| 67 | +// Extended memory block array index hash function to get memory block (allocation/search start) index from block address |
| 68 | +typedef uint32_t ns_dyn_mem_tracker_lib_mem_block_index_hash(void *block, uint32_t ext_mem_blocks_count); |
| 69 | + |
| 70 | +typedef struct ns_dyn_mem_tracker_lib_conf_s { |
| 71 | + ns_dyn_mem_tracker_lib_mem_blocks_t *mem_blocks; /**< Memory blocks array, if NULL calls allocator on init */ |
| 72 | + ns_dyn_mem_tracker_lib_mem_blocks_ext_t *ext_mem_blocks; /**< Extended memory blocks array, if NULL calls allocator on init */ |
| 73 | + ns_dyn_mem_tracker_lib_allocators_t *top_allocators; /**< Top allocators array */ |
| 74 | + ns_dyn_mem_tracker_lib_allocators_t *permanent_allocators; /**< Permanent allocators */ |
| 75 | + ns_dyn_mem_tracker_lib_allocators_t *to_permanent_allocators; /**< To permanent allocators */ |
| 76 | + ns_dyn_mem_tracker_lib_allocators_t *max_snap_shot_allocators; /**< Snap shot of maximum memory used by allocators */ |
| 77 | + ns_dyn_mem_tracker_lib_alloc_mem_blocks *alloc_mem_blocks; /**< Memory block array allocator / array size increase allocator */ |
| 78 | + ns_dyn_mem_tracker_lib_alloc_mem_blocks_ext *ext_alloc_mem_blocks; /**< Extended memory block array allocator / array size increase allocator */ |
| 79 | + ns_dyn_mem_tracker_lib_mem_block_index_hash *block_index_hash; /**< Hash function to get memory block index from block address */ |
| 80 | + uint32_t allocated_memory; /**< Currently allocated memory */ |
| 81 | + uint16_t mem_blocks_count; /**< Number of entries in memory blocks array */ |
| 82 | + uint32_t ext_mem_blocks_count; /**< Number of entries in extended memory blocks array */ |
| 83 | + uint16_t last_mem_block_index; /**< Last memory block in memory blocks array */ |
| 84 | + uint16_t top_allocators_count; /**< Top allocators array count */ |
| 85 | + uint16_t permanent_allocators_count; /**< Permanent allocators array count */ |
| 86 | + uint16_t to_permanent_allocators_count; /**< To permanent allocators array count */ |
| 87 | + uint16_t max_snap_shot_allocators_count; /**< Snap shot of maximum memory used by allocators array count */ |
| 88 | + uint16_t to_permanent_steps_count; /**< How many steps before moving block to permanent allocators list */ |
| 89 | +} ns_dyn_mem_tracker_lib_conf_t; |
| 90 | + |
| 91 | +int8_t ns_dyn_mem_tracker_lib_alloc(ns_dyn_mem_tracker_lib_conf_t *conf, void *caller_addr, const char *function, uint32_t line, void *block, uint32_t alloc_size); |
| 92 | +int8_t ns_dyn_mem_tracker_lib_free(ns_dyn_mem_tracker_lib_conf_t *conf, void *caller_addr, const char *function, uint32_t line, void *block); |
| 93 | +void ns_dyn_mem_tracker_lib_step(ns_dyn_mem_tracker_lib_conf_t *conf); |
| 94 | +int8_t ns_dyn_mem_tracker_lib_allocator_lists_update(ns_dyn_mem_tracker_lib_conf_t *conf); |
| 95 | +void ns_dyn_mem_tracker_lib_max_snap_shot_update(ns_dyn_mem_tracker_lib_conf_t *conf); |
| 96 | + |
| 97 | +#endif |
| 98 | + |
| 99 | +#ifdef __cplusplus |
| 100 | +} |
| 101 | +#endif |
| 102 | +#endif /* NSDYNMEM_TRACKER_LIB_H_ */ |
| 103 | + |
| 104 | + |
0 commit comments