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.
rt_MemBox.c
00001 /** 00002 * @file rt_MemBox.c 00003 * @brief 00004 * 00005 * DAPLink Interface Firmware 00006 * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved 00007 * SPDX-License-Identifier: Apache-2.0 00008 * 00009 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00010 * not use this file except in compliance with the License. 00011 * You may obtain a copy of the License at 00012 * 00013 * http://www.apache.org/licenses/LICENSE-2.0 00014 * 00015 * Unless required by applicable law or agreed to in writing, software 00016 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00017 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 * See the License for the specific language governing permissions and 00019 * limitations under the License. 00020 */ 00021 00022 #include "rt_TypeDef.h" 00023 #include "RTX_Config.h" 00024 #include "rt_System.h" 00025 #include "rt_MemBox.h" 00026 #include "rt_HAL_CM.h" 00027 00028 /*---------------------------------------------------------------------------- 00029 * Global Functions 00030 *---------------------------------------------------------------------------*/ 00031 00032 00033 /*--------------------------- _init_box -------------------------------------*/ 00034 00035 int _init_box (void *box_mem, U32 box_size, U32 blk_size) { 00036 /* Initialize memory block system, returns 0 if OK, 1 if fails. */ 00037 void *end; 00038 void *blk; 00039 void *next; 00040 U32 sizeof_bm; 00041 00042 /* Create memory structure. */ 00043 if (blk_size & BOX_ALIGN_8) { 00044 /* Memory blocks 8-byte aligned. */ 00045 blk_size = ((blk_size & ~BOX_ALIGN_8) + 7) & ~7; 00046 sizeof_bm = (sizeof (struct OS_BM) + 7) & ~7; 00047 } 00048 else { 00049 /* Memory blocks 4-byte aligned. */ 00050 blk_size = (blk_size + 3) & ~3; 00051 sizeof_bm = sizeof (struct OS_BM); 00052 } 00053 if (blk_size == 0) { 00054 return (1); 00055 } 00056 if ((blk_size + sizeof_bm) > box_size) { 00057 return (1); 00058 } 00059 /* Create a Memory structure. */ 00060 blk = ((U8 *) box_mem) + sizeof_bm; 00061 ((P_BM) box_mem)->free = blk; 00062 end = ((U8 *) box_mem) + box_size; 00063 ((P_BM) box_mem)->end = end; 00064 ((P_BM) box_mem)->blk_size = blk_size; 00065 00066 /* Link all free blocks using offsets. */ 00067 end = ((U8 *) end) - blk_size; 00068 while (1) { 00069 next = ((U8 *) blk) + blk_size; 00070 if (next > end) break; 00071 *((void **)blk) = next; 00072 blk = next; 00073 } 00074 /* end marker */ 00075 *((void **)blk) = 0; 00076 return (0); 00077 } 00078 00079 /*--------------------------- rt_alloc_box ----------------------------------*/ 00080 00081 void *rt_alloc_box (void *box_mem) { 00082 /* Allocate a memory block and return start address. */ 00083 void **free; 00084 #ifndef __USE_EXCLUSIVE_ACCESS 00085 int irq_dis; 00086 00087 irq_dis = __disable_irq (); 00088 free = ((P_BM) box_mem)->free; 00089 if (free) { 00090 ((P_BM) box_mem)->free = *free; 00091 } 00092 if (!irq_dis) __enable_irq (); 00093 #else 00094 do { 00095 if ((free = (void **)__ldrex(&((P_BM) box_mem)->free)) == 0) { 00096 __clrex(); 00097 break; 00098 } 00099 } while (__strex((U32)*free, &((P_BM) box_mem)->free)); 00100 #endif 00101 return (free); 00102 } 00103 00104 00105 /*--------------------------- _calloc_box -----------------------------------*/ 00106 00107 void *_calloc_box (void *box_mem) { 00108 /* Allocate a 0-initialized memory block and return start address. */ 00109 void *free; 00110 U32 *p; 00111 U32 i; 00112 00113 free = _alloc_box (box_mem); 00114 if (free) { 00115 p = free; 00116 for (i = ((P_BM) box_mem)->blk_size; i; i -= 4) { 00117 *p = 0; 00118 p++; 00119 } 00120 } 00121 return (free); 00122 } 00123 00124 00125 /*--------------------------- rt_free_box -----------------------------------*/ 00126 00127 int rt_free_box (void *box_mem, void *box) { 00128 /* Free a memory block, returns 0 if OK, 1 if box does not belong to box_mem */ 00129 #ifndef __USE_EXCLUSIVE_ACCESS 00130 int irq_dis; 00131 #endif 00132 00133 if (box < box_mem || box >= ((P_BM) box_mem)->end) { 00134 return (1); 00135 } 00136 00137 #ifndef __USE_EXCLUSIVE_ACCESS 00138 irq_dis = __disable_irq (); 00139 *((void **)box) = ((P_BM) box_mem)->free; 00140 ((P_BM) box_mem)->free = box; 00141 if (!irq_dis) __enable_irq (); 00142 #else 00143 do { 00144 *((void **)box) = (void *)__ldrex(&((P_BM) box_mem)->free); 00145 } while (__strex ((U32)box, &((P_BM) box_mem)->free)); 00146 #endif 00147 return (0); 00148 } 00149 00150 /*---------------------------------------------------------------------------- 00151 * end of file 00152 *---------------------------------------------------------------------------*/ 00153
Generated on Tue Jul 12 2022 15:37:22 by
1.7.2