Path: blob/main/sys/contrib/ncsw/inc/etc/mem_ext.h
48375 views
/* 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/**************************************************************************//**3435@File mem_ext.h3637@Description External prototypes for the memory manager object38*//***************************************************************************/3940#ifndef __MEM_EXT_H41#define __MEM_EXT_H4243#include "std_ext.h"44#include "part_ext.h"454647/**************************************************************************//**48@Group etc_id Utility Library Application Programming Interface4950@Description External routines.5152@{53*//***************************************************************************/5455/**************************************************************************//**56@Group mem_id Slab Memory Manager5758@Description Slab Memory Manager module functions, definitions and enums.5960@{61*//***************************************************************************/6263/* Each block is of the following structure:64*65*66* +-----------+----------+---------------------------+-----------+-----------+67* | Alignment | Prefix | Data | Postfix | Alignment |68* | field | field | field | field | Padding |69* | | | | | |70* +-----------+----------+---------------------------+-----------+-----------+71* and at the beginning of all bytes, an additional optional padding might reside72* to ensure that the first blocks data field is aligned as requested.73*/747576#define MEM_MAX_NAME_LENGTH 87778/**************************************************************************//*79@Description Memory Segment structure80*//***************************************************************************/8182typedef struct83{84char name[MEM_MAX_NAME_LENGTH];85/* The segment's name */86uint8_t **p_Bases; /* Base addresses of the segments */87uint8_t **p_BlocksStack; /* Array of pointers to blocks */88t_Handle h_Spinlock;89uint16_t dataSize; /* Size of each data block */90uint16_t prefixSize; /* How many bytes to reserve before the data */91uint16_t postfixSize; /* How many bytes to reserve after the data */92uint16_t alignment; /* Requested alignment for the data field */93int allocOwner; /* Memory allocation owner */94uint32_t getFailures; /* Number of times get failed */95uint32_t num; /* Number of blocks in segment */96uint32_t current; /* Current block */97bool consecutiveMem; /* Allocate consecutive data blocks memory */98#ifdef DEBUG_MEM_LEAKS99void *p_MemDbg; /* MEM debug database (MEM leaks detection) */100uint32_t blockOffset;101uint32_t blockSize;102#endif /* DEBUG_MEM_LEAKS */103} t_MemorySegment;104105106107/**************************************************************************//**108@Function MEM_Init109110@Description Create a new memory segment.111112@Param[in] name - Name of memory partition.113@Param[in] p_Handle - Handle to new segment is returned through here.114@Param[in] num - Number of blocks in new segment.115@Param[in] dataSize - Size of blocks in segment.116@Param[in] prefixSize - How many bytes to allocate before the data.117@Param[in] postfixSize - How many bytes to allocate after the data.118@Param[in] alignment - Requested alignment for data field (in bytes).119120@Return E_OK - success, E_NO_MEMORY - out of memory.121*//***************************************************************************/122t_Error MEM_Init(char name[],123t_Handle *p_Handle,124uint32_t num,125uint16_t dataSize,126uint16_t prefixSize,127uint16_t postfixSize,128uint16_t alignment);129130/**************************************************************************//**131@Function MEM_InitSmart132133@Description Create a new memory segment.134135@Param[in] name - Name of memory partition.136@Param[in] p_Handle - Handle to new segment is returned through here.137@Param[in] num - Number of blocks in new segment.138@Param[in] dataSize - Size of blocks in segment.139@Param[in] prefixSize - How many bytes to allocate before the data.140@Param[in] postfixSize - How many bytes to allocate after the data.141@Param[in] alignment - Requested alignment for data field (in bytes).142@Param[in] memPartitionId - Memory partition ID for allocation.143@Param[in] consecutiveMem - Whether to allocate the memory blocks144continuously or not.145146@Return E_OK - success, E_NO_MEMORY - out of memory.147*//***************************************************************************/148t_Error MEM_InitSmart(char name[],149t_Handle *p_Handle,150uint32_t num,151uint16_t dataSize,152uint16_t prefixSize,153uint16_t postfixSize,154uint16_t alignment,155uint8_t memPartitionId,156bool consecutiveMem);157158/**************************************************************************//**159@Function MEM_InitByAddress160161@Description Create a new memory segment with a specified base address.162163@Param[in] name - Name of memory partition.164@Param[in] p_Handle - Handle to new segment is returned through here.165@Param[in] num - Number of blocks in new segment.166@Param[in] dataSize - Size of blocks in segment.167@Param[in] prefixSize - How many bytes to allocate before the data.168@Param[in] postfixSize - How many bytes to allocate after the data.169@Param[in] alignment - Requested alignment for data field (in bytes).170@Param[in] address - The required base address.171172@Return E_OK - success, E_NO_MEMORY - out of memory.173*//***************************************************************************/174t_Error MEM_InitByAddress(char name[],175t_Handle *p_Handle,176uint32_t num,177uint16_t dataSize,178uint16_t prefixSize,179uint16_t postfixSize,180uint16_t alignment,181uint8_t *address);182183/**************************************************************************//**184@Function MEM_Free185186@Description Free a specific memory segment.187188@Param[in] h_Mem - Handle to memory segment.189190@Return None.191*//***************************************************************************/192void MEM_Free(t_Handle h_Mem);193194/**************************************************************************//**195@Function MEM_Get196197@Description Get a block of memory from a segment.198199@Param[in] h_Mem - Handle to memory segment.200201@Return Pointer to new memory block on success,0 otherwise.202*//***************************************************************************/203void * MEM_Get(t_Handle h_Mem);204205/**************************************************************************//**206@Function MEM_GetN207208@Description Get up to N blocks of memory from a segment.209210The blocks are assumed to be of a fixed size (one size per segment).211212@Param[in] h_Mem - Handle to memory segment.213@Param[in] num - Number of blocks to allocate.214@Param[out] array - Array of at least num pointers to which the addresses215of the allocated blocks are written.216217@Return The number of blocks actually allocated.218219@Cautions Interrupts are disabled for all of the allocation loop.220Although this loop is very short for each block (several machine221instructions), you should not allocate a very large number222of blocks via this routine.223*//***************************************************************************/224uint16_t MEM_GetN(t_Handle h_Mem, uint32_t num, void *array[]);225226/**************************************************************************//**227@Function MEM_Put228229@Description Put a block of memory back to a segment.230231@Param[in] h_Mem - Handle to memory segment.232@Param[in] p_Block - The block to return.233234@Return Pointer to new memory block on success,0 otherwise.235*//***************************************************************************/236t_Error MEM_Put(t_Handle h_Mem, void *p_Block);237238/**************************************************************************//**239@Function MEM_ComputePartitionSize240241@Description calculate a tight upper boundary of the size of a partition with242given attributes.243244The returned value is suitable if one wants to use MEM_InitByAddress().245246@Param[in] num - The number of blocks in the segment.247@Param[in] dataSize - Size of block to get.248@Param[in] prefixSize - The prefix size249@Param postfixSize - The postfix size250@Param[in] alignment - The requested alignment value (in bytes)251252@Return The memory block size a segment with the given attributes needs.253*//***************************************************************************/254uint32_t MEM_ComputePartitionSize(uint32_t num,255uint16_t dataSize,256uint16_t prefixSize,257uint16_t postfixSize,258uint16_t alignment);259260#ifdef DEBUG_MEM_LEAKS261#if !((defined(__MWERKS__) || defined(__GNUC__)) && (__dest_os == __ppc_eabi))262#error "Memory-Leaks-Debug option is supported only for freescale CodeWarrior"263#endif /* !(defined(__MWERKS__) && ... */264265/**************************************************************************//**266@Function MEM_CheckLeaks267268@Description Report MEM object leaks.269270This routine is automatically called by the MEM_Free() routine,271but it can also be invoked while the MEM object is alive.272273@Param[in] h_Mem - Handle to memory segment.274275@Return None.276*//***************************************************************************/277void MEM_CheckLeaks(t_Handle h_Mem);278279#else /* not DEBUG_MEM_LEAKS */280#define MEM_CheckLeaks(h_Mem)281#endif /* not DEBUG_MEM_LEAKS */282283/**************************************************************************//**284@Description Get base of MEM285*//***************************************************************************/286#define MEM_GetBase(h_Mem) ((t_MemorySegment *)(h_Mem))->p_Bases[0]287288/**************************************************************************//**289@Description Get size of MEM block290*//***************************************************************************/291#define MEM_GetSize(h_Mem) ((t_MemorySegment *)(h_Mem))->dataSize292293/**************************************************************************//**294@Description Get prefix size of MEM block295*//***************************************************************************/296#define MEM_GetPrefixSize(h_Mem) ((t_MemorySegment *)(h_Mem))->prefixSize297298/**************************************************************************//**299@Description Get postfix size of MEM block300*//***************************************************************************/301#define MEM_GetPostfixSize(h_Mem) ((t_MemorySegment *)(h_Mem))->postfixSize302303/**************************************************************************//**304@Description Get alignment of MEM block (in bytes)305*//***************************************************************************/306#define MEM_GetAlignment(h_Mem) ((t_MemorySegment *)(h_Mem))->alignment307308/**************************************************************************//**309@Description Get the number of blocks in the segment310*//***************************************************************************/311#define MEM_GetNumOfBlocks(h_Mem) ((t_MemorySegment *)(h_Mem))->num312313/** @} */ /* end of MEM group */314/** @} */ /* end of etc_id group */315316317#endif /* __MEM_EXT_H */318319320