]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_M/rt_MemBox.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_M / rt_MemBox.c
1 /*----------------------------------------------------------------------------
2  *      RL-ARM - RTX
3  *----------------------------------------------------------------------------
4  *      Name:    RT_MEMBOX.C
5  *      Purpose: Interface functions for fixed memory block 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 "RTX_Conf.h"
37 #include "rt_System.h"
38 #include "rt_MemBox.h"
39 #include "rt_HAL_CM.h"
40
41 /*----------------------------------------------------------------------------
42  *      Global Functions
43  *---------------------------------------------------------------------------*/
44
45
46 /*--------------------------- _init_box -------------------------------------*/
47
48 int _init_box  (void *box_mem, U32 box_size, U32 blk_size) {
49   /* Initialize memory block system, returns 0 if OK, 1 if fails. */
50   void *end;
51   void *blk;
52   void *next;
53   U32  sizeof_bm;
54
55   /* Create memory structure. */
56   if (blk_size & BOX_ALIGN_8) {
57     /* Memory blocks 8-byte aligned. */
58     blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7;
59     sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7;
60   }
61   else {
62     /* Memory blocks 4-byte aligned. */
63     blk_size = (blk_size + 3) & ~3;
64     sizeof_bm = sizeof (struct OS_BM);
65   }
66   if (blk_size == 0) {
67     return (1);
68   }
69   if ((blk_size + sizeof_bm) > box_size) {
70     return (1);
71   }
72   /* Create a Memory structure. */
73   blk = ((U8 *) box_mem) + sizeof_bm;
74   ((P_BM) box_mem)->free = blk;
75   end = ((U8 *) box_mem) + box_size;
76   ((P_BM) box_mem)->end      = end;
77   ((P_BM) box_mem)->blk_size = blk_size;
78
79   /* Link all free blocks using offsets. */
80   end = ((U8 *) end) - blk_size;
81   while (1)  {
82     next = ((U8 *) blk) + blk_size;
83     if (next > end)  break;
84     *((void **)blk) = next;
85     blk = next;
86   }
87   /* end marker */
88   *((void **)blk) = 0;
89   return (0);
90 }
91
92 /*--------------------------- rt_alloc_box ----------------------------------*/
93
94 void *rt_alloc_box (void *box_mem) {
95   /* Allocate a memory block and return start address. */
96   void **free;
97 #ifndef __USE_EXCLUSIVE_ACCESS
98   int  irq_dis;
99
100   irq_dis = __disable_irq ();
101   free = ((P_BM) box_mem)->free;
102   if (free) {
103     ((P_BM) box_mem)->free = *free;
104   }
105   if (!irq_dis) __enable_irq ();
106 #else
107   do {
108     if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) {
109       __clrex();
110       break;
111     }
112   } while (__strex((U32)*free, &((P_BM) box_mem)->free));
113 #endif
114   return (free);
115 }
116
117
118 /*--------------------------- _calloc_box -----------------------------------*/
119
120 void *_calloc_box (void *box_mem)  {
121   /* Allocate a 0-initialized memory block and return start address. */
122   void *free;
123   U32 *p;
124   U32 i;
125
126   free = _alloc_box (box_mem);
127   if (free)  {
128     p = free;
129     for (i = ((P_BM) box_mem)->blk_size; i; i -= 4)  {
130       *p = 0;
131       p++;
132     }
133   }
134   return (free);
135 }
136
137
138 /*--------------------------- rt_free_box -----------------------------------*/
139
140 int rt_free_box (void *box_mem, void *box) {
141   /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */
142 #ifndef __USE_EXCLUSIVE_ACCESS
143   int irq_dis;
144 #endif
145
146   if (box < box_mem || box >= ((P_BM) box_mem)->end) {
147     return (1);
148   }
149
150 #ifndef __USE_EXCLUSIVE_ACCESS
151   irq_dis = __disable_irq ();
152   *((void **)box) = ((P_BM) box_mem)->free;
153   ((P_BM) box_mem)->free = box;
154   if (!irq_dis) __enable_irq ();
155 #else
156   do {
157     *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free);
158   } while (__strex ((U32)box, &((P_BM) box_mem)->free));
159 #endif
160   return (0);
161 }
162
163 /*----------------------------------------------------------------------------
164  * end of file
165  *---------------------------------------------------------------------------*/
166