/* Copyright (c) 2008-2012 Freescale Semiconductor, Inc1* All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10* * Neither the name of Freescale Semiconductor nor the11* names of its contributors may be used to endorse or promote products12* derived from this software without specific prior written permission.13*14*15* ALTERNATIVELY, this software may be distributed under the terms of the16* GNU General Public License ("GPL") as published by the Free Software17* Foundation, either version 2 of that License or (at your option) any18* later version.19*20* THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY24* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;26* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233/**************************************************************************//**34@File mm_ext.h3536@Description Memory Manager Application Programming Interface37*//***************************************************************************/38#ifndef __MM_EXT39#define __MM_EXT4041#include "std_ext.h"4243#define MM_MAX_ALIGNMENT 20 /* Alignments from 2 to 128 are available44where maximum alignment defined as45MM_MAX_ALIGNMENT power of 2 */4647#define MM_MAX_NAME_LEN 324849/**************************************************************************//**50@Group etc_id Utility Library Application Programming Interface5152@Description External routines.5354@{55*//***************************************************************************/5657/**************************************************************************//**58@Group mm_grp Flexible Memory Manager5960@Description Flexible Memory Manager module functions,definitions and enums.61(All of the following functions,definitions and enums can be found in mm_ext.h)6263@{64*//***************************************************************************/656667/**************************************************************************//**68@Function MM_Init6970@Description Initializes a new MM object.7172It initializes a new memory block consisting of base address73and size of the available memory by calling to MemBlock_Init74routine. It is also initializes a new free block for each75by calling FreeBlock_Init routine, which is pointed to76the almost all memory started from the required alignment77from the base address and to the end of the memory.78The handle to the new MM object is returned via "MM"79argument (passed by reference).8081@Param[in] h_MM - Handle to the MM object.82@Param[in] base - Base address of the MM.83@Param[in] size - Size of the MM.8485@Return E_OK is returned on success. E_NOMEMORY is returned if the new MM object or a new free block can not be initialized.86*//***************************************************************************/87t_Error MM_Init(t_Handle *h_MM, uint64_t base, uint64_t size);8889/**************************************************************************//**90@Function MM_Get9192@Description Allocates a block of memory according to the given size and the alignment.9394The Alignment argument tells from which95free list allocate a block of memory. 2^alignment indicates96the alignment that the base address of the allocated block97should have. So, the only values 1, 2, 4, 8, 16, 32 and 6498are available for the alignment argument.99The routine passes through the specific free list of free100blocks and seeks for a first block that have anough memory101that is required (best fit).102After the block is found and data is allocated, it calls103the internal MM_CutFree routine to update all free lists104do not include a just allocated block. Of course, each105free list contains a free blocks with the same alignment.106It is also creates a busy block that holds107information about an allocated block.108109@Param[in] h_MM - Handle to the MM object.110@Param[in] size - Size of the MM.111@Param[in] alignment - Index as a power of two defines a required112alignment (in bytes); Should be 1, 2, 4, 8, 16, 32 or 64113@Param[in] name - The name that specifies an allocated block.114115@Return base address of an allocated block ILLEGAL_BASE if can't allocate a block116*//***************************************************************************/117uint64_t MM_Get(t_Handle h_MM, uint64_t size, uint64_t alignment, char *name);118119/**************************************************************************//**120@Function MM_GetBase121122@Description Gets the base address of the required MM objects.123124@Param[in] h_MM - Handle to the MM object.125126@Return base address of the block.127*//***************************************************************************/128uint64_t MM_GetBase(t_Handle h_MM);129130/**************************************************************************//**131@Function MM_GetForce132133@Description Force memory allocation.134135It means to allocate a block of memory of the given136size from the given base address.137The routine checks if the required block can be allocated138(that is it is free) and then, calls the internal MM_CutFree139routine to update all free lists do not include that block.140141@Param[in] h_MM - Handle to the MM object.142@Param[in] base - Base address of the MM.143@Param[in] size - Size of the MM.144@Param[in] name - Name that specifies an allocated block.145146@Return base address of an allocated block, ILLEGAL_BASE if can't allocate a block.147*//***************************************************************************/148uint64_t MM_GetForce(t_Handle h_MM, uint64_t base, uint64_t size, char *name);149150/**************************************************************************//**151@Function MM_GetForceMin152153@Description Allocates a block of memory according to the given size, the alignment and minimum base address.154155The Alignment argument tells from which156free list allocate a block of memory. 2^alignment indicates157the alignment that the base address of the allocated block158should have. So, the only values 1, 2, 4, 8, 16, 32 and 64159are available for the alignment argument.160The minimum baser address forces the location of the block161to be from a given address onward.162The routine passes through the specific free list of free163blocks and seeks for the first base address equal or smaller164than the required minimum address and end address larger than165than the required base + its size - i.e. that may contain166the required block.167After the block is found and data is allocated, it calls168the internal MM_CutFree routine to update all free lists169do not include a just allocated block. Of course, each170free list contains a free blocks with the same alignment.171It is also creates a busy block that holds172information about an allocated block.173174@Param[in] h_MM - Handle to the MM object.175@Param[in] size - Size of the MM.176@Param[in] alignment - Index as a power of two defines a required177alignment (in bytes); Should be 1, 2, 4, 8, 16, 32 or 64178@Param[in] min - The minimum base address of the block.179@Param[in] name - Name that specifies an allocated block.180181@Return base address of an allocated block,ILLEGAL_BASE if can't allocate a block.182*//***************************************************************************/183uint64_t MM_GetForceMin(t_Handle h_MM,184uint64_t size,185uint64_t alignment,186uint64_t min,187char *name);188189/**************************************************************************//**190@Function MM_Put191192@Description Puts a block of memory of the given base address back to the memory.193194It checks if there is a busy block with the195given base address. If not, it returns 0, that196means can't free a block. Otherwise, it gets parameters of197the busy block and after it updates lists of free blocks,198removes that busy block from the list by calling to MM_CutBusy199routine.200After that it calls to MM_AddFree routine to add a new free201block to the free lists.202203@Param[in] h_MM - Handle to the MM object.204@Param[in] base - Base address of the MM.205206@Return The size of bytes released, 0 if failed.207*//***************************************************************************/208uint64_t MM_Put(t_Handle h_MM, uint64_t base);209210/**************************************************************************//**211@Function MM_PutForce212213@Description Releases a block of memory of the required size from the required base address.214215First, it calls to MM_CutBusy routine216to cut a free block from the busy list. And then, calls to217MM_AddFree routine to add the free block to the free lists.218219@Param[in] h_MM - Handle to the MM object.220@Param[in] base - Base address of of a block to free.221@Param[in] size - Size of a block to free.222223@Return The number of bytes released, 0 on failure.224*//***************************************************************************/225uint64_t MM_PutForce(t_Handle h_MM, uint64_t base, uint64_t size);226227/**************************************************************************//**228@Function MM_Add229230@Description Adds a new memory block for memory allocation.231232When a new memory block is initialized and added to the233memory list, it calls to MM_AddFree routine to add the234new free block to the free lists.235236@Param[in] h_MM - Handle to the MM object.237@Param[in] base - Base address of the memory block.238@Param[in] size - Size of the memory block.239240@Return E_OK on success, otherwise returns an error code.241*//***************************************************************************/242t_Error MM_Add(t_Handle h_MM, uint64_t base, uint64_t size);243244/**************************************************************************//**245@Function MM_Dump246247@Description Prints results of free and busy lists.248249@Param[in] h_MM - Handle to the MM object.250*//***************************************************************************/251void MM_Dump(t_Handle h_MM);252253/**************************************************************************//**254@Function MM_Free255256@Description Releases memory allocated for MM object.257258@Param[in] h_MM - Handle of the MM object.259*//***************************************************************************/260void MM_Free(t_Handle h_MM);261262/**************************************************************************//**263@Function MM_GetMemBlock264265@Description Returns base address of the memory block specified by the index.266267If index is 0, returns base address268of the first memory block, 1 - returns base address269of the second memory block, etc.270Note, those memory blocks are allocated by the271application before MM_Init or MM_Add and have to272be released by the application before or after invoking273the MM_Free routine.274275@Param[in] h_MM - Handle to the MM object.276@Param[in] index - Index of the memory block.277278@Return valid base address or ILLEGAL_BASE if no memory block specified by the index.279*//***************************************************************************/280uint64_t MM_GetMemBlock(t_Handle h_MM, int index);281282/**************************************************************************//**283@Function MM_InRange284285@Description Checks if a specific address is in the memory range of the passed MM object.286287@Param[in] h_MM - Handle to the MM object.288@Param[in] addr - The address to be checked.289290@Return TRUE if the address is in the address range of the block, FALSE otherwise.291*//***************************************************************************/292bool MM_InRange(t_Handle h_MM, uint64_t addr);293294/**************************************************************************//**295@Function MM_GetFreeMemSize296297@Description Returns the size (in bytes) of free memory.298299@Param[in] h_MM - Handle to the MM object.300301@Return Free memory size in bytes.302*//***************************************************************************/303uint64_t MM_GetFreeMemSize(t_Handle h_MM);304305306/** @} */ /* end of mm_grp group */307/** @} */ /* end of etc_id group */308309#endif /* __MM_EXT_H */310311312