1 /* Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of Freescale Semiconductor nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
16 * ALTERNATIVELY, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") as published by the Free Software
18 * Foundation, either version 2 of that License or (at your option) any
21 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /**************************************************************************//**
37 @Description External prototypes for the memory manager object
38 *//***************************************************************************/
47 /**************************************************************************//**
48 @Group etc_id Utility Library Application Programming Interface
50 @Description External routines.
53 *//***************************************************************************/
55 /**************************************************************************//**
56 @Group mem_id Slab Memory Manager
58 @Description Slab Memory Manager module functions, definitions and enums.
61 *//***************************************************************************/
63 /* Each block is of the following structure:
66 * +-----------+----------+---------------------------+-----------+-----------+
67 * | Alignment | Prefix | Data | Postfix | Alignment |
68 * | field | field | field | field | Padding |
70 * +-----------+----------+---------------------------+-----------+-----------+
71 * and at the beginning of all bytes, an additional optional padding might reside
72 * to ensure that the first blocks data field is aligned as requested.
76 #define MEM_MAX_NAME_LENGTH 8
78 /**************************************************************************//*
79 @Description Memory Segment structure
80 *//***************************************************************************/
84 char name[MEM_MAX_NAME_LENGTH];
85 /* The segment's name */
86 uint8_t **p_Bases; /* Base addresses of the segments */
87 uint8_t **p_BlocksStack; /* Array of pointers to blocks */
89 uint16_t dataSize; /* Size of each data block */
90 uint16_t prefixSize; /* How many bytes to reserve before the data */
91 uint16_t postfixSize; /* How many bytes to reserve after the data */
92 uint16_t alignment; /* Requested alignment for the data field */
93 int allocOwner; /* Memory allocation owner */
94 uint32_t getFailures; /* Number of times get failed */
95 uint32_t num; /* Number of blocks in segment */
96 uint32_t current; /* Current block */
97 bool consecutiveMem; /* Allocate consecutive data blocks memory */
98 #ifdef DEBUG_MEM_LEAKS
99 void *p_MemDbg; /* MEM debug database (MEM leaks detection) */
100 uint32_t blockOffset;
102 #endif /* DEBUG_MEM_LEAKS */
107 /**************************************************************************//**
110 @Description Create a new memory segment.
112 @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).
120 @Return E_OK - success, E_NO_MEMORY - out of memory.
121 *//***************************************************************************/
122 t_Error MEM_Init(char name[],
127 uint16_t postfixSize,
130 /**************************************************************************//**
131 @Function MEM_InitSmart
133 @Description Create a new memory segment.
135 @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 blocks
146 @Return E_OK - success, E_NO_MEMORY - out of memory.
147 *//***************************************************************************/
148 t_Error MEM_InitSmart(char name[],
153 uint16_t postfixSize,
155 uint8_t memPartitionId,
156 bool consecutiveMem);
158 /**************************************************************************//**
159 @Function MEM_InitByAddress
161 @Description Create a new memory segment with a specified base address.
163 @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.
172 @Return E_OK - success, E_NO_MEMORY - out of memory.
173 *//***************************************************************************/
174 t_Error MEM_InitByAddress(char name[],
179 uint16_t postfixSize,
183 /**************************************************************************//**
186 @Description Free a specific memory segment.
188 @Param[in] h_Mem - Handle to memory segment.
191 *//***************************************************************************/
192 void MEM_Free(t_Handle h_Mem);
194 /**************************************************************************//**
197 @Description Get a block of memory from a segment.
199 @Param[in] h_Mem - Handle to memory segment.
201 @Return Pointer to new memory block on success,0 otherwise.
202 *//***************************************************************************/
203 void * MEM_Get(t_Handle h_Mem);
205 /**************************************************************************//**
208 @Description Get up to N blocks of memory from a segment.
210 The blocks are assumed to be of a fixed size (one size per segment).
212 @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 addresses
215 of the allocated blocks are written.
217 @Return The number of blocks actually allocated.
219 @Cautions Interrupts are disabled for all of the allocation loop.
220 Although this loop is very short for each block (several machine
221 instructions), you should not allocate a very large number
222 of blocks via this routine.
223 *//***************************************************************************/
224 uint16_t MEM_GetN(t_Handle h_Mem, uint32_t num, void *array[]);
226 /**************************************************************************//**
229 @Description Put a block of memory back to a segment.
231 @Param[in] h_Mem - Handle to memory segment.
232 @Param[in] p_Block - The block to return.
234 @Return Pointer to new memory block on success,0 otherwise.
235 *//***************************************************************************/
236 t_Error MEM_Put(t_Handle h_Mem, void *p_Block);
238 /**************************************************************************//**
239 @Function MEM_ComputePartitionSize
241 @Description calculate a tight upper boundary of the size of a partition with
244 The returned value is suitable if one wants to use MEM_InitByAddress().
246 @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 size
249 @Param postfixSize - The postfix size
250 @Param[in] alignment - The requested alignment value (in bytes)
252 @Return The memory block size a segment with the given attributes needs.
253 *//***************************************************************************/
254 uint32_t MEM_ComputePartitionSize(uint32_t num,
257 uint16_t postfixSize,
260 #ifdef DEBUG_MEM_LEAKS
261 #if !(defined(__MWERKS__) && (__dest_os == __ppc_eabi))
262 #error "Memory-Leaks-Debug option is supported only for freescale CodeWarrior"
263 #endif /* !(defined(__MWERKS__) && ... */
265 /**************************************************************************//**
266 @Function MEM_CheckLeaks
268 @Description Report MEM object leaks.
270 This routine is automatically called by the MEM_Free() routine,
271 but it can also be invoked while the MEM object is alive.
273 @Param[in] h_Mem - Handle to memory segment.
276 *//***************************************************************************/
277 void MEM_CheckLeaks(t_Handle h_Mem);
279 #else /* not DEBUG_MEM_LEAKS */
280 #define MEM_CheckLeaks(h_Mem)
281 #endif /* not DEBUG_MEM_LEAKS */
283 /**************************************************************************//**
284 @Description Get base of MEM
285 *//***************************************************************************/
286 #define MEM_GetBase(h_Mem) ((t_MemorySegment *)(h_Mem))->p_Bases[0]
288 /**************************************************************************//**
289 @Description Get size of MEM block
290 *//***************************************************************************/
291 #define MEM_GetSize(h_Mem) ((t_MemorySegment *)(h_Mem))->dataSize
293 /**************************************************************************//**
294 @Description Get prefix size of MEM block
295 *//***************************************************************************/
296 #define MEM_GetPrefixSize(h_Mem) ((t_MemorySegment *)(h_Mem))->prefixSize
298 /**************************************************************************//**
299 @Description Get postfix size of MEM block
300 *//***************************************************************************/
301 #define MEM_GetPostfixSize(h_Mem) ((t_MemorySegment *)(h_Mem))->postfixSize
303 /**************************************************************************//**
304 @Description Get alignment of MEM block (in bytes)
305 *//***************************************************************************/
306 #define MEM_GetAlignment(h_Mem) ((t_MemorySegment *)(h_Mem))->alignment
308 /**************************************************************************//**
309 @Description Get the number of blocks in the segment
310 *//***************************************************************************/
311 #define MEM_GetNumOfBlocks(h_Mem) ((t_MemorySegment *)(h_Mem))->num
313 /** @} */ /* end of MEM group */
314 /** @} */ /* end of etc_id group */
317 #endif /* __MEM_EXT_H */