Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-rtos by
rt_MemBox.c
00001 /*---------------------------------------------------------------------------- 00002 * RL-ARM - RTX 00003 *---------------------------------------------------------------------------- 00004 * Name: RT_MEMBOX.C 00005 * Purpose: Interface functions for fixed memory block management system 00006 * Rev.: V4.60 00007 *---------------------------------------------------------------------------- 00008 * 00009 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH 00010 * All rights reserved. 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions are met: 00013 * - Redistributions of source code must retain the above copyright 00014 * notice, this list of conditions and the following disclaimer. 00015 * - Redistributions in binary form must reproduce the above copyright 00016 * notice, this list of conditions and the following disclaimer in the 00017 * documentation and/or other materials provided with the distribution. 00018 * - Neither the name of ARM nor the names of its contributors may be used 00019 * to endorse or promote products derived from this software without 00020 * specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00023 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00024 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00025 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE 00026 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00027 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00028 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00029 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00030 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00031 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *---------------------------------------------------------------------------*/ 00034 00035 #include "rt_TypeDef.h" 00036 #include "RTX_Conf.h" 00037 #include "rt_System.h" 00038 #include "rt_MemBox.h" 00039 #include "rt_HAL_CM.h" 00040 00041 /*---------------------------------------------------------------------------- 00042 * Global Functions 00043 *---------------------------------------------------------------------------*/ 00044 00045 00046 /*--------------------------- _init_box -------------------------------------*/ 00047 00048 int _init_box (void *box_mem, U32 box_size, U32 blk_size) { 00049 /* Initialize memory block system, returns 0 if OK, 1 if fails. */ 00050 void *end; 00051 void *blk; 00052 void *next; 00053 U32 sizeof_bm; 00054 00055 /* Create memory structure. */ 00056 if (blk_size & BOX_ALIGN_8) { 00057 /* Memory blocks 8-byte aligned. */ 00058 blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7; 00059 sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7; 00060 } 00061 else { 00062 /* Memory blocks 4-byte aligned. */ 00063 blk_size = (blk_size + 3) & ~3; 00064 sizeof_bm = sizeof (struct OS_BM); 00065 } 00066 if (blk_size == 0) { 00067 return (1); 00068 } 00069 if ((blk_size + sizeof_bm) > box_size) { 00070 return (1); 00071 } 00072 /* Create a Memory structure. */ 00073 blk = ((U8 *) box_mem) + sizeof_bm; 00074 ((P_BM) box_mem)->free = blk; 00075 end = ((U8 *) box_mem) + box_size; 00076 ((P_BM) box_mem)->end = end; 00077 ((P_BM) box_mem)->blk_size = blk_size; 00078 00079 /* Link all free blocks using offsets. */ 00080 end = ((U8 *) end) - blk_size; 00081 while (1) { 00082 next = ((U8 *) blk) + blk_size; 00083 if (next > end) break; 00084 *((void **)blk) = next; 00085 blk = next; 00086 } 00087 /* end marker */ 00088 *((void **)blk) = 0; 00089 return (0); 00090 } 00091 00092 /*--------------------------- rt_alloc_box ----------------------------------*/ 00093 00094 void *rt_alloc_box (void *box_mem) { 00095 /* Allocate a memory block and return start address. */ 00096 void **free; 00097 #ifndef __USE_EXCLUSIVE_ACCESS 00098 int irq_dis; 00099 00100 irq_dis = __disable_irq (); 00101 free = ((P_BM) box_mem)->free; 00102 if (free) { 00103 ((P_BM) box_mem)->free = *free; 00104 } 00105 if (!irq_dis) __enable_irq (); 00106 #else 00107 do { 00108 if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) { 00109 __clrex(); 00110 break; 00111 } 00112 } while (__strex((U32)*free, &((P_BM) box_mem)->free)); 00113 #endif 00114 return (free); 00115 } 00116 00117 00118 /*--------------------------- _calloc_box -----------------------------------*/ 00119 00120 void *_calloc_box (void *box_mem) { 00121 /* Allocate a 0-initialized memory block and return start address. */ 00122 void *free; 00123 U32 *p; 00124 U32 i; 00125 00126 free = _alloc_box (box_mem); 00127 if (free) { 00128 p = free; 00129 for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) { 00130 *p = 0; 00131 p++; 00132 } 00133 } 00134 return (free); 00135 } 00136 00137 00138 /*--------------------------- rt_free_box -----------------------------------*/ 00139 00140 int rt_free_box (void *box_mem, void *box) { 00141 /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */ 00142 #ifndef __USE_EXCLUSIVE_ACCESS 00143 int irq_dis; 00144 #endif 00145 00146 if (box < box_mem || box >= ((P_BM) box_mem)->end) { 00147 return (1); 00148 } 00149 00150 #ifndef __USE_EXCLUSIVE_ACCESS 00151 irq_dis = __disable_irq (); 00152 *((void **)box) = ((P_BM) box_mem)->free; 00153 ((P_BM) box_mem)->free = box; 00154 if (!irq_dis) __enable_irq (); 00155 #else 00156 do { 00157 *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free); 00158 } while (__strex ((U32)box, &((P_BM) box_mem)->free)); 00159 #endif 00160 return (0); 00161 } 00162 00163 /*---------------------------------------------------------------------------- 00164 * end of file 00165 *---------------------------------------------------------------------------*/ 00166
Generated on Wed Jul 13 2022 18:32:39 by
