los_heap.h

浏览该文件的文档。
00001 /*----------------------------------------------------------------------------
00002  * Copyright (c) <2013-2015>, <Huawei Technologies Co., Ltd>
00003  * All rights reserved.
00004  * Redistribution and use in source and binary forms, with or without modification,
00005  * are permitted provided that the following conditions are met:
00006  * 1. Redistributions of source code must retain the above copyright notice, this list of
00007  * conditions and the following disclaimer.
00008  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
00009  * of conditions and the following disclaimer in the documentation and/or other materials
00010  * provided with the distribution.
00011  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
00012  * to endorse or promote products derived from this software without specific prior written
00013  * permission.
00014  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00015  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00016  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
00018  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00019  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00020  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00021  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00022  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00023  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00024  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025  *---------------------------------------------------------------------------*/
00026 /*----------------------------------------------------------------------------
00027  * Notice of Export Control Law
00028  * ===============================================
00029  * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
00030  * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
00031  * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
00032  * applicable export control laws and regulations.
00033  *---------------------------------------------------------------------------*/
00034 
00035 /**@defgroup los_heap Heap
00036  * @ingroup kernel
00037  */
00038 
00039 #ifndef _LOS_HEAP_H
00040 #define _LOS_HEAP_H
00041 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 #include <los_typedef.h>
00047 #include "los_base.h"
00048 #if (LOSCFG_KERNEL_MEM_SLAB == YES)
00049 #include "los_slab.ph"
00050 #endif
00051 
00052 #define IS_ALIGNED(value)                       (0 == (((UINT32)(value)) & ((UINT32)(value - 1))))
00053 #define OS_MEM_ALIGN(value, uwAlign)            (((UINT32)(value) + (UINT32)(uwAlign - 1)) & (~(UINT32)(uwAlign - 1)))
00054 #define OS_MEM_ALIGN_FLAG                       (0x80000000)
00055 #define OS_MEM_SET_ALIGN_FLAG(uwAlign)          (uwAlign = ((uwAlign) | OS_MEM_ALIGN_FLAG))
00056 #define OS_MEM_GET_ALIGN_FLAG(uwAlign)          ((uwAlign) & OS_MEM_ALIGN_FLAG)
00057 #define OS_MEM_GET_ALIGN_GAPSIZE(uwAlign)       ((uwAlign) & (~OS_MEM_ALIGN_FLAG))
00058 
00059 #define RAM_HEAP_SIZE                           ((OS_SYS_MEM_SIZE) &~ 7)
00060 #define RAM_HEAP_START                          (OS_SYS_MEM_ADDR)
00061 
00062 #ifdef CONFIG_DDR_HEAP
00063 #define DDR_HEAP_INIT()                         osHeapInit((VOID *)DDR_HEAP_START, DDR_HEAP_SIZE)
00064 #define DDR_HEAP_ALLOC(sz)                      osHeapAllocAlign((VOID *)DDR_HEAP_START, OS_MEM_ALIGN(sz, DCACHE_LINE_SIZE), DCACHE_LINE_SIZE)
00065 #define DDR_HEAP_FREE(p)                        osHeapFree((VOID *)DDR_HEAP_START, p)
00066 #endif
00067 
00068 struct LOS_HEAP_NODE {
00069 
00070     struct LOS_HEAP_NODE* pstPrev;
00071     UINT32 uwSize:30;
00072     UINT32 uwUsed: 1;
00073     UINT32 uwAlign:1;
00074     UINT8  ucData[];/*lint !e43*/
00075 };
00076 
00077 struct LOS_HEAP_MANAGER{
00078     struct LOS_HEAP_NODE *pstHead;
00079     struct LOS_HEAP_NODE *pstTail;
00080     UINT32 uwSize;
00081 #if (LOSCFG_MEM_MUL_POOL == YES)
00082     VOID *pNextPool;
00083 #endif
00084 #if (LOSCFG_KERNEL_MEM_SLAB == YES)
00085     struct LOS_SLAB_CONTROL_HEADER stSlabCtrlHdr;
00086 #endif
00087 };
00088 
00089 /**
00090  *@ingroup los_heap
00091  *@brief Initialization heap memory.
00092  *
00093  *@par Description:
00094  *This API is used to initialization heap memory.
00095  *@attention
00096  *<ul>
00097  *<li>None.</li>
00098  *</ul>
00099  *
00100  *@param pPool   [IN/OUT] A pointer pointed to the memory pool.
00101  *@param uwSz   [IN] Size of heap memory.
00102  *
00103  *@retval TRUE   Initialization success.
00104  *@retval FALSE  Initialization failed.
00105  *@par Dependency:
00106  *<ul><li>los_heap.h: the header file that contains the API declaration.</li></ul>
00107  *@see None.
00108  *@since Huawei LiteOS V100R001C00
00109  */
00110 extern BOOL osHeapInit(VOID *pPool, UINT32 uwSz);
00111 
00112 /**
00113  *@ingroup los_heap
00114  *@brief Alloc memory block from heap memory.
00115  *
00116  *@par Description:
00117  *This API is used to alloc memory block from heap memory.
00118  *@attention
00119  *<ul>
00120  *<li>None.</li>
00121  *</ul>
00122  *
00123  *@param pPool   [IN/OUT] A pointer pointed to the memory pool.
00124  *@param uwSz   [IN] Size of heap memory.
00125  *
00126  *@retval VOID*
00127  *@par Dependency:
00128  *<ul><li>los_heap.h: the header file that contains the API declaration.</li></ul>
00129  *@see osHeapFree
00130  *@since Huawei LiteOS V100R001C00
00131  */
00132 extern VOID* osHeapAlloc(VOID *pPool, UINT32 uwSz);
00133 
00134 /**
00135  *@ingroup los_heap
00136  *@brief Alloc aligned memory block from heap memory.
00137  *
00138  *@par Description:
00139  *This API is used to alloc aligned  memory block from heap memory.
00140  *@attention
00141  *<ul>
00142  *<li>None.</li>
00143  *</ul>
00144  *
00145  *@param pPool   [IN/OUT] A pointer pointed to the memory pool.
00146  *@param uwSz   [IN] Size of heap memory.
00147  *@param uwBoundary   [IN] Boundary the heap needs align
00148  *
00149  *@retval VOID*
00150  *@par Dependency:
00151  *<ul><li>los_heap.h: the header file that contains the API declaration.</li></ul>
00152  *@see osHeapFree
00153  *@since Huawei LiteOS V100R001C00
00154  */
00155 extern VOID* osHeapAllocAlign(VOID *pPool, UINT32 uwSz, UINT32 uwBoundary);
00156 
00157 /**
00158  *@ingroup los_heap
00159  *@brief Free memory block from heap memory.
00160  *
00161  *@par Description:
00162  *This API is used to free memory block from heap memory.
00163  *@attention
00164  *<ul>
00165  *<li>None.</li>
00166  *</ul>
00167  *
00168  *@param pPool   [IN/OUT] A pointer pointed to the memory pool.
00169  *@param pPtr    [IN] Point to be freed.
00170  *
00171  *@retval BOOL TRUE  free success  FALSE  free failed
00172  *@par Dependency:
00173  *<ul><li>los_heap.h: the header file that contains the API declaration.</li></ul>
00174  *@see osHeapAlloc
00175  *@since Huawei LiteOS V100R001C00
00176  */
00177 extern BOOL osHeapFree(VOID *pPool, VOID* pPtr);
00178 
00179 /**
00180  *@ingroup los_memory
00181  *@brief Get the memory info from Heap.
00182  *
00183  *@par Description:
00184  *This API is used to get the memory info from Heap.
00185  *@attention
00186  *<ul>
00187  *<li>None.</li>
00188  *</ul>
00189  *@param None.
00190  *
00191  *@retval   UINT32  Max size of heap memory being used.
00192  *
00193  *@par Dependency:
00194  *<ul><li>los_heap.h: the header file that contains the API declaration.</li></ul>
00195  *@see None.
00196  *@since Huawei LiteOS
00197  */
00198 #if (LOSCFG_HEAP_MEMORY_PEAK_STATISTICS == YES)
00199 extern UINT32 osHeapGetHeapMemoryPeak(VOID);
00200 #endif
00201 
00202 #ifdef __cplusplus
00203 }
00204 #endif
00205 
00206 
00207 #endif
00208