LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/lib/heap - heap.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 0.0 % 85 0
Test Date: 2026-11-06 14:43:19 Functions: 0.0 % 26 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0.0 % 20 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2019 Intel Corporation
       3                 :             :  * Copyright (c) 2026 Qualcomm Technologies, Inc.
       4                 :             :  *
       5                 :             :  * SPDX-License-Identifier: Apache-2.0
       6                 :             :  */
       7                 :             : #ifndef ZEPHYR_INCLUDE_LIB_OS_HEAP_H_
       8                 :             : #define ZEPHYR_INCLUDE_LIB_OS_HEAP_H_
       9                 :             : 
      10                 :             : #include <zephyr/sys/minmax.h>
      11                 :             : 
      12                 :             : /*
      13                 :             :  * Internal heap APIs
      14                 :             :  */
      15                 :             : 
      16                 :             : /* These validation checks are non-trivially expensive, so enable
      17                 :             :  * only when debugging the heap code.  They shouldn't be routine
      18                 :             :  * assertions.
      19                 :             :  */
      20                 :             : #ifdef CONFIG_SYS_HEAP_VALIDATE
      21                 :             : #define CHECK(x) __ASSERT(x, "")
      22                 :             : #else
      23                 :             : #define CHECK(x) /**/
      24                 :             : #endif
      25                 :             : 
      26                 :             : /* Heap hardening level predicates.  Each is true when the configured
      27                 :             :  * hardening level is at or above the named level.  The compiler
      28                 :             :  * eliminates dead code at lower levels.
      29                 :             :  */
      30                 :             : #define SYS_HEAP_HARDENING_BASIC    (CONFIG_SYS_HEAP_HARDENING_LEVEL >= 1)
      31                 :             : #define SYS_HEAP_HARDENING_MODERATE (CONFIG_SYS_HEAP_HARDENING_LEVEL >= 2)
      32                 :             : #define SYS_HEAP_HARDENING_FULL     (CONFIG_SYS_HEAP_HARDENING_LEVEL >= 3)
      33                 :             : #define SYS_HEAP_HARDENING_EXTREME (CONFIG_SYS_HEAP_HARDENING_LEVEL >= 4)
      34                 :             : 
      35                 :             : /* Chunks are identified by their offset in 8 byte units from the
      36                 :             :  * first address in the buffer (a zero-valued chunkid_t is used as a
      37                 :             :  * null; that chunk would always point into the metadata at the start
      38                 :             :  * of the heap and cannot be allocated).  They are prefixed by a
      39                 :             :  * variable size header that depends on the size of the heap.  Heaps
      40                 :             :  * with fewer than 2^15 units (256kb) of storage use shorts to store
      41                 :             :  * the fields, otherwise the units are 32 bit integers for a 16Gb heap
      42                 :             :  * space (larger spaces really aren't in scope for this code, but
      43                 :             :  * could be handled similarly I suppose).  Because of that design
      44                 :             :  * there's a certain amount of boilerplate API needed to expose the
      45                 :             :  * field accessors since we can't use natural syntax.
      46                 :             :  *
      47                 :             :  * The fields are:
      48                 :             :  *   LEFT_SIZE: The size of the left (next lower chunk in memory)
      49                 :             :  *              neighbor chunk.
      50                 :             :  *   SIZE_AND_USED: the total size (including header) of the chunk in
      51                 :             :  *                  8-byte units.  The bottom bit stores a "used" flag.
      52                 :             :  *   FREE_PREV: Chunk ID of the previous node in a free list.
      53                 :             :  *   FREE_NEXT: Chunk ID of the next node in a free list.
      54                 :             :  *
      55                 :             :  * The free lists are circular lists, one for each power-of-two size
      56                 :             :  * category.  The free list pointers exist only for free chunks,
      57                 :             :  * obviously.  This memory is part of the user's buffer when
      58                 :             :  * allocated.
      59                 :             :  *
      60                 :             :  * The field order is so that allocated buffers are immediately bounded
      61                 :             :  * by SIZE_AND_USED of the current chunk at the bottom, and LEFT_SIZE of
      62                 :             :  * the following chunk at the top. This ordering allows for quick buffer
      63                 :             :  * overflow detection by testing left_chunk(c + chunk_size(c)) == c.
      64                 :             :  */
      65                 :             : 
      66                 :             : enum chunk_fields { LEFT_SIZE, SIZE_AND_USED, FREE_PREV, FREE_NEXT };
      67                 :             : 
      68                 :             : #define CHUNK_UNIT 8U
      69                 :             : 
      70                 :             : typedef struct { char bytes[CHUNK_UNIT]; } chunk_unit_t;
      71                 :             : 
      72                 :             : /* big_heap needs uint32_t, small_heap needs uint16_t */
      73                 :             : typedef uint32_t chunkid_t;
      74                 :             : typedef uint32_t chunksz_t;
      75                 :             : 
      76                 :             : #if defined(CONFIG_SYS_HEAP_CANARIES) || \
      77                 :             :     defined(CONFIG_SYS_HEAP_THREAD_STATS) || \
      78                 :             :     defined(CONFIG_SYS_HEAP_CALLER_POINTER)
      79                 :             : 
      80                 :             : #ifdef CONFIG_SYS_HEAP_THREAD_STATS
      81                 :             : struct k_thread;
      82                 :             : #endif
      83                 :             : 
      84                 :             : struct z_heap_chunk_trailer {
      85                 :             : #ifdef CONFIG_SYS_HEAP_CANARIES
      86                 :             :         uint32_t canary;
      87                 :             : #endif
      88                 :             : #ifdef CONFIG_SYS_HEAP_THREAD_STATS
      89                 :             :         struct k_thread *thread;
      90                 :             : #endif
      91                 :             : #ifdef CONFIG_SYS_HEAP_CALLER_POINTER
      92                 :             :         void *caller;
      93                 :             : #endif
      94                 :             : } __aligned(CHUNK_UNIT);
      95                 :             : 
      96                 :             : #define CHUNK_TRAILER_SIZE (sizeof(struct z_heap_chunk_trailer) / CHUNK_UNIT)
      97                 :             : 
      98                 :             : #else
      99                 :             : 
     100                 :             : #define CHUNK_TRAILER_SIZE 0
     101                 :             : 
     102                 :             : #endif
     103                 :             : 
     104                 :             : #ifdef CONFIG_SYS_HEAP_THREAD_STATS
     105                 :             : 
     106                 :             : struct z_heap_thread_stat {
     107                 :             :         struct k_thread *thread;
     108                 :             : #if defined(CONFIG_THREAD_NAME)
     109                 :             :         char name[CONFIG_THREAD_MAX_NAME_LEN];
     110                 :             : #endif
     111                 :             :         size_t total_alloc;
     112                 :             : };
     113                 :             : 
     114                 :             : struct z_heap_stats {
     115                 :             :         size_t isr_alloc_bytes;
     116                 :             :         size_t boot_alloc_bytes;
     117                 :             :         size_t overflow_alloc_bytes;
     118                 :             :         size_t num_threads;
     119                 :             :         struct z_heap_thread_stat threads[CONFIG_SYS_HEAP_STATS_MAX_TRACKED_THREADS];
     120                 :             : };
     121                 :             : 
     122                 :             : /*
     123                 :             :  * Sentinel stored in the chunk trailer thread field for allocations made
     124                 :             :  * before the Zephyr scheduler has started (k_is_pre_kernel() == true).
     125                 :             :  */
     126                 :             : #define Z_HEAP_BOOT_SENTINEL ((struct k_thread *)(uintptr_t)1U)
     127                 :             : 
     128                 :             : #endif /* CONFIG_SYS_HEAP_THREAD_STATS */
     129                 :             : 
     130                 :             : struct z_heap_bucket {
     131                 :             :         chunkid_t next;
     132                 :             : };
     133                 :             : 
     134                 :             : struct z_heap {
     135                 :             :         chunkid_t chunk0_hdr[2];
     136                 :             :         chunkid_t end_chunk;
     137                 :             :         uint32_t avail_buckets;
     138                 :             : #ifdef CONFIG_SYS_HEAP_RUNTIME_STATS
     139                 :             :         size_t free_bytes;
     140                 :             :         size_t allocated_bytes;
     141                 :             :         size_t max_allocated_bytes;
     142                 :             : #endif
     143                 :             : #ifdef CONFIG_SYS_HEAP_CANARIES_RANDOM
     144                 :             :         uint32_t canary_base;
     145                 :             : #endif
     146                 :             : #ifdef CONFIG_SYS_HEAP_THREAD_STATS
     147                 :             :         struct z_heap_stats *stats;
     148                 :             : #endif
     149                 :             :         struct z_heap_bucket buckets[];
     150                 :             : };
     151                 :             : 
     152                 :           0 : static inline bool big_heap_chunks(chunksz_t chunks)
     153                 :             : {
     154                 :           0 :         if (IS_ENABLED(CONFIG_SYS_HEAP_SMALL_ONLY)) {
     155                 :           0 :                 return false;
     156                 :             :         }
     157                 :             :         if (IS_ENABLED(CONFIG_SYS_HEAP_BIG_ONLY) || sizeof(void *) > 4U) {
     158                 :             :                 return true;
     159                 :             :         }
     160                 :             :         return chunks > 0x7fffU;
     161                 :             : }
     162                 :             : 
     163                 :           0 : static inline bool big_heap_bytes(size_t bytes)
     164                 :             : {
     165                 :           0 :         return big_heap_chunks(bytes / CHUNK_UNIT);
     166                 :             : }
     167                 :             : 
     168                 :           0 : static inline bool big_heap(struct z_heap *h)
     169                 :             : {
     170                 :           0 :         return big_heap_chunks(h->end_chunk);
     171                 :             : }
     172                 :             : 
     173                 :           0 : static inline chunk_unit_t *chunk_buf(struct z_heap *h)
     174                 :             : {
     175                 :             :         /* the struct z_heap matches with the first chunk */
     176                 :           0 :         return (chunk_unit_t *)h;
     177                 :             : }
     178                 :             : 
     179                 :           0 : static inline chunkid_t chunk_field(struct z_heap *h, chunkid_t c,
     180                 :             :                                     enum chunk_fields f)
     181                 :             : {
     182                 :           0 :         chunk_unit_t *buf = chunk_buf(h);
     183                 :           0 :         void *cmem = &buf[c];
     184                 :             : 
     185         [ #  # ]:           0 :         if (big_heap(h)) {
     186                 :           0 :                 return ((uint32_t *)cmem)[f];
     187                 :             :         } else {
     188                 :           0 :                 return ((uint16_t *)cmem)[f];
     189                 :             :         }
     190                 :             : }
     191                 :             : 
     192                 :           0 : static inline void chunk_set(struct z_heap *h, chunkid_t c,
     193                 :             :                              enum chunk_fields f, chunkid_t val)
     194                 :             : {
     195                 :           0 :         CHECK(c <= h->end_chunk);
     196                 :             : 
     197                 :           0 :         chunk_unit_t *buf = chunk_buf(h);
     198                 :           0 :         void *cmem = &buf[c];
     199                 :             : 
     200         [ #  # ]:           0 :         if (big_heap(h)) {
     201                 :           0 :                 CHECK(val == (uint32_t)val);
     202                 :           0 :                 ((uint32_t *)cmem)[f] = val;
     203                 :             :         } else {
     204                 :           0 :                 CHECK(val == (uint16_t)val);
     205                 :           0 :                 ((uint16_t *)cmem)[f] = val;
     206                 :             :         }
     207                 :           0 : }
     208                 :             : 
     209                 :           0 : static inline bool chunk_used(struct z_heap *h, chunkid_t c)
     210                 :             : {
     211                 :           0 :         return chunk_field(h, c, SIZE_AND_USED) & 1U;
     212                 :             : }
     213                 :             : 
     214                 :           0 : static inline chunksz_t chunk_size(struct z_heap *h, chunkid_t c)
     215                 :             : {
     216                 :           0 :         return chunk_field(h, c, SIZE_AND_USED) >> 1;
     217                 :             : }
     218                 :             : 
     219                 :           0 : static inline void set_chunk_used(struct z_heap *h, chunkid_t c, bool used)
     220                 :             : {
     221                 :           0 :         chunk_unit_t *buf = chunk_buf(h);
     222                 :           0 :         void *cmem = &buf[c];
     223                 :             : 
     224         [ #  # ]:           0 :         if (big_heap(h)) {
     225         [ #  # ]:           0 :                 if (used) {
     226                 :           0 :                         ((uint32_t *)cmem)[SIZE_AND_USED] |= 1U;
     227                 :             :                 } else {
     228                 :           0 :                         ((uint32_t *)cmem)[SIZE_AND_USED] &= ~1U;
     229                 :             :                 }
     230                 :             :         } else {
     231         [ #  # ]:           0 :                 if (used) {
     232                 :           0 :                         ((uint16_t *)cmem)[SIZE_AND_USED] |= 1U;
     233                 :             :                 } else {
     234                 :           0 :                         ((uint16_t *)cmem)[SIZE_AND_USED] &= ~1U;
     235                 :             :                 }
     236                 :             :         }
     237                 :           0 : }
     238                 :             : 
     239                 :             : /*
     240                 :             :  * Note: no need to preserve the used bit here as the chunk is never in use
     241                 :             :  * when its size is modified, and potential set_chunk_used() is always
     242                 :             :  * invoked after set_chunk_size().
     243                 :             :  */
     244                 :           0 : static inline void set_chunk_size(struct z_heap *h, chunkid_t c, chunksz_t size)
     245                 :             : {
     246                 :           0 :         chunk_set(h, c, SIZE_AND_USED, size << 1);
     247                 :           0 : }
     248                 :             : 
     249                 :           0 : static inline chunkid_t prev_free_chunk(struct z_heap *h, chunkid_t c)
     250                 :             : {
     251                 :           0 :         return chunk_field(h, c, FREE_PREV);
     252                 :             : }
     253                 :             : 
     254                 :           0 : static inline chunkid_t next_free_chunk(struct z_heap *h, chunkid_t c)
     255                 :             : {
     256                 :           0 :         return chunk_field(h, c, FREE_NEXT);
     257                 :             : }
     258                 :             : 
     259                 :           0 : static inline void set_prev_free_chunk(struct z_heap *h, chunkid_t c,
     260                 :             :                                        chunkid_t prev)
     261                 :             : {
     262                 :           0 :         chunk_set(h, c, FREE_PREV, prev);
     263                 :           0 : }
     264                 :             : 
     265                 :           0 : static inline void set_next_free_chunk(struct z_heap *h, chunkid_t c,
     266                 :             :                                        chunkid_t next)
     267                 :             : {
     268                 :           0 :         chunk_set(h, c, FREE_NEXT, next);
     269                 :           0 : }
     270                 :             : 
     271                 :           0 : static inline chunkid_t left_chunk(struct z_heap *h, chunkid_t c)
     272                 :             : {
     273                 :           0 :         return c - chunk_field(h, c, LEFT_SIZE);
     274                 :             : }
     275                 :             : 
     276                 :           0 : static inline chunkid_t right_chunk(struct z_heap *h, chunkid_t c)
     277                 :             : {
     278                 :           0 :         return c + chunk_size(h, c);
     279                 :             : }
     280                 :             : 
     281                 :           0 : static inline void set_left_chunk_size(struct z_heap *h, chunkid_t c,
     282                 :             :                                        chunksz_t size)
     283                 :             : {
     284                 :           0 :         chunk_set(h, c, LEFT_SIZE, size);
     285                 :           0 : }
     286                 :             : 
     287                 :           0 : static inline size_t chunk_header_bytes(struct z_heap *h)
     288                 :             : {
     289         [ #  # ]:           0 :         return big_heap(h) ? 8 : 4;
     290                 :             : }
     291                 :             : 
     292                 :           0 : static inline size_t heap_footer_bytes(size_t size)
     293                 :             : {
     294         [ #  # ]:           0 :         return big_heap_bytes(size) ? 8 : 4;
     295                 :             : }
     296                 :             : 
     297                 :           0 : static inline chunksz_t chunksz(size_t bytes)
     298                 :             : {
     299                 :           0 :         return (bytes + CHUNK_UNIT - 1U) / CHUNK_UNIT;
     300                 :             : }
     301                 :             : 
     302                 :             : /**
     303                 :             :  * Convert the number of requested bytes to chunks and clamp it to facilitate
     304                 :             :  * error handling. As some of the heap is used for metadata, there will never
     305                 :             :  * be enough space for 'end_chunk' chunks. Also note that since 'size_t' may
     306                 :             :  * be 64-bits wide, clamping guards against overflow when converting to the
     307                 :             :  * 32-bit wide 'chunksz_t'.
     308                 :             :  */
     309                 :           0 : static ALWAYS_INLINE chunksz_t bytes_to_chunksz(struct z_heap *h, size_t bytes, size_t extra)
     310                 :             : {
     311                 :           0 :         size_t chunks = (bytes / CHUNK_UNIT) + (extra / CHUNK_UNIT);
     312                 :           0 :         size_t oddments = ((bytes % CHUNK_UNIT) + (extra % CHUNK_UNIT) +
     313                 :           0 :                            chunk_header_bytes(h) + CHUNK_UNIT - 1U) / CHUNK_UNIT;
     314                 :             : 
     315                 :           0 :         return (chunksz_t)min(chunks + oddments + CHUNK_TRAILER_SIZE, h->end_chunk);
     316                 :             : }
     317                 :             : 
     318                 :           0 : static inline chunksz_t min_chunk_size(struct z_heap *h)
     319                 :             : {
     320                 :           0 :         return chunksz(chunk_header_bytes(h) + 1) + CHUNK_TRAILER_SIZE;
     321                 :             : }
     322                 :             : 
     323                 :             : /*
     324                 :             :  * Return true if chunk is undersized, i.e. smaller than min_chunk_size().
     325                 :             :  * Such chunks are not added to the free list because:
     326                 :             :  * 1) they would be too small to be allocatable anyway, and
     327                 :             :  * 2) they might be too small to store free list pointers.
     328                 :             :  * It happens that min_chunk_size() is always >= the free pointer storage size.
     329                 :             :  * The initial conditions short-circuit the comparison with build-time constants
     330                 :             :  * when undersized chunks cannot occur.
     331                 :             :  */
     332                 :           0 : static inline bool undersized_chunk(struct z_heap *h, chunkid_t c)
     333                 :             : {
     334         [ #  # ]:           0 :         return (CHUNK_TRAILER_SIZE != 0 || big_heap(h)) &&
     335         [ #  # ]:           0 :                 chunk_size(h, c) < min_chunk_size(h);
     336                 :             : }
     337                 :             : 
     338                 :           0 : static inline size_t chunk_usable_bytes(struct z_heap *h, chunkid_t c)
     339                 :             : {
     340                 :           0 :         return chunk_size(h, c) * CHUNK_UNIT - chunk_header_bytes(h)
     341                 :           0 :                - CHUNK_TRAILER_SIZE * CHUNK_UNIT;
     342                 :             : }
     343                 :             : 
     344                 :           0 : static inline size_t mem_align_gap(struct z_heap *h, void *mem)
     345                 :             : {
     346         [ #  # ]:           0 :         if (chunk_header_bytes(h) == CHUNK_UNIT) {
     347                 :             :                 return 0;
     348                 :             :         }
     349                 :           0 :         return ((uintptr_t)mem - chunk_header_bytes(h)) & (CHUNK_UNIT - 1);
     350                 :             : }
     351                 :             : 
     352                 :           0 : static inline int bucket_idx(struct z_heap *h, chunksz_t sz)
     353                 :             : {
     354                 :           0 :         unsigned int usable_sz = sz - min_chunk_size(h) + 1;
     355                 :           0 :         return 31 - __builtin_clz(usable_sz);
     356                 :             : }
     357                 :             : 
     358                 :             : static inline void get_alloc_info(struct z_heap *h, size_t *alloc_bytes,
     359                 :             :                            size_t *free_bytes)
     360                 :             : {
     361                 :             :         chunkid_t c;
     362                 :             : 
     363                 :             :         *alloc_bytes = 0;
     364                 :             :         *free_bytes = 0;
     365                 :             : 
     366                 :             :         for (c = right_chunk(h, 0); c < h->end_chunk; c = right_chunk(h, c)) {
     367                 :             :                 if (chunk_used(h, c)) {
     368                 :             :                         *alloc_bytes += chunk_usable_bytes(h, c);
     369                 :             :                 } else if (!undersized_chunk(h, c)) {
     370                 :             :                         *free_bytes += chunk_usable_bytes(h, c);
     371                 :             :                 }
     372                 :             :         }
     373                 :             : }
     374                 :             : 
     375                 :             : #if defined(CONFIG_SYS_HEAP_CANARIES) || \
     376                 :             :     defined(CONFIG_SYS_HEAP_THREAD_STATS) || \
     377                 :             :     defined(CONFIG_SYS_HEAP_CALLER_POINTER)
     378                 :             : 
     379                 :             : /* Returns pointer to the chunk trailer (at the end of the chunk) */
     380                 :             : static inline struct z_heap_chunk_trailer *chunk_trailer(struct z_heap *h,
     381                 :             :                                                          chunkid_t c)
     382                 :             : {
     383                 :             :         chunk_unit_t *buf = chunk_buf(h);
     384                 :             : 
     385                 :             :         return (struct z_heap_chunk_trailer *)&buf[c + chunk_size(h, c)
     386                 :             :                                                    - CHUNK_TRAILER_SIZE];
     387                 :             : }
     388                 :             : 
     389                 :             : #endif
     390                 :             : 
     391                 :             : #ifdef CONFIG_SYS_HEAP_THREAD_STATS
     392                 :             : struct sys_heap;
     393                 :             : void z_heap_stats_on_init(struct sys_heap *heap);
     394                 :             : void z_heap_stats_on_alloc(struct sys_heap *heap, chunkid_t c, void *mem);
     395                 :             : void z_heap_stats_on_free(struct sys_heap *heap, chunkid_t c, void *mem);
     396                 :             : void z_heap_stats_on_realloc(struct sys_heap *heap, chunkid_t c, void *ptr,
     397                 :             :                              size_t old_usable, struct k_thread *old_owner);
     398                 :             : #else
     399                 :             : #define z_heap_stats_on_init(heap)                    do { } while (false)
     400                 :             : #define z_heap_stats_on_alloc(heap, c, mem)           do { } while (false)
     401                 :             : #define z_heap_stats_on_free(heap, c, mem)            do { } while (false)
     402                 :             : #define z_heap_stats_on_realloc(heap, c, ptr, ob, oo) do { } while (false)
     403                 :             : #endif
     404                 :             : 
     405                 :             : #ifdef CONFIG_SYS_HEAP_VALIDATE
     406                 :             : bool z_heap_full_check(struct z_heap *h);
     407                 :             : #else
     408                 :             : static inline bool z_heap_full_check(struct z_heap *h)
     409                 :             : {
     410                 :             :         ARG_UNUSED(h);
     411                 :             :         return true;
     412                 :             : }
     413                 :             : #endif
     414                 :             : 
     415                 :             : #endif /* ZEPHYR_INCLUDE_LIB_OS_HEAP_H_ */
        

Generated by: LCOV version 2.0-1