        -:    0:Source:../../../os/common/oslib/include/chheap.h
        -:    0:Programs:17
        -:    1:/*
        -:    2:    ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
        -:    3:
        -:    4:    This file is part of ChibiOS.
        -:    5:
        -:    6:    ChibiOS is free software; you can redistribute it and/or modify
        -:    7:    it under the terms of the GNU General Public License as published by
        -:    8:    the Free Software Foundation; either version 3 of the License, or
        -:    9:    (at your option) any later version.
        -:   10:
        -:   11:    ChibiOS is distributed in the hope that it will be useful,
        -:   12:    but WITHOUT ANY WARRANTY; without even the implied warranty of
        -:   13:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        -:   14:    GNU General Public License for more details.
        -:   15:
        -:   16:    You should have received a copy of the GNU General Public License
        -:   17:    along with this program.  If not, see <http://www.gnu.org/licenses/>.
        -:   18:*/
        -:   19:
        -:   20:/**
        -:   21: * @file    chheap.h
        -:   22: * @brief   Heaps macros and structures.
        -:   23: *
        -:   24: * @addtogroup heaps
        -:   25: * @{
        -:   26: */
        -:   27:
        -:   28:#ifndef CHHEAP_H
        -:   29:#define CHHEAP_H
        -:   30:
        -:   31:#if !defined(CH_CFG_USE_HEAP)
        -:   32:#define CH_CFG_USE_HEAP                     FALSE
        -:   33:#endif
        -:   34:
        -:   35:#if (CH_CFG_USE_HEAP == TRUE) || defined(__DOXYGEN__)
        -:   36:
        -:   37:/*===========================================================================*/
        -:   38:/* Module constants.                                                         */
        -:   39:/*===========================================================================*/
        -:   40:
        -:   41:/**
        -:   42: * @brief   Minimum alignment used for heap.
        -:   43: * @note    Cannot use the sizeof operator in this macro.
        -:   44: */
        -:   45:#if (SIZEOF_PTR == 4) || defined(__DOXYGEN__)
        -:   46:#define CH_HEAP_ALIGNMENT   8U
        -:   47:#elif (SIZEOF_PTR == 2)
        -:   48:#define CH_HEAP_ALIGNMENT   4U
        -:   49:#else
        -:   50:#error "unsupported pointer size"
        -:   51:#endif
        -:   52:
        -:   53:/*===========================================================================*/
        -:   54:/* Module pre-compile time settings.                                         */
        -:   55:/*===========================================================================*/
        -:   56:
        -:   57:/*===========================================================================*/
        -:   58:/* Derived constants and error checks.                                       */
        -:   59:/*===========================================================================*/
        -:   60:
        -:   61:#if CH_CFG_USE_MEMCORE == FALSE
        -:   62:#error "CH_CFG_USE_HEAP requires CH_CFG_USE_MEMCORE"
        -:   63:#endif
        -:   64:
        -:   65:#if (CH_CFG_USE_MUTEXES == FALSE) && (CH_CFG_USE_SEMAPHORES == FALSE)
        -:   66:#error "CH_CFG_USE_HEAP requires CH_CFG_USE_MUTEXES and/or CH_CFG_USE_SEMAPHORES"
        -:   67:#endif
        -:   68:
        -:   69:/*===========================================================================*/
        -:   70:/* Module data structures and types.                                         */
        -:   71:/*===========================================================================*/
        -:   72:
        -:   73:/**
        -:   74: * @brief   Type of a memory heap.
        -:   75: */
        -:   76:typedef struct memory_heap memory_heap_t;
        -:   77:
        -:   78:/**
        -:   79: * @brief   Type of a memory heap header.
        -:   80: */
        -:   81:typedef union heap_header heap_header_t;
        -:   82:
        -:   83:/**
        -:   84: * @brief   Memory heap block header.
        -:   85: */
        -:   86:union heap_header {
        -:   87:  struct {
        -:   88:    heap_header_t       *next;      /**< @brief Next block in free list.    */
        -:   89:    size_t              pages;      /**< @brief Size of the area in pages.  */
        -:   90:  } free;
        -:   91:  struct {
        -:   92:    memory_heap_t       *heap;      /**< @brief Block owner heap.           */
        -:   93:    size_t              size;       /**< @brief Size of the area in bytes.  */
        -:   94:  } used;
        -:   95:};
        -:   96:
        -:   97:/**
        -:   98: * @brief   Structure describing a memory heap.
        -:   99: */
        -:  100:struct memory_heap {
        -:  101:  memgetfunc2_t         provider;   /**< @brief Memory blocks provider for
        -:  102:                                                this heap.                  */
        -:  103:  heap_header_t         header;     /**< @brief Free blocks list header.    */
        -:  104:#if CH_CFG_USE_MUTEXES == TRUE
        -:  105:  mutex_t               mtx;        /**< @brief Heap access mutex.          */
        -:  106:#else
        -:  107:  semaphore_t           sem;        /**< @brief Heap access semaphore.      */
        -:  108:#endif
        -:  109:};
        -:  110:
        -:  111:/*===========================================================================*/
        -:  112:/* Module macros.                                                            */
        -:  113:/*===========================================================================*/
        -:  114:
        -:  115:/**
        -:  116: * @brief   Allocation of an aligned static heap buffer.
        -:  117: */
        -:  118:#define CH_HEAP_AREA(name, size)                                            \
        -:  119:  ALIGNED_VAR(CH_HEAP_ALIGNMENT)                                            \
        -:  120:  uint8_t name[MEM_ALIGN_NEXT((size), CH_HEAP_ALIGNMENT)]
        -:  121:
        -:  122:/*===========================================================================*/
        -:  123:/* External declarations.                                                    */
        -:  124:/*===========================================================================*/
        -:  125:
        -:  126:#ifdef __cplusplus
        -:  127:extern "C" {
        -:  128:#endif
        -:  129:  void _heap_init(void);
        -:  130:  void chHeapObjectInit(memory_heap_t *heapp, void *buf, size_t size);
        -:  131:  void *chHeapAllocAligned(memory_heap_t *heapp, size_t size, unsigned align);
        -:  132:  void chHeapFree(void *p);
        -:  133:  size_t chHeapStatus(memory_heap_t *heapp, size_t *totalp, size_t *largestp);
        -:  134:#ifdef __cplusplus
        -:  135:}
        -:  136:#endif
        -:  137:
        -:  138:/*===========================================================================*/
        -:  139:/* Module inline functions.                                                  */
        -:  140:/*===========================================================================*/
        -:  141:
        -:  142:/**
        -:  143: * @brief   Allocates a block of memory from the heap by using the first-fit
        -:  144: *          algorithm.
        -:  145: * @details The allocated block is guaranteed to be properly aligned for a
        -:  146: *          pointer data type.
        -:  147: *
        -:  148: * @param[in] heapp     pointer to a heap descriptor or @p NULL in order to
        -:  149: *                      access the default heap.
        -:  150: * @param[in] size      the size of the block to be allocated. Note that the
        -:  151: *                      allocated block may be a bit bigger than the requested
        -:  152: *                      size for alignment and fragmentation reasons.
        -:  153: * @return              A pointer to the allocated block.
        -:  154: * @retval NULL         if the block cannot be allocated.
        -:  155: *
        -:  156: * @api
        -:  157: */
function chHeapAlloc called 2 returned 100% blocks executed 100%
        2:  158:static inline void *chHeapAlloc(memory_heap_t *heapp, size_t size) {
        -:  159:
        2:  160:  return chHeapAllocAligned(heapp, size, CH_HEAP_ALIGNMENT);
call    0 returned 100%
        -:  161:}
        -:  162:
        -:  163:/**
        -:  164: * @brief   Returns the size of an allocated block.
        -:  165: * @note    The returned value is the requested size, the real size is the
        -:  166: *          same value aligned to the next @p CH_HEAP_ALIGNMENT multiple.
        -:  167: *
        -:  168: * @param[in] p         pointer to the memory block
        -:  169: * @return              Size of the block.
        -:  170: *
        -:  171: * @api
        -:  172: */
        -:  173:static inline size_t chHeapGetSize(const void *p) {
        -:  174:
        -:  175:  return ((heap_header_t *)p - 1U)->used.size;
        -:  176:}
        -:  177:
        -:  178:#endif /* CH_CFG_USE_HEAP == TRUE */
        -:  179:
        -:  180:#endif /* CHHEAP_H */
        -:  181:
        -:  182:/** @} */
