Clone of mbed rtos 2 for easy accessibility.
Fork of mbed-rtos by
rtx/TARGET_CORTEX_M/rt_MemBox.c@49:77c8e4604045, 2014-11-06 (annotated)
- Committer:
- mbed_official
- Date:
- Thu Nov 06 13:00:11 2014 +0000
- Revision:
- 49:77c8e4604045
- Child:
- 112:53ace74b190c
Synchronized with git revision 7b90c2ba137baaf9769219e0e8a7b8e8d1299c4f
Full URL: https://github.com/mbedmicro/mbed/commit/7b90c2ba137baaf9769219e0e8a7b8e8d1299c4f/
This target is not yet tested, so it can't be released as part of the official
SDK build for now.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 49:77c8e4604045 | 1 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 2 | * RL-ARM - RTX |
mbed_official | 49:77c8e4604045 | 3 | *---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 4 | * Name: RT_MEMBOX.C |
mbed_official | 49:77c8e4604045 | 5 | * Purpose: Interface functions for fixed memory block management system |
mbed_official | 49:77c8e4604045 | 6 | * Rev.: V4.60 |
mbed_official | 49:77c8e4604045 | 7 | *---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 8 | * |
mbed_official | 49:77c8e4604045 | 9 | * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH |
mbed_official | 49:77c8e4604045 | 10 | * All rights reserved. |
mbed_official | 49:77c8e4604045 | 11 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 49:77c8e4604045 | 12 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 49:77c8e4604045 | 13 | * - Redistributions of source code must retain the above copyright |
mbed_official | 49:77c8e4604045 | 14 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 49:77c8e4604045 | 15 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 49:77c8e4604045 | 16 | * notice, this list of conditions and the following disclaimer in the |
mbed_official | 49:77c8e4604045 | 17 | * documentation and/or other materials provided with the distribution. |
mbed_official | 49:77c8e4604045 | 18 | * - Neither the name of ARM nor the names of its contributors may be used |
mbed_official | 49:77c8e4604045 | 19 | * to endorse or promote products derived from this software without |
mbed_official | 49:77c8e4604045 | 20 | * specific prior written permission. |
mbed_official | 49:77c8e4604045 | 21 | * |
mbed_official | 49:77c8e4604045 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 49:77c8e4604045 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 49:77c8e4604045 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
mbed_official | 49:77c8e4604045 | 25 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE |
mbed_official | 49:77c8e4604045 | 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
mbed_official | 49:77c8e4604045 | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
mbed_official | 49:77c8e4604045 | 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
mbed_official | 49:77c8e4604045 | 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
mbed_official | 49:77c8e4604045 | 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
mbed_official | 49:77c8e4604045 | 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 49:77c8e4604045 | 32 | * POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 49:77c8e4604045 | 33 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 34 | |
mbed_official | 49:77c8e4604045 | 35 | #include "rt_TypeDef.h" |
mbed_official | 49:77c8e4604045 | 36 | #include "RTX_Conf.h" |
mbed_official | 49:77c8e4604045 | 37 | #include "rt_System.h" |
mbed_official | 49:77c8e4604045 | 38 | #include "rt_MemBox.h" |
mbed_official | 49:77c8e4604045 | 39 | #include "rt_HAL_CM.h" |
mbed_official | 49:77c8e4604045 | 40 | |
mbed_official | 49:77c8e4604045 | 41 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 42 | * Global Functions |
mbed_official | 49:77c8e4604045 | 43 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 44 | |
mbed_official | 49:77c8e4604045 | 45 | |
mbed_official | 49:77c8e4604045 | 46 | /*--------------------------- _init_box -------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 47 | |
mbed_official | 49:77c8e4604045 | 48 | int _init_box (void *box_mem, U32 box_size, U32 blk_size) { |
mbed_official | 49:77c8e4604045 | 49 | /* Initialize memory block system, returns 0 if OK, 1 if fails. */ |
mbed_official | 49:77c8e4604045 | 50 | void *end; |
mbed_official | 49:77c8e4604045 | 51 | void *blk; |
mbed_official | 49:77c8e4604045 | 52 | void *next; |
mbed_official | 49:77c8e4604045 | 53 | U32 sizeof_bm; |
mbed_official | 49:77c8e4604045 | 54 | |
mbed_official | 49:77c8e4604045 | 55 | /* Create memory structure. */ |
mbed_official | 49:77c8e4604045 | 56 | if (blk_size & BOX_ALIGN_8) { |
mbed_official | 49:77c8e4604045 | 57 | /* Memory blocks 8-byte aligned. */ |
mbed_official | 49:77c8e4604045 | 58 | blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7; |
mbed_official | 49:77c8e4604045 | 59 | sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7; |
mbed_official | 49:77c8e4604045 | 60 | } |
mbed_official | 49:77c8e4604045 | 61 | else { |
mbed_official | 49:77c8e4604045 | 62 | /* Memory blocks 4-byte aligned. */ |
mbed_official | 49:77c8e4604045 | 63 | blk_size = (blk_size + 3) & ~3; |
mbed_official | 49:77c8e4604045 | 64 | sizeof_bm = sizeof (struct OS_BM); |
mbed_official | 49:77c8e4604045 | 65 | } |
mbed_official | 49:77c8e4604045 | 66 | if (blk_size == 0) { |
mbed_official | 49:77c8e4604045 | 67 | return (1); |
mbed_official | 49:77c8e4604045 | 68 | } |
mbed_official | 49:77c8e4604045 | 69 | if ((blk_size + sizeof_bm) > box_size) { |
mbed_official | 49:77c8e4604045 | 70 | return (1); |
mbed_official | 49:77c8e4604045 | 71 | } |
mbed_official | 49:77c8e4604045 | 72 | /* Create a Memory structure. */ |
mbed_official | 49:77c8e4604045 | 73 | blk = ((U8 *) box_mem) + sizeof_bm; |
mbed_official | 49:77c8e4604045 | 74 | ((P_BM) box_mem)->free = blk; |
mbed_official | 49:77c8e4604045 | 75 | end = ((U8 *) box_mem) + box_size; |
mbed_official | 49:77c8e4604045 | 76 | ((P_BM) box_mem)->end = end; |
mbed_official | 49:77c8e4604045 | 77 | ((P_BM) box_mem)->blk_size = blk_size; |
mbed_official | 49:77c8e4604045 | 78 | |
mbed_official | 49:77c8e4604045 | 79 | /* Link all free blocks using offsets. */ |
mbed_official | 49:77c8e4604045 | 80 | end = ((U8 *) end) - blk_size; |
mbed_official | 49:77c8e4604045 | 81 | while (1) { |
mbed_official | 49:77c8e4604045 | 82 | next = ((U8 *) blk) + blk_size; |
mbed_official | 49:77c8e4604045 | 83 | if (next > end) break; |
mbed_official | 49:77c8e4604045 | 84 | *((void **)blk) = next; |
mbed_official | 49:77c8e4604045 | 85 | blk = next; |
mbed_official | 49:77c8e4604045 | 86 | } |
mbed_official | 49:77c8e4604045 | 87 | /* end marker */ |
mbed_official | 49:77c8e4604045 | 88 | *((void **)blk) = 0; |
mbed_official | 49:77c8e4604045 | 89 | return (0); |
mbed_official | 49:77c8e4604045 | 90 | } |
mbed_official | 49:77c8e4604045 | 91 | |
mbed_official | 49:77c8e4604045 | 92 | /*--------------------------- rt_alloc_box ----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 93 | |
mbed_official | 49:77c8e4604045 | 94 | void *rt_alloc_box (void *box_mem) { |
mbed_official | 49:77c8e4604045 | 95 | /* Allocate a memory block and return start address. */ |
mbed_official | 49:77c8e4604045 | 96 | void **free; |
mbed_official | 49:77c8e4604045 | 97 | #ifndef __USE_EXCLUSIVE_ACCESS |
mbed_official | 49:77c8e4604045 | 98 | int irq_dis; |
mbed_official | 49:77c8e4604045 | 99 | |
mbed_official | 49:77c8e4604045 | 100 | irq_dis = __disable_irq (); |
mbed_official | 49:77c8e4604045 | 101 | free = ((P_BM) box_mem)->free; |
mbed_official | 49:77c8e4604045 | 102 | if (free) { |
mbed_official | 49:77c8e4604045 | 103 | ((P_BM) box_mem)->free = *free; |
mbed_official | 49:77c8e4604045 | 104 | } |
mbed_official | 49:77c8e4604045 | 105 | if (!irq_dis) __enable_irq (); |
mbed_official | 49:77c8e4604045 | 106 | #else |
mbed_official | 49:77c8e4604045 | 107 | do { |
mbed_official | 49:77c8e4604045 | 108 | if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) { |
mbed_official | 49:77c8e4604045 | 109 | __clrex(); |
mbed_official | 49:77c8e4604045 | 110 | break; |
mbed_official | 49:77c8e4604045 | 111 | } |
mbed_official | 49:77c8e4604045 | 112 | } while (__strex((U32)*free, &((P_BM) box_mem)->free)); |
mbed_official | 49:77c8e4604045 | 113 | #endif |
mbed_official | 49:77c8e4604045 | 114 | return (free); |
mbed_official | 49:77c8e4604045 | 115 | } |
mbed_official | 49:77c8e4604045 | 116 | |
mbed_official | 49:77c8e4604045 | 117 | |
mbed_official | 49:77c8e4604045 | 118 | /*--------------------------- _calloc_box -----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 119 | |
mbed_official | 49:77c8e4604045 | 120 | void *_calloc_box (void *box_mem) { |
mbed_official | 49:77c8e4604045 | 121 | /* Allocate a 0-initialized memory block and return start address. */ |
mbed_official | 49:77c8e4604045 | 122 | void *free; |
mbed_official | 49:77c8e4604045 | 123 | U32 *p; |
mbed_official | 49:77c8e4604045 | 124 | U32 i; |
mbed_official | 49:77c8e4604045 | 125 | |
mbed_official | 49:77c8e4604045 | 126 | free = _alloc_box (box_mem); |
mbed_official | 49:77c8e4604045 | 127 | if (free) { |
mbed_official | 49:77c8e4604045 | 128 | p = free; |
mbed_official | 49:77c8e4604045 | 129 | for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) { |
mbed_official | 49:77c8e4604045 | 130 | *p = 0; |
mbed_official | 49:77c8e4604045 | 131 | p++; |
mbed_official | 49:77c8e4604045 | 132 | } |
mbed_official | 49:77c8e4604045 | 133 | } |
mbed_official | 49:77c8e4604045 | 134 | return (free); |
mbed_official | 49:77c8e4604045 | 135 | } |
mbed_official | 49:77c8e4604045 | 136 | |
mbed_official | 49:77c8e4604045 | 137 | |
mbed_official | 49:77c8e4604045 | 138 | /*--------------------------- rt_free_box -----------------------------------*/ |
mbed_official | 49:77c8e4604045 | 139 | |
mbed_official | 49:77c8e4604045 | 140 | int rt_free_box (void *box_mem, void *box) { |
mbed_official | 49:77c8e4604045 | 141 | /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */ |
mbed_official | 49:77c8e4604045 | 142 | #ifndef __USE_EXCLUSIVE_ACCESS |
mbed_official | 49:77c8e4604045 | 143 | int irq_dis; |
mbed_official | 49:77c8e4604045 | 144 | #endif |
mbed_official | 49:77c8e4604045 | 145 | |
mbed_official | 49:77c8e4604045 | 146 | if (box < box_mem || box >= ((P_BM) box_mem)->end) { |
mbed_official | 49:77c8e4604045 | 147 | return (1); |
mbed_official | 49:77c8e4604045 | 148 | } |
mbed_official | 49:77c8e4604045 | 149 | |
mbed_official | 49:77c8e4604045 | 150 | #ifndef __USE_EXCLUSIVE_ACCESS |
mbed_official | 49:77c8e4604045 | 151 | irq_dis = __disable_irq (); |
mbed_official | 49:77c8e4604045 | 152 | *((void **)box) = ((P_BM) box_mem)->free; |
mbed_official | 49:77c8e4604045 | 153 | ((P_BM) box_mem)->free = box; |
mbed_official | 49:77c8e4604045 | 154 | if (!irq_dis) __enable_irq (); |
mbed_official | 49:77c8e4604045 | 155 | #else |
mbed_official | 49:77c8e4604045 | 156 | do { |
mbed_official | 49:77c8e4604045 | 157 | *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free); |
mbed_official | 49:77c8e4604045 | 158 | } while (__strex ((U32)box, &((P_BM) box_mem)->free)); |
mbed_official | 49:77c8e4604045 | 159 | #endif |
mbed_official | 49:77c8e4604045 | 160 | return (0); |
mbed_official | 49:77c8e4604045 | 161 | } |
mbed_official | 49:77c8e4604045 | 162 | |
mbed_official | 49:77c8e4604045 | 163 | /*---------------------------------------------------------------------------- |
mbed_official | 49:77c8e4604045 | 164 | * end of file |
mbed_official | 49:77c8e4604045 | 165 | *---------------------------------------------------------------------------*/ |
mbed_official | 49:77c8e4604045 | 166 |