2010-08-10 03:11:02 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
< html > < head > < meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8" >
< title > ChibiOS/RT: chheap.c Source File< / title >
< link href = "custom.css" rel = "stylesheet" type = "text/css" >
< link href = "tabs.css" rel = "stylesheet" type = "text/css" >
< / head > < body >
< table style = "text-align: center; width: 100%;" border = "0"
cellpadding="2" cellspacing="2">
< tbody >
< tr >
< td style = "width: 80px;" > < img alt = "ChibiOS/RT Logo" src = "logo_small.png" > < / td >
< td > < big > < big > ChibiOS/RT< / big > < / big > < br > < br > Architecture - Reference Manual - Guides< / td >
< td style = "width: 80px;" > < / td >
< / tr >
< / tbody >
< / table >
< hr size = "1" >
2010-11-22 05:53:37 +00:00
<!-- Generated by Doxygen 1.7.1 -->
2010-08-10 03:11:02 +00:00
< div class = "navigation" id = "top" >
< div class = "tabs" >
2010-11-22 05:53:37 +00:00
< ul class = "tablist" >
2010-08-10 03:11:02 +00:00
< li > < a href = "main.html" > < span > Main Page< / span > < / a > < / li >
< li > < a href = "modules.html" > < span > Modules< / span > < / a > < / li >
< li > < a href = "annotated.html" > < span > Data Structures< / span > < / a > < / li >
< li class = "current" > < a href = "files.html" > < span > Files< / span > < / a > < / li >
< / ul >
< / div >
2010-11-22 05:53:37 +00:00
< div class = "tabs2" >
< ul class = "tablist" >
2010-08-10 03:11:02 +00:00
< li > < a href = "files.html" > < span > File List< / span > < / a > < / li >
< li > < a href = "globals.html" > < span > Globals< / span > < / a > < / li >
< / ul >
< / div >
2010-11-22 05:53:37 +00:00
< div class = "header" >
< div class = "headertitle" >
< h1 > chheap.c< / h1 > < / div >
< / div >
< div class = "contents" >
< a href = "chheap_8c.html" > Go to the documentation of this file.< / a > < div class = "fragment" > < pre class = "fragment" > < a name = "l00001" > < / a > 00001 < span class = "comment" > /*< / span >
2010-08-10 03:11:02 +00:00
< a name = "l00002" > < / a > 00002 < span class = "comment" > ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.< / span >
< a name = "l00003" > < / a > 00003 < span class = "comment" > < / span >
< a name = "l00004" > < / a > 00004 < span class = "comment" > This file is part of ChibiOS/RT.< / span >
< a name = "l00005" > < / a > 00005 < span class = "comment" > < / span >
< a name = "l00006" > < / a > 00006 < span class = "comment" > ChibiOS/RT is free software; you can redistribute it and/or modify< / span >
< a name = "l00007" > < / a > 00007 < span class = "comment" > it under the terms of the GNU General Public License as published by< / span >
< a name = "l00008" > < / a > 00008 < span class = "comment" > the Free Software Foundation; either version 3 of the License, or< / span >
< a name = "l00009" > < / a > 00009 < span class = "comment" > (at your option) any later version.< / span >
< a name = "l00010" > < / a > 00010 < span class = "comment" > < / span >
< a name = "l00011" > < / a > 00011 < span class = "comment" > ChibiOS/RT is distributed in the hope that it will be useful,< / span >
< a name = "l00012" > < / a > 00012 < span class = "comment" > but WITHOUT ANY WARRANTY; without even the implied warranty of< / span >
< a name = "l00013" > < / a > 00013 < span class = "comment" > MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the< / span >
< a name = "l00014" > < / a > 00014 < span class = "comment" > GNU General Public License for more details.< / span >
< a name = "l00015" > < / a > 00015 < span class = "comment" > < / span >
< a name = "l00016" > < / a > 00016 < span class = "comment" > You should have received a copy of the GNU General Public License< / span >
< a name = "l00017" > < / a > 00017 < span class = "comment" > along with this program. If not, see < http://www.gnu.org/licenses/> .< / span >
< a name = "l00018" > < / a > 00018 < span class = "comment" > < / span >
< a name = "l00019" > < / a > 00019 < span class = "comment" > ---< / span >
< a name = "l00020" > < / a > 00020 < span class = "comment" > < / span >
< a name = "l00021" > < / a > 00021 < span class = "comment" > A special exception to the GPL can be applied should you wish to distribute< / span >
< a name = "l00022" > < / a > 00022 < span class = "comment" > a combined work that includes ChibiOS/RT, without being obliged to provide< / span >
< a name = "l00023" > < / a > 00023 < span class = "comment" > the source code for any proprietary components. See the file exception.txt< / span >
< a name = "l00024" > < / a > 00024 < span class = "comment" > for full details of how and when the exception can be applied.< / span >
< a name = "l00025" > < / a > 00025 < span class = "comment" > */< / span >
< a name = "l00026" > < / a > 00026 < span class = "comment" > < / span >
< a name = "l00027" > < / a > 00027 < span class = "comment" > /**< / span >
< a name = "l00028" > < / a > 00028 < span class = "comment" > * @file chheap.c< / span >
< a name = "l00029" > < / a > 00029 < span class = "comment" > * @brief Heaps code.< / span >
< a name = "l00030" > < / a > 00030 < span class = "comment" > *< / span >
< a name = "l00031" > < / a > 00031 < span class = "comment" > * @addtogroup heaps< / span >
< a name = "l00032" > < / a > 00032 < span class = "comment" > * @details Heap Allocator related APIs.< / span >
< a name = "l00033" > < / a > 00033 < span class = "comment" > * < h2> Operation mode< /h2> < / span >
< a name = "l00034" > < / a > 00034 < span class = "comment" > * The heap allocator implements a first-fit strategy and its APIs< / span >
< a name = "l00035" > < / a > 00035 < span class = "comment" > * are functionally equivalent to the usual @p malloc() and @p free()< / span >
< a name = "l00036" > < / a > 00036 < span class = "comment" > * library functions. The main difference is that the OS heap APIs< / span >
< a name = "l00037" > < / a > 00037 < span class = "comment" > * are guaranteed to be thread safe.< br> < / span >
< a name = "l00038" > < / a > 00038 < span class = "comment" > * By enabling the @p CH_USE_MALLOC_HEAP option the heap manager< / span >
< a name = "l00039" > < / a > 00039 < span class = "comment" > * will use the runtime-provided @p malloc() and @p free() as< / span >
< a name = "l00040" > < / a > 00040 < span class = "comment" > * backend for the heap APIs instead of the system provided< / span >
< a name = "l00041" > < / a > 00041 < span class = "comment" > * allocator.< br> < / span >
< a name = "l00042" > < / a > 00042 < span class = "comment" > * In order to use the heap APIs the @p CH_USE_HEAP option must< / span >
< a name = "l00043" > < / a > 00043 < span class = "comment" > * be enabled in @p chconf.h.< / span >
< a name = "l00044" > < / a > 00044 < span class = "comment" > * @{< / span >
< a name = "l00045" > < / a > 00045 < span class = "comment" > */< / span >
< a name = "l00046" > < / a > 00046
< a name = "l00047" > < / a > 00047 < span class = "preprocessor" > #include " < a class = "code" href = "ch_8h.html" title = "ChibiOS/RT main include file." > ch.h< / a > " < / span >
< a name = "l00048" > < / a > 00048
< a name = "l00049" > < / a > 00049 < span class = "preprocessor" > #if CH_USE_HEAP< / span >
< a name = "l00050" > < / a > 00050 < span class = "preprocessor" > < / span >
< a name = "l00051" > < / a > 00051 < span class = "preprocessor" > #if !CH_USE_MALLOC_HEAP< / span >
< a name = "l00052" > < / a > 00052 < span class = "preprocessor" > < / span >
< a name = "l00053" > < / a > 00053 < span class = "comment" > /*< / span >
< a name = "l00054" > < / a > 00054 < span class = "comment" > * Defaults on the best synchronization mechanism available.< / span >
< a name = "l00055" > < / a > 00055 < span class = "comment" > */< / span >
< a name = "l00056" > < / a > 00056 < span class = "preprocessor" > #if CH_USE_MUTEXES< / span >
< a name = "l00057" > < / a > 00057 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_LOCK(h) chMtxLock(& (h)-> h_mtx)< / span >
< a name = "l00058" > < / a > 00058 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_UNLOCK(h) chMtxUnlock()< / span >
< a name = "l00059" > < / a > 00059 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #else< / span >
< a name = "l00060" > < / a > 00060 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_LOCK(h) chSemWait(& (h)-> h_sem)< / span >
< a name = "l00061" > < / a > 00061 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_UNLOCK(h) chSemSignal(& (h)-> h_sem)< / span >
< a name = "l00062" > < / a > 00062 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #endif< / span >
< a name = "l00063" > < / a > 00063 < span class = "preprocessor" > < / span > < span class = "comment" > < / span >
< a name = "l00064" > < / a > 00064 < span class = "comment" > /**< / span >
< a name = "l00065" > < / a > 00065 < span class = "comment" > * @brief Default heap descriptor.< / span >
< a name = "l00066" > < / a > 00066 < span class = "comment" > */< / span >
< a name = "l00067" > < / a > 00067 < span class = "keyword" > static< / span > < a class = "code" href = "structmemory__heap.html" title = "Structure describing a memory heap." > MemoryHeap< / a > default_heap;
< a name = "l00068" > < / a > 00068 < span class = "comment" > < / span >
< a name = "l00069" > < / a > 00069 < span class = "comment" > /**< / span >
< a name = "l00070" > < / a > 00070 < span class = "comment" > * @brief Initializes the default heap.< / span >
< a name = "l00071" > < / a > 00071 < span class = "comment" > * @note Internal use only.< / span >
< a name = "l00072" > < / a > 00072 < span class = "comment" > */< / span >
< a name = "l00073" > < / a > < a class = "code" href = "group__heaps.html#ga7e7a6625a49b6a560a47ae7575575264" > 00073< / a > < span class = "keywordtype" > void< / span > < a class = "code" href = "group__heaps.html#ga7e7a6625a49b6a560a47ae7575575264" title = "Initializes the default heap." > heap_init< / a > (< span class = "keywordtype" > void< / span > ) {
2010-11-22 05:53:37 +00:00
< a name = "l00074" > < / a > 00074 default_heap.< a class = "code" href = "structmemory__heap.html#a4cad69cd924f2de313510634d2f6de1e" title = "Memory blocks provider for this heap." > h_provider< / a > = chCoreAlloc;
2010-08-10 03:11:02 +00:00
< a name = "l00075" > < / a > 00075 default_heap.< a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > .h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = (< span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > *)NULL;
< a name = "l00076" > < / a > 00076 default_heap.< a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > .h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > = 0;
< a name = "l00077" > < / a > 00077 < span class = "preprocessor" > #if CH_USE_MUTEXES< / span >
< a name = "l00078" > < / a > 00078 < span class = "preprocessor" > < / span > < a class = "code" href = "group__mutexes.html#gac55fe4d05aa6bbeb31594193d2e4d2b6" title = "Initializes s Mutex structure." > chMtxInit< / a > (& default_heap.< a class = "code" href = "structmemory__heap.html#aa7eafd71c17c8175c2c7662685777fb5" title = "Heap access mutex." > h_mtx< / a > );
< a name = "l00079" > < / a > 00079 < span class = "preprocessor" > #else< / span >
< a name = "l00080" > < / a > 00080 < span class = "preprocessor" > < / span > < a class = "code" href = "group__semaphores.html#gafe8fc6155a871074e8017efd908b2c58" title = "Initializes a semaphore with the specified counter value." > chSemInit< / a > (& default_heap.h_sem, 1);
< a name = "l00081" > < / a > 00081 < span class = "preprocessor" > #endif< / span >
< a name = "l00082" > < / a > 00082 < span class = "preprocessor" > < / span > }
< a name = "l00083" > < / a > 00083 < span class = "comment" > < / span >
< a name = "l00084" > < / a > 00084 < span class = "comment" > /**< / span >
< a name = "l00085" > < / a > 00085 < span class = "comment" > * @brief Initializes a memory heap from a static memory area.< / span >
< a name = "l00086" > < / a > 00086 < span class = "comment" > * @note Both the heap buffer base and the heap size must be aligned to< / span >
< a name = "l00087" > < / a > 00087 < span class = "comment" > * the @p align_t type size.< / span >
< a name = "l00088" > < / a > 00088 < span class = "comment" > *< / span >
< a name = "l00089" > < / a > 00089 < span class = "comment" > * @param[out] heapp pointer to the memory heap descriptor to be initialized< / span >
< a name = "l00090" > < / a > 00090 < span class = "comment" > * @param[in] buf heap buffer base< / span >
< a name = "l00091" > < / a > 00091 < span class = "comment" > * @param[in] size heap size< / span >
< a name = "l00092" > < / a > 00092 < span class = "comment" > */< / span >
< a name = "l00093" > < / a > < a class = "code" href = "group__heaps.html#ga7135c9ddbd1402a4a36ce44df948f4e4" > 00093< / a > < span class = "keywordtype" > void< / span > < a class = "code" href = "group__heaps.html#ga7135c9ddbd1402a4a36ce44df948f4e4" title = "Initializes a memory heap from a static memory area." > chHeapInit< / a > (< a class = "code" href = "structmemory__heap.html" title = "Structure describing a memory heap." > MemoryHeap< / a > *heapp, < span class = "keywordtype" > void< / span > *buf, < span class = "keywordtype" > size_t< / span > < a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > ) {
< a name = "l00094" > < / a > 00094 < span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > *hp;
< a name = "l00095" > < / a > 00095
< a name = "l00096" > < / a > 00096 < a class = "code" href = "group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title = "Function parameter check." > chDbgCheck< / a > (< a class = "code" href = "group__memcore.html#gac38a64d9001b24d62f1c9ab79fbc328b" title = "Returns whatever a pointer or memory size is aligned to the type align_t." > MEM_IS_ALIGNED< / a > (buf) & & < a class = "code" href = "group__memcore.html#gac38a64d9001b24d62f1c9ab79fbc328b" title = "Returns whatever a pointer or memory size is aligned to the type align_t." > MEM_IS_ALIGNED< / a > (size), < span class = "stringliteral" > " chHeapInit" < / span > );
< a name = "l00097" > < / a > 00097
2010-11-22 05:53:37 +00:00
< a name = "l00098" > < / a > 00098 heapp-> < a class = "code" href = "structmemory__heap.html#a4cad69cd924f2de313510634d2f6de1e" title = "Memory blocks provider for this heap." > h_provider< / a > = (memgetfunc_t)NULL;
2010-08-10 03:11:02 +00:00
< a name = "l00099" > < / a > 00099 heapp-> < a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > .h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = hp = buf;
< a name = "l00100" > < / a > 00100 heapp-> < a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > .h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > = 0;
< a name = "l00101" > < / a > 00101 hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = NULL;
< a name = "l00102" > < / a > 00102 hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > = size - < span class = "keyword" > sizeof< / span > (< span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > );
< a name = "l00103" > < / a > 00103 < span class = "preprocessor" > #if CH_USE_MUTEXES< / span >
< a name = "l00104" > < / a > 00104 < span class = "preprocessor" > < / span > < a class = "code" href = "group__mutexes.html#gac55fe4d05aa6bbeb31594193d2e4d2b6" title = "Initializes s Mutex structure." > chMtxInit< / a > (& heapp-> < a class = "code" href = "structmemory__heap.html#aa7eafd71c17c8175c2c7662685777fb5" title = "Heap access mutex." > h_mtx< / a > );
< a name = "l00105" > < / a > 00105 < span class = "preprocessor" > #else< / span >
< a name = "l00106" > < / a > 00106 < span class = "preprocessor" > < / span > < a class = "code" href = "group__semaphores.html#gafe8fc6155a871074e8017efd908b2c58" title = "Initializes a semaphore with the specified counter value." > chSemInit< / a > (& heapp-> h_sem, 1);
< a name = "l00107" > < / a > 00107 < span class = "preprocessor" > #endif< / span >
< a name = "l00108" > < / a > 00108 < span class = "preprocessor" > < / span > }
< a name = "l00109" > < / a > 00109 < span class = "comment" > < / span >
< a name = "l00110" > < / a > 00110 < span class = "comment" > /**< / span >
< a name = "l00111" > < / a > 00111 < span class = "comment" > * @brief Allocates a block of memory from the heap by using the first-fit< / span >
< a name = "l00112" > < / a > 00112 < span class = "comment" > * algorithm.< / span >
< a name = "l00113" > < / a > 00113 < span class = "comment" > * @details The allocated block is guaranteed to be properly aligned for a< / span >
< a name = "l00114" > < / a > 00114 < span class = "comment" > * pointer data type (@p align_t).< / span >
< a name = "l00115" > < / a > 00115 < span class = "comment" > *< / span >
< a name = "l00116" > < / a > 00116 < span class = "comment" > * @param[in] heapp pointer to a heap descriptor or @p NULL in order to< / span >
< a name = "l00117" > < / a > 00117 < span class = "comment" > * access the default heap.< / span >
< a name = "l00118" > < / a > 00118 < span class = "comment" > * @param[in] size the size of the block to be allocated. Note that the< / span >
< a name = "l00119" > < / a > 00119 < span class = "comment" > * allocated block may be a bit bigger than the requested< / span >
< a name = "l00120" > < / a > 00120 < span class = "comment" > * size for alignment and fragmentation reasons.< / span >
< a name = "l00121" > < / a > 00121 < span class = "comment" > * @return A pointer to the allocated block.< / span >
< a name = "l00122" > < / a > 00122 < span class = "comment" > * @retval NULL if the block cannot be allocated.< / span >
< a name = "l00123" > < / a > 00123 < span class = "comment" > */< / span >
< a name = "l00124" > < / a > < a class = "code" href = "group__heaps.html#ga3d364eec0bef8b1986f63b3288ebbd5f" > 00124< / a > < span class = "keywordtype" > void< / span > *< a class = "code" href = "group__heaps.html#ga3d364eec0bef8b1986f63b3288ebbd5f" title = "Allocates a block of memory from the heap by using the first-fit algorithm." > chHeapAlloc< / a > (< a class = "code" href = "structmemory__heap.html" title = "Structure describing a memory heap." > MemoryHeap< / a > *heapp, < span class = "keywordtype" > size_t< / span > < a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > ) {
< a name = "l00125" > < / a > 00125 < span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > *qp, *hp, *fp;
< a name = "l00126" > < / a > 00126
< a name = "l00127" > < / a > 00127 < span class = "keywordflow" > if< / span > (heapp == NULL)
< a name = "l00128" > < / a > 00128 heapp = & default_heap;
< a name = "l00129" > < / a > 00129
< a name = "l00130" > < / a > 00130 size = < a class = "code" href = "group__memcore.html#ga216a7738be23e0caa9d18e510eb795eb" title = "Alignment helper macro." > MEM_ALIGN_SIZE< / a > (size);
< a name = "l00131" > < / a > 00131 qp = & heapp-> < a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > ;
< a name = "l00132" > < / a > 00132 H_LOCK(heapp);
< a name = "l00133" > < / a > 00133
< a name = "l00134" > < / a > 00134 < span class = "keywordflow" > while< / span > (qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next != NULL) {
< a name = "l00135" > < / a > 00135 hp = qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next;
< a name = "l00136" > < / a > 00136 < span class = "keywordflow" > if< / span > (hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > > = size) {
< a name = "l00137" > < / a > 00137 < span class = "keywordflow" > if< / span > (hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > < size + < span class = "keyword" > sizeof< / span > (< span class = "keyword" > union< / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > )) {
< a name = "l00138" > < / a > 00138 < span class = "comment" > /* Gets the whole block even if it is slightly bigger than the< / span >
< a name = "l00139" > < / a > 00139 < span class = "comment" > requested size because the fragment would be too small to be< / span >
< a name = "l00140" > < / a > 00140 < span class = "comment" > useful.*/< / span >
< a name = "l00141" > < / a > 00141 qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next;
< a name = "l00142" > < / a > 00142 }
< a name = "l00143" > < / a > 00143 < span class = "keywordflow" > else< / span > {
< a name = "l00144" > < / a > 00144 < span class = "comment" > /* Block bigger enough, must split it.*/< / span >
< a name = "l00145" > < / a > 00145 fp = (< span class = "keywordtype" > void< / span > *)((< a class = "code" href = "group___s_t_m8___c_o_r_e.html#gaba7bc1797add20fe3efdf37ced1182c5" > uint8_t< / a > *)(hp) + < span class = "keyword" > sizeof< / span > (< span class = "keyword" > union< / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > ) + size);
< a name = "l00146" > < / a > 00146 fp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next;
< a name = "l00147" > < / a > 00147 fp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > = hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > - < span class = "keyword" > sizeof< / span > (< span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > ) - size;
< a name = "l00148" > < / a > 00148 qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = fp;
< a name = "l00149" > < / a > 00149 hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > = size;
< a name = "l00150" > < / a > 00150 }
< a name = "l00151" > < / a > 00151 hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .heap = heapp;
< a name = "l00152" > < / a > 00152
< a name = "l00153" > < / a > 00153 H_UNLOCK(heapp);
< a name = "l00154" > < / a > 00154 < span class = "keywordflow" > return< / span > (< span class = "keywordtype" > void< / span > *)(hp + 1);
< a name = "l00155" > < / a > 00155 }
< a name = "l00156" > < / a > 00156 qp = hp;
< a name = "l00157" > < / a > 00157 }
< a name = "l00158" > < / a > 00158
< a name = "l00159" > < / a > 00159 H_UNLOCK(heapp);
< a name = "l00160" > < / a > 00160
< a name = "l00161" > < / a > 00161 < span class = "comment" > /* More memory is required, tries to get it from the associated provider< / span >
< a name = "l00162" > < / a > 00162 < span class = "comment" > else fails.*/< / span >
< a name = "l00163" > < / a > 00163 < span class = "keywordflow" > if< / span > (heapp-> < a class = "code" href = "structmemory__heap.html#a4cad69cd924f2de313510634d2f6de1e" title = "Memory blocks provider for this heap." > h_provider< / a > ) {
< a name = "l00164" > < / a > 00164 hp = heapp-> < a class = "code" href = "structmemory__heap.html#a4cad69cd924f2de313510634d2f6de1e" title = "Memory blocks provider for this heap." > h_provider< / a > (size + < span class = "keyword" > sizeof< / span > (< span class = "keyword" > union< / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > ));
< a name = "l00165" > < / a > 00165 < span class = "keywordflow" > if< / span > (hp != NULL) {
< a name = "l00166" > < / a > 00166 hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .heap = heapp;
< a name = "l00167" > < / a > 00167 hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > = size;
< a name = "l00168" > < / a > 00168 hp++;
< a name = "l00169" > < / a > 00169 < span class = "keywordflow" > return< / span > (< span class = "keywordtype" > void< / span > *)hp;
< a name = "l00170" > < / a > 00170 }
< a name = "l00171" > < / a > 00171 }
< a name = "l00172" > < / a > 00172 < span class = "keywordflow" > return< / span > NULL;
< a name = "l00173" > < / a > 00173 }
< a name = "l00174" > < / a > 00174
< a name = "l00175" > < / a > 00175 < span class = "preprocessor" > #define LIMIT(p) (union heap_header *)((uint8_t *)(p) + \< / span >
< a name = "l00176" > < / a > 00176 < span class = "preprocessor" > sizeof(union heap_header) + \< / span >
< a name = "l00177" > < / a > 00177 < span class = "preprocessor" > (p)-> h.size)< / span >
< a name = "l00178" > < / a > 00178 < span class = "preprocessor" > < / span > < span class = "comment" > < / span >
< a name = "l00179" > < / a > 00179 < span class = "comment" > /**< / span >
< a name = "l00180" > < / a > 00180 < span class = "comment" > * @brief Frees a previously allocated memory block.< / span >
< a name = "l00181" > < / a > 00181 < span class = "comment" > *< / span >
< a name = "l00182" > < / a > 00182 < span class = "comment" > * @param[in] p pointer to the memory block to be freed< / span >
< a name = "l00183" > < / a > 00183 < span class = "comment" > */< / span >
< a name = "l00184" > < / a > < a class = "code" href = "group__heaps.html#ga0fc553df0138f02e0b233be91e413c79" > 00184< / a > < span class = "keywordtype" > void< / span > < a class = "code" href = "group__heaps.html#ga0fc553df0138f02e0b233be91e413c79" title = "Frees a previously allocated memory block." > chHeapFree< / a > (< span class = "keywordtype" > void< / span > *p) {
< a name = "l00185" > < / a > 00185 < span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > *qp, *hp;
< a name = "l00186" > < / a > 00186 < a class = "code" href = "structmemory__heap.html" title = "Structure describing a memory heap." > MemoryHeap< / a > *heapp;
< a name = "l00187" > < / a > 00187
< a name = "l00188" > < / a > 00188 < a class = "code" href = "group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title = "Function parameter check." > chDbgCheck< / a > (p != NULL, < span class = "stringliteral" > " chHeapFree" < / span > );
< a name = "l00189" > < / a > 00189
< a name = "l00190" > < / a > 00190 hp = (< span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > *)p - 1;
< a name = "l00191" > < / a > 00191 heapp = hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .heap;
< a name = "l00192" > < / a > 00192 qp = & heapp-> < a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > ;
< a name = "l00193" > < / a > 00193 H_LOCK(heapp);
< a name = "l00194" > < / a > 00194
< a name = "l00195" > < / a > 00195 < span class = "keywordflow" > while< / span > (TRUE) {
< a name = "l00196" > < / a > 00196 < a class = "code" href = "group__debug.html#ga43d50c69eb730d0f024eb832d61f30c9" title = "Condition assertion." > chDbgAssert< / a > ((hp < qp) || (hp > = LIMIT(qp)),
< a name = "l00197" > < / a > 00197 < span class = "stringliteral" > " chHeapFree(), #1" < / span > ,
< a name = "l00198" > < / a > 00198 < span class = "stringliteral" > " within free block" < / span > );
< a name = "l00199" > < / a > 00199
< a name = "l00200" > < / a > 00200 < span class = "keywordflow" > if< / span > (((qp == & heapp-> < a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > ) || (hp > qp)) & &
< a name = "l00201" > < / a > 00201 ((qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next == NULL) || (hp < qp-> h.u.next))) {
< a name = "l00202" > < / a > 00202 < span class = "comment" > /* Insertion after qp.*/< / span >
< a name = "l00203" > < / a > 00203 hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next;
< a name = "l00204" > < / a > 00204 qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = hp;
< a name = "l00205" > < / a > 00205 < span class = "comment" > /* Verifies if the newly inserted block should be merged.*/< / span >
< a name = "l00206" > < / a > 00206 < span class = "keywordflow" > if< / span > (LIMIT(hp) == hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next) {
< a name = "l00207" > < / a > 00207 < span class = "comment" > /* Merge with the next block.*/< / span >
< a name = "l00208" > < / a > 00208 hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > += hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next-> h.size + < span class = "keyword" > sizeof< / span > (< span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > );
< a name = "l00209" > < / a > 00209 hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next-> h.u.next;
< a name = "l00210" > < / a > 00210 }
< a name = "l00211" > < / a > 00211 < span class = "keywordflow" > if< / span > ((LIMIT(qp) == hp)) {
< a name = "l00212" > < / a > 00212 < span class = "comment" > /* Merge with the previous block.*/< / span >
< a name = "l00213" > < / a > 00213 qp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > += hp-> h.< a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > + < span class = "keyword" > sizeof< / span > (< span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > );
< a name = "l00214" > < / a > 00214 qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next = hp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next;
< a name = "l00215" > < / a > 00215 }
< a name = "l00216" > < / a > 00216 < span class = "keywordflow" > break< / span > ;
< a name = "l00217" > < / a > 00217 }
< a name = "l00218" > < / a > 00218 qp = qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next;
< a name = "l00219" > < / a > 00219 }
< a name = "l00220" > < / a > 00220
< a name = "l00221" > < / a > 00221 H_UNLOCK(heapp);
< a name = "l00222" > < / a > 00222 < span class = "keywordflow" > return< / span > ;
< a name = "l00223" > < / a > 00223 }
< a name = "l00224" > < / a > 00224 < span class = "comment" > < / span >
< a name = "l00225" > < / a > 00225 < span class = "comment" > /**< / span >
< a name = "l00226" > < / a > 00226 < span class = "comment" > * @brief Reports the heap status.< / span >
< a name = "l00227" > < / a > 00227 < span class = "comment" > * @note This function is meant to be used in the test suite, it should< / span >
< a name = "l00228" > < / a > 00228 < span class = "comment" > * not be really useful for the application code.< / span >
< a name = "l00229" > < / a > 00229 < span class = "comment" > * @note This function is not implemented when the @p CH_USE_MALLOC_HEAP< / span >
< a name = "l00230" > < / a > 00230 < span class = "comment" > * configuration option is used (it always returns zero).< / span >
< a name = "l00231" > < / a > 00231 < span class = "comment" > *< / span >
< a name = "l00232" > < / a > 00232 < span class = "comment" > * @param[in] heapp pointer to a heap descriptor or @p NULL in order to< / span >
< a name = "l00233" > < / a > 00233 < span class = "comment" > * access the default heap.< / span >
< a name = "l00234" > < / a > 00234 < span class = "comment" > * @param[in] sizep pointer to a variable that will receive the total< / span >
< a name = "l00235" > < / a > 00235 < span class = "comment" > * fragmented free space< / span >
< a name = "l00236" > < / a > 00236 < span class = "comment" > * @return The number of fragments in the heap.< / span >
< a name = "l00237" > < / a > 00237 < span class = "comment" > */< / span >
< a name = "l00238" > < / a > < a class = "code" href = "group__heaps.html#ga944ffb8c5b83c26b984e6a072d871a8b" > 00238< / a > < span class = "keywordtype" > size_t< / span > < a class = "code" href = "group__heaps.html#ga944ffb8c5b83c26b984e6a072d871a8b" title = "Reports the heap status." > chHeapStatus< / a > (< a class = "code" href = "structmemory__heap.html" title = "Structure describing a memory heap." > MemoryHeap< / a > *heapp, < span class = "keywordtype" > size_t< / span > *sizep) {
< a name = "l00239" > < / a > 00239 < span class = "keyword" > union < / span > < a class = "code" href = "unionheap__header.html" title = "Memory heap block header." > heap_header< / a > *qp;
< a name = "l00240" > < / a > 00240 < span class = "keywordtype" > size_t< / span > n, sz;
< a name = "l00241" > < / a > 00241
< a name = "l00242" > < / a > 00242 < span class = "keywordflow" > if< / span > (heapp == NULL)
< a name = "l00243" > < / a > 00243 heapp = & default_heap;
< a name = "l00244" > < / a > 00244
< a name = "l00245" > < / a > 00245 H_LOCK(heapp);
< a name = "l00246" > < / a > 00246
< a name = "l00247" > < / a > 00247 sz = 0;
< a name = "l00248" > < / a > 00248 < span class = "keywordflow" > for< / span > (n = 0, qp = & heapp-> < a class = "code" href = "structmemory__heap.html#a77ba7416411ebd49578c71bca15d19ae" title = "Free blocks list header." > h_free< / a > ; qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next; n++, qp = qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next)
< a name = "l00249" > < / a > 00249 sz += qp-> h.< a class = "code" href = "unionheap__header.html#a5bf3372839178fcc710e37d64f561e06" title = "Overlapped fields." > u< / a > .next-> h.size;
< a name = "l00250" > < / a > 00250 < span class = "keywordflow" > if< / span > (sizep)
< a name = "l00251" > < / a > 00251 *sizep = sz;
< a name = "l00252" > < / a > 00252
< a name = "l00253" > < / a > 00253 H_UNLOCK(heapp);
< a name = "l00254" > < / a > 00254 < span class = "keywordflow" > return< / span > n;
< a name = "l00255" > < / a > 00255 }
< a name = "l00256" > < / a > 00256
< a name = "l00257" > < / a > 00257 < span class = "preprocessor" > #else < / span > < span class = "comment" > /* CH_USE_MALLOC_HEAP */< / span >
< a name = "l00258" > < / a > 00258
< a name = "l00259" > < / a > 00259 < span class = "preprocessor" > #include < stdlib.h> < / span >
< a name = "l00260" > < / a > 00260
< a name = "l00261" > < / a > 00261 < span class = "preprocessor" > #if CH_USE_MUTEXES< / span >
< a name = "l00262" > < / a > 00262 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_LOCK() chMtxLock(& hmtx)< / span >
< a name = "l00263" > < / a > 00263 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_UNLOCK() chMtxUnlock()< / span >
< a name = "l00264" > < / a > 00264 < span class = "preprocessor" > < / span > < span class = "keyword" > static< / span > < a class = "code" href = "struct_mutex.html" title = "Mutex structure." > Mutex< / a > hmtx;
< a name = "l00265" > < / a > 00265 < span class = "preprocessor" > #elif CH_USE_SEMAPHORES< / span >
< a name = "l00266" > < / a > 00266 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_LOCK() chSemWait(& hsem)< / span >
< a name = "l00267" > < / a > 00267 < span class = "preprocessor" > < / span > < span class = "preprocessor" > #define H_UNLOCK() chSemSignal(& hsem)< / span >
< a name = "l00268" > < / a > 00268 < span class = "preprocessor" > < / span > < span class = "keyword" > static< / span > < a class = "code" href = "struct_semaphore.html" title = "Semaphore structure." > Semaphore< / a > hsem;
< a name = "l00269" > < / a > 00269 < span class = "preprocessor" > #endif< / span >
< a name = "l00270" > < / a > 00270 < span class = "preprocessor" > < / span >
< a name = "l00271" > < / a > 00271 < span class = "keywordtype" > void< / span > < a class = "code" href = "group__heaps.html#ga7e7a6625a49b6a560a47ae7575575264" title = "Initializes the default heap." > heap_init< / a > (< span class = "keywordtype" > void< / span > ) {
< a name = "l00272" > < / a > 00272
< a name = "l00273" > < / a > 00273 < span class = "preprocessor" > #if CH_USE_MUTEXES< / span >
< a name = "l00274" > < / a > 00274 < span class = "preprocessor" > < / span > < a class = "code" href = "group__mutexes.html#gac55fe4d05aa6bbeb31594193d2e4d2b6" title = "Initializes s Mutex structure." > chMtxInit< / a > (& hmtx);
< a name = "l00275" > < / a > 00275 < span class = "preprocessor" > #else< / span >
< a name = "l00276" > < / a > 00276 < span class = "preprocessor" > < / span > < a class = "code" href = "group__semaphores.html#gafe8fc6155a871074e8017efd908b2c58" title = "Initializes a semaphore with the specified counter value." > chSemInit< / a > (& hsem, 1);
< a name = "l00277" > < / a > 00277 < span class = "preprocessor" > #endif< / span >
< a name = "l00278" > < / a > 00278 < span class = "preprocessor" > < / span > }
< a name = "l00279" > < / a > 00279
< a name = "l00280" > < / a > 00280 < span class = "keywordtype" > void< / span > *< a class = "code" href = "group__heaps.html#ga3d364eec0bef8b1986f63b3288ebbd5f" title = "Allocates a block of memory from the heap by using the first-fit algorithm." > chHeapAlloc< / a > (< a class = "code" href = "structmemory__heap.html" title = "Structure describing a memory heap." > MemoryHeap< / a > *heapp, < span class = "keywordtype" > size_t< / span > < a class = "code" href = "unionheap__header.html#a591d8bde47aea2ab624cdde2cf2587ac" title = "Size of the memory block." > size< / a > ) {
< a name = "l00281" > < / a > 00281 < span class = "keywordtype" > void< / span > *p;
< a name = "l00282" > < / a > 00282
< a name = "l00283" > < / a > 00283 < a class = "code" href = "group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title = "Function parameter check." > chDbgCheck< / a > (heapp == NULL, < span class = "stringliteral" > " chHeapAlloc" < / span > );
< a name = "l00284" > < / a > 00284
< a name = "l00285" > < / a > 00285 H_LOCK();
< a name = "l00286" > < / a > 00286 p = malloc(size);
< a name = "l00287" > < / a > 00287 H_UNLOCK();
< a name = "l00288" > < / a > 00288 < span class = "keywordflow" > return< / span > p;
< a name = "l00289" > < / a > 00289 }
< a name = "l00290" > < / a > 00290
< a name = "l00291" > < / a > 00291 < span class = "keywordtype" > void< / span > < a class = "code" href = "group__heaps.html#ga0fc553df0138f02e0b233be91e413c79" title = "Frees a previously allocated memory block." > chHeapFree< / a > (< span class = "keywordtype" > void< / span > *p) {
< a name = "l00292" > < / a > 00292
< a name = "l00293" > < / a > 00293 < a class = "code" href = "group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title = "Function parameter check." > chDbgCheck< / a > (p != NULL, < span class = "stringliteral" > " chHeapFree" < / span > );
< a name = "l00294" > < / a > 00294
< a name = "l00295" > < / a > 00295 H_LOCK();
< a name = "l00296" > < / a > 00296 free(p);
< a name = "l00297" > < / a > 00297 H_UNLOCK();
< a name = "l00298" > < / a > 00298 }
< a name = "l00299" > < / a > 00299
< a name = "l00300" > < / a > 00300 < span class = "keywordtype" > size_t< / span > < a class = "code" href = "group__heaps.html#ga944ffb8c5b83c26b984e6a072d871a8b" title = "Reports the heap status." > chHeapStatus< / a > (< a class = "code" href = "structmemory__heap.html" title = "Structure describing a memory heap." > MemoryHeap< / a > *heapp, < span class = "keywordtype" > size_t< / span > *sizep) {
< a name = "l00301" > < / a > 00301
< a name = "l00302" > < / a > 00302 < a class = "code" href = "group__debug.html#ga6bb6c0f97caab3a66332c8bfbf7a3844" title = "Function parameter check." > chDbgCheck< / a > (heapp == NULL, < span class = "stringliteral" > " chHeapStatus" < / span > );
< a name = "l00303" > < / a > 00303
< a name = "l00304" > < / a > 00304 < span class = "keywordflow" > if< / span > (sizep)
< a name = "l00305" > < / a > 00305 *sizep = 0;
< a name = "l00306" > < / a > 00306 < span class = "keywordflow" > return< / span > 0;
< a name = "l00307" > < / a > 00307 }
< a name = "l00308" > < / a > 00308
< a name = "l00309" > < / a > 00309 < span class = "preprocessor" > #endif < / span > < span class = "comment" > /* CH_USE_MALLOC_HEAP */< / span >
< a name = "l00310" > < / a > 00310
< a name = "l00311" > < / a > 00311 < span class = "preprocessor" > #endif < / span > < span class = "comment" > /* CH_USE_HEAP */< / span >
< a name = "l00312" > < / a > 00312 < span class = "comment" > < / span >
< a name = "l00313" > < / a > 00313 < span class = "comment" > /** @} */< / span >
< / pre > < / div > < / div >
2010-11-22 05:53:37 +00:00
< / div >
2010-08-10 03:11:02 +00:00
< hr size = "1" > < address style = "text-align: right;" > < small >
2010-11-22 05:53:37 +00:00
Generated on Sun Oct 24 2010 09:40:37 for ChibiOS/RT by < a href = "http://www.doxygen.org/index.html" > < img src = "doxygen.png" alt = "doxygen" align = "middle" border = "0" > < / a > 1.7.1< / small > < / address >
2010-08-10 03:11:02 +00:00
< / body >
< / html >