1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: Interface functions for Dynamic Memory Management System
7 *----------------------------------------------------------------------------
9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
10 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * - Neither the name of ARM nor the names of its contributors may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *---------------------------------------------------------------------------*/
35 #include "rt_TypeDef.h"
36 #include "rt_Memory.h"
41 // Initialize Dynamic Memory pool
43 // pool: Pointer to memory pool
44 // size: Size of memory pool in bytes
45 // Return: 0 - OK, 1 - Error
47 int rt_init_mem (void *pool, U32 size) {
50 if ((pool == NULL) || (size < sizeof(MEMP))) return (1);
53 ptr->next = (MEMP *)((U32)pool + size - sizeof(MEMP *));
54 ptr->next->next = NULL;
60 // Allocate Memory from Memory pool
62 // pool: Pointer to memory pool
63 // size: Size of memory in bytes to allocate
64 // Return: Pointer to allocated memory
66 void *rt_alloc_mem (void *pool, U32 size) {
67 MEMP *p, *p_search, *p_new;
70 if ((pool == NULL) || (size == 0)) return NULL;
72 /* Add header offset to 'size' */
74 /* Make sure that block is 4-byte aligned */
75 size = (size + 3) & ~3;
77 p_search = (MEMP *)pool;
79 hole_size = (U32)p_search->next - (U32)p_search;
80 hole_size -= p_search->len;
81 /* Check if hole size is big enough */
82 if (hole_size >= size) break;
83 p_search = p_search->next;
84 if (p_search->next == NULL) {
85 /* Failed, we are at the end of the list */
90 if (p_search->len == 0) {
91 /* No block is allocated, set the Length of the first element */
93 p = (MEMP *)(((U32)p_search) + sizeof(MEMP));
95 /* Insert new list element into the memory list */
96 p_new = (MEMP *)((U32)p_search + p_search->len);
97 p_new->next = p_search->next;
99 p_search->next = p_new;
100 p = (MEMP *)(((U32)p_new) + sizeof(MEMP));
106 // Free Memory and return it to Memory pool
108 // pool: Pointer to memory pool
109 // mem: Pointer to memory to free
110 // Return: 0 - OK, 1 - Error
112 int rt_free_mem (void *pool, void *mem) {
113 MEMP *p_search, *p_prev, *p_return;
115 if ((pool == NULL) || (mem == NULL)) return (1);
117 p_return = (MEMP *)((U32)mem - sizeof(MEMP));
119 /* Set list header */
121 p_search = (MEMP *)pool;
122 while (p_search != p_return) {
124 p_search = p_search->next;
125 if (p_search == NULL) {
126 /* Valid Memory block not found */
131 if (p_prev == NULL) {
132 /* First block to be released, only set length to 0 */
135 /* Discard block from chain list */
136 p_prev->next = p_search->next;