]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_Memory.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_A / rt_Memory.c
1 /*----------------------------------------------------------------------------
2  *      RL-ARM - RTX
3  *----------------------------------------------------------------------------
4  *      Name:    RT_MEMORY.C
5  *      Purpose: Interface functions for Dynamic Memory Management System
6  *      Rev.:    V4.60
7  *----------------------------------------------------------------------------
8  *
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.
21  *
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  *---------------------------------------------------------------------------*/
34
35 #include "rt_TypeDef.h"
36 #include "rt_Memory.h"
37
38
39 /* Functions */
40
41 // Initialize Dynamic Memory pool
42 //   Parameters:
43 //     pool:    Pointer to memory pool
44 //     size:    Size of memory pool in bytes
45 //   Return:    0 - OK, 1 - Error
46
47 int rt_init_mem (void *pool, U32 size) {
48   MEMP *ptr;
49
50   if ((pool == NULL) || (size < sizeof(MEMP))) return (1);
51
52   ptr = (MEMP *)pool;
53   ptr->next = (MEMP *)((U32)pool + size - sizeof(MEMP *));
54   ptr->next->next = NULL;
55   ptr->len = 0;
56
57   return (0);
58 }
59
60 // Allocate Memory from Memory pool
61 //   Parameters:
62 //     pool:    Pointer to memory pool
63 //     size:    Size of memory in bytes to allocate
64 //   Return:    Pointer to allocated memory
65
66 void *rt_alloc_mem (void *pool, U32 size) {
67   MEMP *p, *p_search, *p_new;
68   U32   hole_size;
69
70   if ((pool == NULL) || (size == 0)) return NULL;
71
72   /* Add header offset to 'size' */
73   size += sizeof(MEMP);
74   /* Make sure that block is 4-byte aligned  */
75   size = (size + 3) & ~3;
76
77   p_search = (MEMP *)pool;
78   while (1) {
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 */
86       return NULL;
87     }
88   }
89
90   if (p_search->len == 0) {
91     /* No block is allocated, set the Length of the first element */
92     p_search->len = size;
93     p = (MEMP *)(((U32)p_search) + sizeof(MEMP));
94   } else {
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;
98     p_new->len  = size;
99     p_search->next = p_new;
100     p = (MEMP *)(((U32)p_new) + sizeof(MEMP));
101   }
102
103   return (p);
104 }
105
106 // Free Memory and return it to Memory pool
107 //   Parameters:
108 //     pool:    Pointer to memory pool
109 //     mem:     Pointer to memory to free
110 //   Return:    0 - OK, 1 - Error
111
112 int rt_free_mem (void *pool, void *mem) {
113   MEMP *p_search, *p_prev, *p_return;
114
115   if ((pool == NULL) || (mem == NULL)) return (1);
116
117   p_return = (MEMP *)((U32)mem - sizeof(MEMP));
118
119   /* Set list header */
120   p_prev = NULL;
121   p_search = (MEMP *)pool;
122   while (p_search != p_return) {
123     p_prev   = p_search;
124     p_search = p_search->next;
125     if (p_search == NULL) {
126       /* Valid Memory block not found */
127       return (1);
128     }
129   }
130
131   if (p_prev == NULL) {
132     /* First block to be released, only set length to 0 */
133     p_search->len = 0;
134   } else {
135     /* Discard block from chain list */
136     p_prev->next = p_search->next;
137   }
138
139   return (0);
140 }