Eric Ebert
/
CoOS
CoOS Demonstrator adapted to mbed Hardware.
kernelHeap.c@0:57690853989a, 2010-12-03 (annotated)
- Committer:
- ericebert
- Date:
- Fri Dec 03 19:45:30 2010 +0000
- Revision:
- 0:57690853989a
Some basic LED-Flashing works in the CoOS-RTOS using Tasks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ericebert | 0:57690853989a | 1 | /** |
ericebert | 0:57690853989a | 2 | ******************************************************************************* |
ericebert | 0:57690853989a | 3 | * @file kernelHeap.c |
ericebert | 0:57690853989a | 4 | * @version V1.1.3 |
ericebert | 0:57690853989a | 5 | * @date 2010.04.26 |
ericebert | 0:57690853989a | 6 | * @brief kernel heap management implementation code of CooCox CoOS kernel. |
ericebert | 0:57690853989a | 7 | ******************************************************************************* |
ericebert | 0:57690853989a | 8 | * @copy |
ericebert | 0:57690853989a | 9 | * |
ericebert | 0:57690853989a | 10 | * INTERNAL FILE,DON'T PUBLIC. |
ericebert | 0:57690853989a | 11 | * |
ericebert | 0:57690853989a | 12 | * <h2><center>© COPYRIGHT 2009 CooCox </center></h2> |
ericebert | 0:57690853989a | 13 | ******************************************************************************* |
ericebert | 0:57690853989a | 14 | */ |
ericebert | 0:57690853989a | 15 | |
ericebert | 0:57690853989a | 16 | |
ericebert | 0:57690853989a | 17 | /*---------------------------- Include ---------------------------------------*/ |
ericebert | 0:57690853989a | 18 | #include <coocox.h> |
ericebert | 0:57690853989a | 19 | |
ericebert | 0:57690853989a | 20 | |
ericebert | 0:57690853989a | 21 | #if CFG_KHEAP_EN >0 |
ericebert | 0:57690853989a | 22 | /*---------------------------- Variable Define -------------------------------*/ |
ericebert | 0:57690853989a | 23 | U32 KernelHeap[KHEAP_SIZE] = {0}; /*!< Kernel heap */ |
ericebert | 0:57690853989a | 24 | P_FMB FMBlist = 0; /*!< Free memory block list */ |
ericebert | 0:57690853989a | 25 | KHeap Kheap = {0}; /*!< Kernel heap control */ |
ericebert | 0:57690853989a | 26 | |
ericebert | 0:57690853989a | 27 | |
ericebert | 0:57690853989a | 28 | /*---------------------------- Function Declare ------------------------------*/ |
ericebert | 0:57690853989a | 29 | static P_FMB GetPreFMB(P_UMB usedMB); |
ericebert | 0:57690853989a | 30 | /** |
ericebert | 0:57690853989a | 31 | ******************************************************************************* |
ericebert | 0:57690853989a | 32 | * @brief Create kernel heap |
ericebert | 0:57690853989a | 33 | * @param[in] None |
ericebert | 0:57690853989a | 34 | * @param[out] None |
ericebert | 0:57690853989a | 35 | * @retval None |
ericebert | 0:57690853989a | 36 | * |
ericebert | 0:57690853989a | 37 | * @par Description |
ericebert | 0:57690853989a | 38 | * @details This function is called to create kernel heap. |
ericebert | 0:57690853989a | 39 | ******************************************************************************* |
ericebert | 0:57690853989a | 40 | */ |
ericebert | 0:57690853989a | 41 | void CoCreateKheap(void) |
ericebert | 0:57690853989a | 42 | { |
ericebert | 0:57690853989a | 43 | Kheap.startAddr = (U32)(KernelHeap); /* Initialize kernel heap control */ |
ericebert | 0:57690853989a | 44 | Kheap.endAddr = (U32)(KernelHeap) + KHEAP_SIZE*4; |
ericebert | 0:57690853989a | 45 | FMBlist = (P_FMB)KernelHeap; /* Initialize free memory block list*/ |
ericebert | 0:57690853989a | 46 | FMBlist->nextFMB = 0; |
ericebert | 0:57690853989a | 47 | FMBlist->nextUMB = 0; |
ericebert | 0:57690853989a | 48 | FMBlist->preUMB = 0; |
ericebert | 0:57690853989a | 49 | } |
ericebert | 0:57690853989a | 50 | |
ericebert | 0:57690853989a | 51 | |
ericebert | 0:57690853989a | 52 | /** |
ericebert | 0:57690853989a | 53 | ******************************************************************************* |
ericebert | 0:57690853989a | 54 | * @brief Allocation size bytes of memory block from kernel heap. |
ericebert | 0:57690853989a | 55 | * @param[in] size Length of menory block. |
ericebert | 0:57690853989a | 56 | * @param[out] None |
ericebert | 0:57690853989a | 57 | * @retval 0 Allocate fail. |
ericebert | 0:57690853989a | 58 | * @retval others Pointer to memory block. |
ericebert | 0:57690853989a | 59 | * |
ericebert | 0:57690853989a | 60 | * @par Description |
ericebert | 0:57690853989a | 61 | * @details This function is called to allocation size bytes of memory block. |
ericebert | 0:57690853989a | 62 | ******************************************************************************* |
ericebert | 0:57690853989a | 63 | */ |
ericebert | 0:57690853989a | 64 | void* CoKmalloc(U32 size) |
ericebert | 0:57690853989a | 65 | { |
ericebert | 0:57690853989a | 66 | P_FMB freeMB,newFMB,preFMB; |
ericebert | 0:57690853989a | 67 | P_UMB usedMB,tmpUMB; |
ericebert | 0:57690853989a | 68 | U8* memAddr; |
ericebert | 0:57690853989a | 69 | U32 freeSize; |
ericebert | 0:57690853989a | 70 | U32 kheapAddr; |
ericebert | 0:57690853989a | 71 | |
ericebert | 0:57690853989a | 72 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
ericebert | 0:57690853989a | 73 | if( size == 0 ) |
ericebert | 0:57690853989a | 74 | { |
ericebert | 0:57690853989a | 75 | return 0; |
ericebert | 0:57690853989a | 76 | } |
ericebert | 0:57690853989a | 77 | #endif |
ericebert | 0:57690853989a | 78 | |
ericebert | 0:57690853989a | 79 | /* Word alignment,and add used memory head size */ |
ericebert | 0:57690853989a | 80 | size = (((size+3)>>2)<<2) + 8; |
ericebert | 0:57690853989a | 81 | kheapAddr = Kheap.endAddr; /* Get the end address of kernel heap */ |
ericebert | 0:57690853989a | 82 | OsSchedLock(); /* Lock schedule */ |
ericebert | 0:57690853989a | 83 | freeMB = FMBlist; /* Get first item of free memory list */ |
ericebert | 0:57690853989a | 84 | preFMB = 0; |
ericebert | 0:57690853989a | 85 | while(freeMB != 0 ) /* Is out of free memory list? */ |
ericebert | 0:57690853989a | 86 | { /* No */ |
ericebert | 0:57690853989a | 87 | if(freeMB->nextUMB == 0) /* Is last item of free memory list? */ |
ericebert | 0:57690853989a | 88 | { /* Yes,get size for this free item */ |
ericebert | 0:57690853989a | 89 | freeSize = kheapAddr - (U32)(freeMB); |
ericebert | 0:57690853989a | 90 | } |
ericebert | 0:57690853989a | 91 | else /* No,get size for this free item */ |
ericebert | 0:57690853989a | 92 | { |
ericebert | 0:57690853989a | 93 | freeSize = (U32)(freeMB->nextUMB) -1 - (U32)(freeMB); |
ericebert | 0:57690853989a | 94 | } |
ericebert | 0:57690853989a | 95 | if(freeSize >= size) /* If the size equal or greater than need */ |
ericebert | 0:57690853989a | 96 | { /* Yes,assign in this free memory */ |
ericebert | 0:57690853989a | 97 | usedMB=(P_UMB)freeMB;/* Get the address for used memory block head*/ |
ericebert | 0:57690853989a | 98 | |
ericebert | 0:57690853989a | 99 | /* Get the address for used memory block */ |
ericebert | 0:57690853989a | 100 | memAddr = (U8*)((U32)(usedMB) + 8); |
ericebert | 0:57690853989a | 101 | |
ericebert | 0:57690853989a | 102 | /* Is left size of free memory smaller than 12? */ |
ericebert | 0:57690853989a | 103 | if((freeSize-size) < 12) |
ericebert | 0:57690853989a | 104 | { |
ericebert | 0:57690853989a | 105 | /* Yes,malloc together(12 is the size of the header information |
ericebert | 0:57690853989a | 106 | of free memory block ). */ |
ericebert | 0:57690853989a | 107 | if(preFMB != 0)/* Is first item of free memory block list? */ |
ericebert | 0:57690853989a | 108 | { /* No,set the link for list */ |
ericebert | 0:57690853989a | 109 | preFMB->nextFMB = freeMB->nextFMB; |
ericebert | 0:57690853989a | 110 | } |
ericebert | 0:57690853989a | 111 | else /* Yes,reset the first item */ |
ericebert | 0:57690853989a | 112 | { |
ericebert | 0:57690853989a | 113 | FMBlist = freeMB->nextFMB; |
ericebert | 0:57690853989a | 114 | } |
ericebert | 0:57690853989a | 115 | |
ericebert | 0:57690853989a | 116 | if(freeMB->nextUMB != 0) /* Is last item? */ |
ericebert | 0:57690853989a | 117 | { /* No,set the link for list */ |
ericebert | 0:57690853989a | 118 | tmpUMB = (P_UMB)((U32)(freeMB->nextUMB)-1); |
ericebert | 0:57690853989a | 119 | tmpUMB->preMB = (void*)((U32)usedMB|0x1); |
ericebert | 0:57690853989a | 120 | } |
ericebert | 0:57690853989a | 121 | |
ericebert | 0:57690853989a | 122 | usedMB->nextMB = freeMB->nextUMB;/* Set used memory block link*/ |
ericebert | 0:57690853989a | 123 | usedMB->preMB = freeMB->preUMB; |
ericebert | 0:57690853989a | 124 | } |
ericebert | 0:57690853989a | 125 | else /* No,the left size more than 12 */ |
ericebert | 0:57690853989a | 126 | { |
ericebert | 0:57690853989a | 127 | /* Get new free memory block address */ |
ericebert | 0:57690853989a | 128 | newFMB = (P_FMB)((U32)(freeMB) + size); |
ericebert | 0:57690853989a | 129 | |
ericebert | 0:57690853989a | 130 | if(preFMB != 0)/* Is first item of free memory block list? */ |
ericebert | 0:57690853989a | 131 | { |
ericebert | 0:57690853989a | 132 | preFMB->nextFMB = newFMB; /* No,set the link for list */ |
ericebert | 0:57690853989a | 133 | } |
ericebert | 0:57690853989a | 134 | else |
ericebert | 0:57690853989a | 135 | { |
ericebert | 0:57690853989a | 136 | FMBlist = newFMB; /* Yes,reset the first item */ |
ericebert | 0:57690853989a | 137 | } |
ericebert | 0:57690853989a | 138 | |
ericebert | 0:57690853989a | 139 | /* Set link for new free memory block */ |
ericebert | 0:57690853989a | 140 | newFMB->preUMB = (P_UMB)((U32)usedMB|0x1); |
ericebert | 0:57690853989a | 141 | newFMB->nextUMB = freeMB->nextUMB; |
ericebert | 0:57690853989a | 142 | newFMB->nextFMB = freeMB->nextFMB; |
ericebert | 0:57690853989a | 143 | |
ericebert | 0:57690853989a | 144 | if(freeMB->nextUMB != 0) /* Is last item? */ |
ericebert | 0:57690853989a | 145 | { /* No,set the link for list */ |
ericebert | 0:57690853989a | 146 | tmpUMB = (P_UMB)((U32)(freeMB->nextUMB)-1); |
ericebert | 0:57690853989a | 147 | tmpUMB->preMB = newFMB; |
ericebert | 0:57690853989a | 148 | } |
ericebert | 0:57690853989a | 149 | |
ericebert | 0:57690853989a | 150 | usedMB->nextMB = newFMB; /* Set used memory block link */ |
ericebert | 0:57690853989a | 151 | usedMB->preMB = freeMB->preUMB; |
ericebert | 0:57690853989a | 152 | } |
ericebert | 0:57690853989a | 153 | |
ericebert | 0:57690853989a | 154 | if(freeMB->preUMB != 0) /* Is first item? */ |
ericebert | 0:57690853989a | 155 | { /* No,set the link for list */ |
ericebert | 0:57690853989a | 156 | tmpUMB = (P_UMB)((U32)(freeMB->preUMB)-1); |
ericebert | 0:57690853989a | 157 | tmpUMB->nextMB = (void*)((U32)usedMB|0x1); |
ericebert | 0:57690853989a | 158 | } |
ericebert | 0:57690853989a | 159 | |
ericebert | 0:57690853989a | 160 | OsSchedUnlock(); /* Unlock schedule */ |
ericebert | 0:57690853989a | 161 | return memAddr; /* Return used memory block address */ |
ericebert | 0:57690853989a | 162 | } |
ericebert | 0:57690853989a | 163 | preFMB = freeMB; /* Save current free memory block as previous */ |
ericebert | 0:57690853989a | 164 | freeMB = freeMB->nextFMB; /* Get the next item as current item*/ |
ericebert | 0:57690853989a | 165 | } |
ericebert | 0:57690853989a | 166 | OsSchedUnlock(); /* Unlock schedule */ |
ericebert | 0:57690853989a | 167 | return 0; /* Error return */ |
ericebert | 0:57690853989a | 168 | } |
ericebert | 0:57690853989a | 169 | |
ericebert | 0:57690853989a | 170 | |
ericebert | 0:57690853989a | 171 | /** |
ericebert | 0:57690853989a | 172 | ******************************************************************************* |
ericebert | 0:57690853989a | 173 | * @brief Release memory block to kernel heap. |
ericebert | 0:57690853989a | 174 | * @param[in] memBuf Pointer to memory block. |
ericebert | 0:57690853989a | 175 | * @param[out] None |
ericebert | 0:57690853989a | 176 | * @retval None |
ericebert | 0:57690853989a | 177 | * |
ericebert | 0:57690853989a | 178 | * @par Description |
ericebert | 0:57690853989a | 179 | * @details This function is called to release memory block. |
ericebert | 0:57690853989a | 180 | ******************************************************************************* |
ericebert | 0:57690853989a | 181 | */ |
ericebert | 0:57690853989a | 182 | void CoKfree(void* memBuf) |
ericebert | 0:57690853989a | 183 | { |
ericebert | 0:57690853989a | 184 | P_FMB curFMB,nextFMB,preFMB; |
ericebert | 0:57690853989a | 185 | P_UMB usedMB,nextUMB,preUMB; |
ericebert | 0:57690853989a | 186 | |
ericebert | 0:57690853989a | 187 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
ericebert | 0:57690853989a | 188 | if(memBuf == 0) |
ericebert | 0:57690853989a | 189 | { |
ericebert | 0:57690853989a | 190 | return; |
ericebert | 0:57690853989a | 191 | } |
ericebert | 0:57690853989a | 192 | #endif |
ericebert | 0:57690853989a | 193 | |
ericebert | 0:57690853989a | 194 | usedMB = (P_UMB)((U32)(memBuf)-8); |
ericebert | 0:57690853989a | 195 | |
ericebert | 0:57690853989a | 196 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
ericebert | 0:57690853989a | 197 | if((U32)(memBuf) < Kheap.startAddr) |
ericebert | 0:57690853989a | 198 | { |
ericebert | 0:57690853989a | 199 | return; |
ericebert | 0:57690853989a | 200 | } |
ericebert | 0:57690853989a | 201 | if((U32)(memBuf) > Kheap.endAddr) |
ericebert | 0:57690853989a | 202 | { |
ericebert | 0:57690853989a | 203 | return; |
ericebert | 0:57690853989a | 204 | } |
ericebert | 0:57690853989a | 205 | #endif |
ericebert | 0:57690853989a | 206 | |
ericebert | 0:57690853989a | 207 | |
ericebert | 0:57690853989a | 208 | OsSchedLock(); /* Lock schedule */ |
ericebert | 0:57690853989a | 209 | |
ericebert | 0:57690853989a | 210 | #if CFG_PAR_CHECKOUT_EN >0 /* Check UMB in list */ |
ericebert | 0:57690853989a | 211 | if((U32)(usedMB) < (U32)(FMBlist)) |
ericebert | 0:57690853989a | 212 | { |
ericebert | 0:57690853989a | 213 | preUMB = (P_UMB)((U32)(FMBlist->preUMB)-1); |
ericebert | 0:57690853989a | 214 | while(preUMB != usedMB) |
ericebert | 0:57690853989a | 215 | { |
ericebert | 0:57690853989a | 216 | if(preUMB == 0) |
ericebert | 0:57690853989a | 217 | { |
ericebert | 0:57690853989a | 218 | OsSchedUnlock(); |
ericebert | 0:57690853989a | 219 | return; |
ericebert | 0:57690853989a | 220 | } |
ericebert | 0:57690853989a | 221 | preUMB = (P_UMB)((U32)(preUMB->preMB)-1); |
ericebert | 0:57690853989a | 222 | } |
ericebert | 0:57690853989a | 223 | } |
ericebert | 0:57690853989a | 224 | else |
ericebert | 0:57690853989a | 225 | { |
ericebert | 0:57690853989a | 226 | if(FMBlist == 0) |
ericebert | 0:57690853989a | 227 | { |
ericebert | 0:57690853989a | 228 | nextUMB = (P_UMB)(Kheap.startAddr); |
ericebert | 0:57690853989a | 229 | } |
ericebert | 0:57690853989a | 230 | else |
ericebert | 0:57690853989a | 231 | { |
ericebert | 0:57690853989a | 232 | if(FMBlist->nextUMB != 0) |
ericebert | 0:57690853989a | 233 | { |
ericebert | 0:57690853989a | 234 | nextUMB = (P_UMB)((U32)(FMBlist->nextUMB)-1); |
ericebert | 0:57690853989a | 235 | } |
ericebert | 0:57690853989a | 236 | else |
ericebert | 0:57690853989a | 237 | { |
ericebert | 0:57690853989a | 238 | nextUMB = 0; |
ericebert | 0:57690853989a | 239 | } |
ericebert | 0:57690853989a | 240 | } |
ericebert | 0:57690853989a | 241 | |
ericebert | 0:57690853989a | 242 | while(nextUMB != usedMB) |
ericebert | 0:57690853989a | 243 | { |
ericebert | 0:57690853989a | 244 | if(nextUMB == 0) |
ericebert | 0:57690853989a | 245 | { |
ericebert | 0:57690853989a | 246 | OsSchedUnlock(); |
ericebert | 0:57690853989a | 247 | return; |
ericebert | 0:57690853989a | 248 | } |
ericebert | 0:57690853989a | 249 | if(((U32)(nextUMB->nextMB)&0x1) == 0) |
ericebert | 0:57690853989a | 250 | { |
ericebert | 0:57690853989a | 251 | nextFMB = (P_FMB)(nextUMB->nextMB); |
ericebert | 0:57690853989a | 252 | nextUMB = (P_UMB)((U32)(nextFMB->nextUMB)-1); |
ericebert | 0:57690853989a | 253 | } |
ericebert | 0:57690853989a | 254 | else |
ericebert | 0:57690853989a | 255 | { |
ericebert | 0:57690853989a | 256 | nextUMB = (P_UMB)((U32)(nextUMB->nextMB)-1); |
ericebert | 0:57690853989a | 257 | } |
ericebert | 0:57690853989a | 258 | } |
ericebert | 0:57690853989a | 259 | } |
ericebert | 0:57690853989a | 260 | #endif |
ericebert | 0:57690853989a | 261 | |
ericebert | 0:57690853989a | 262 | |
ericebert | 0:57690853989a | 263 | /* Is between two free memory block? */ |
ericebert | 0:57690853989a | 264 | if( (((U32)(usedMB->nextMB)&0x1) == 0) && (((U32)(usedMB->preMB)&0x1)==0) ) |
ericebert | 0:57690853989a | 265 | { /* Yes,is the only one item in kernel heap? */ |
ericebert | 0:57690853989a | 266 | if((usedMB->nextMB == 0) && (usedMB->preMB == 0)) |
ericebert | 0:57690853989a | 267 | { |
ericebert | 0:57690853989a | 268 | curFMB = (P_FMB)usedMB; /* Yes,release this item */ |
ericebert | 0:57690853989a | 269 | curFMB->nextFMB = 0; |
ericebert | 0:57690853989a | 270 | curFMB->nextUMB = 0; |
ericebert | 0:57690853989a | 271 | curFMB->preUMB = 0; |
ericebert | 0:57690853989a | 272 | FMBlist = curFMB; |
ericebert | 0:57690853989a | 273 | } |
ericebert | 0:57690853989a | 274 | else if(usedMB->preMB == 0) /* Is the first item in kernel heap */ |
ericebert | 0:57690853989a | 275 | { |
ericebert | 0:57690853989a | 276 | /* Yes,release this item,and set link for list */ |
ericebert | 0:57690853989a | 277 | curFMB = (P_FMB)usedMB; |
ericebert | 0:57690853989a | 278 | nextFMB = (P_FMB)usedMB->nextMB; |
ericebert | 0:57690853989a | 279 | |
ericebert | 0:57690853989a | 280 | curFMB->nextFMB = nextFMB->nextFMB; |
ericebert | 0:57690853989a | 281 | curFMB->nextUMB = nextFMB->nextUMB; |
ericebert | 0:57690853989a | 282 | curFMB->preUMB = 0; |
ericebert | 0:57690853989a | 283 | FMBlist = curFMB; |
ericebert | 0:57690853989a | 284 | } |
ericebert | 0:57690853989a | 285 | else if(usedMB->nextMB == 0) /* Is the last item in kernel heap */ |
ericebert | 0:57690853989a | 286 | { /* Yes,release this item,and set link for list */ |
ericebert | 0:57690853989a | 287 | curFMB = (P_FMB)(usedMB->preMB); |
ericebert | 0:57690853989a | 288 | curFMB->nextFMB = 0; |
ericebert | 0:57690853989a | 289 | curFMB->nextUMB = 0; |
ericebert | 0:57690853989a | 290 | } |
ericebert | 0:57690853989a | 291 | else /* All no,show this item between two normal FMB */ |
ericebert | 0:57690853989a | 292 | { |
ericebert | 0:57690853989a | 293 | /* release this item,and set link for list */ |
ericebert | 0:57690853989a | 294 | nextFMB = (P_FMB)usedMB->nextMB; |
ericebert | 0:57690853989a | 295 | curFMB = (P_FMB)(usedMB->preMB); |
ericebert | 0:57690853989a | 296 | |
ericebert | 0:57690853989a | 297 | curFMB->nextFMB = nextFMB->nextFMB; |
ericebert | 0:57690853989a | 298 | curFMB->nextUMB = nextFMB->nextUMB; |
ericebert | 0:57690853989a | 299 | } |
ericebert | 0:57690853989a | 300 | } |
ericebert | 0:57690853989a | 301 | else if(((U32)(usedMB->preMB)&0x1) == 0) /* Is between FMB and UMB? */ |
ericebert | 0:57690853989a | 302 | { |
ericebert | 0:57690853989a | 303 | if(usedMB->preMB == 0) /* Yes,is the first item in kernel heap? */ |
ericebert | 0:57690853989a | 304 | { |
ericebert | 0:57690853989a | 305 | /* Yes,release this item,and set link for list */ |
ericebert | 0:57690853989a | 306 | curFMB = (P_FMB)usedMB; |
ericebert | 0:57690853989a | 307 | nextUMB = (P_UMB)usedMB->nextMB; |
ericebert | 0:57690853989a | 308 | curFMB->nextUMB = nextUMB; |
ericebert | 0:57690853989a | 309 | curFMB->preUMB = 0; |
ericebert | 0:57690853989a | 310 | curFMB->nextFMB = FMBlist; |
ericebert | 0:57690853989a | 311 | FMBlist = curFMB; |
ericebert | 0:57690853989a | 312 | } |
ericebert | 0:57690853989a | 313 | else /* No,release this item,and set link for list */ |
ericebert | 0:57690853989a | 314 | { |
ericebert | 0:57690853989a | 315 | curFMB = (P_FMB)usedMB->preMB; |
ericebert | 0:57690853989a | 316 | nextUMB = (P_UMB)usedMB->nextMB; |
ericebert | 0:57690853989a | 317 | curFMB->nextUMB = nextUMB; |
ericebert | 0:57690853989a | 318 | } |
ericebert | 0:57690853989a | 319 | |
ericebert | 0:57690853989a | 320 | } |
ericebert | 0:57690853989a | 321 | else if(((U32)(usedMB->nextMB)&0x1) == 0) /* Is between UMB and FMB? */ |
ericebert | 0:57690853989a | 322 | { /* Yes */ |
ericebert | 0:57690853989a | 323 | preUMB = (P_UMB)(usedMB->preMB); /* Get previous UMB */ |
ericebert | 0:57690853989a | 324 | curFMB = (P_FMB)(usedMB); /* new FMB */ |
ericebert | 0:57690853989a | 325 | preFMB = GetPreFMB(usedMB); /* Get previous FMB */ |
ericebert | 0:57690853989a | 326 | if(preFMB == 0) /* Is previous FMB==0? */ |
ericebert | 0:57690853989a | 327 | { |
ericebert | 0:57690853989a | 328 | nextFMB = FMBlist; /* Yes,get next FMB */ |
ericebert | 0:57690853989a | 329 | FMBlist = curFMB; /* Reset new FMB as the first item of FMB list*/ |
ericebert | 0:57690853989a | 330 | } |
ericebert | 0:57690853989a | 331 | else |
ericebert | 0:57690853989a | 332 | { |
ericebert | 0:57690853989a | 333 | nextFMB = preFMB->nextFMB; /* No,get next FMB */ |
ericebert | 0:57690853989a | 334 | preFMB->nextFMB = curFMB; /* Set link for FMB list */ |
ericebert | 0:57690853989a | 335 | } |
ericebert | 0:57690853989a | 336 | |
ericebert | 0:57690853989a | 337 | if(nextFMB == 0) /* Is new FMB as last item of FMB list? */ |
ericebert | 0:57690853989a | 338 | { |
ericebert | 0:57690853989a | 339 | curFMB->preUMB = preUMB; /* Yes,set link for list */ |
ericebert | 0:57690853989a | 340 | curFMB->nextUMB = 0; |
ericebert | 0:57690853989a | 341 | curFMB->nextFMB = 0; |
ericebert | 0:57690853989a | 342 | } |
ericebert | 0:57690853989a | 343 | else |
ericebert | 0:57690853989a | 344 | { |
ericebert | 0:57690853989a | 345 | curFMB->preUMB = preUMB; /* No,set link for list */ |
ericebert | 0:57690853989a | 346 | curFMB->nextUMB = nextFMB->nextUMB; |
ericebert | 0:57690853989a | 347 | curFMB->nextFMB = nextFMB->nextFMB; |
ericebert | 0:57690853989a | 348 | } |
ericebert | 0:57690853989a | 349 | } |
ericebert | 0:57690853989a | 350 | else /* All no,show UMB between two UMB*/ |
ericebert | 0:57690853989a | 351 | { |
ericebert | 0:57690853989a | 352 | curFMB = (P_FMB)(usedMB); /* new FMB */ |
ericebert | 0:57690853989a | 353 | preFMB = GetPreFMB(usedMB); /* Get previous FMB */ |
ericebert | 0:57690853989a | 354 | preUMB = (P_UMB)(usedMB->preMB); /* Get previous UMB */ |
ericebert | 0:57690853989a | 355 | nextUMB = (P_UMB)(usedMB->nextMB); /* Get next UMB */ |
ericebert | 0:57690853989a | 356 | |
ericebert | 0:57690853989a | 357 | if(preFMB == 0 ) /* Is previous FMB==0? */ |
ericebert | 0:57690853989a | 358 | { |
ericebert | 0:57690853989a | 359 | nextFMB = FMBlist; /* Yes,get next FMB */ |
ericebert | 0:57690853989a | 360 | FMBlist = curFMB; /* Reset new FMB as the first item of FMB list */ |
ericebert | 0:57690853989a | 361 | } |
ericebert | 0:57690853989a | 362 | else |
ericebert | 0:57690853989a | 363 | { |
ericebert | 0:57690853989a | 364 | nextFMB = preFMB->nextFMB; /* No,get next FMB */ |
ericebert | 0:57690853989a | 365 | preFMB->nextFMB = curFMB; /* Set link for FMB list */ |
ericebert | 0:57690853989a | 366 | } |
ericebert | 0:57690853989a | 367 | |
ericebert | 0:57690853989a | 368 | curFMB->preUMB = preUMB; /* Set current FMB link for list */ |
ericebert | 0:57690853989a | 369 | curFMB->nextUMB = nextUMB; |
ericebert | 0:57690853989a | 370 | curFMB->nextFMB = nextFMB; |
ericebert | 0:57690853989a | 371 | } |
ericebert | 0:57690853989a | 372 | |
ericebert | 0:57690853989a | 373 | if(curFMB->preUMB != 0)/* Is current FMB as first item in kernel heap? */ |
ericebert | 0:57690853989a | 374 | { /* No,set link for list */ |
ericebert | 0:57690853989a | 375 | preUMB = (P_UMB)((U32)(curFMB->preUMB)-1); |
ericebert | 0:57690853989a | 376 | preUMB->nextMB = (void*)curFMB; |
ericebert | 0:57690853989a | 377 | } |
ericebert | 0:57690853989a | 378 | if(curFMB->nextUMB != 0)/* Is current FMB as last item in kernel heap? */ |
ericebert | 0:57690853989a | 379 | { /* No,set link for list */ |
ericebert | 0:57690853989a | 380 | nextUMB = (P_UMB)((U32)(curFMB->nextUMB)-1); |
ericebert | 0:57690853989a | 381 | nextUMB->preMB = (void*)curFMB; |
ericebert | 0:57690853989a | 382 | } |
ericebert | 0:57690853989a | 383 | OsSchedUnlock(); /* Unlock schedule */ |
ericebert | 0:57690853989a | 384 | } |
ericebert | 0:57690853989a | 385 | |
ericebert | 0:57690853989a | 386 | |
ericebert | 0:57690853989a | 387 | /** |
ericebert | 0:57690853989a | 388 | ******************************************************************************* |
ericebert | 0:57690853989a | 389 | * @brief Get previous free memory block pointer. |
ericebert | 0:57690853989a | 390 | * @param[in] usedMB Current used memory block. |
ericebert | 0:57690853989a | 391 | * @param[out] None |
ericebert | 0:57690853989a | 392 | * @retval Previous free memory block pointer. |
ericebert | 0:57690853989a | 393 | * |
ericebert | 0:57690853989a | 394 | * @par Description |
ericebert | 0:57690853989a | 395 | * @details This function is called to get previous free memory block pointer. |
ericebert | 0:57690853989a | 396 | ******************************************************************************* |
ericebert | 0:57690853989a | 397 | */ |
ericebert | 0:57690853989a | 398 | static P_FMB GetPreFMB(P_UMB usedMB) |
ericebert | 0:57690853989a | 399 | { |
ericebert | 0:57690853989a | 400 | P_UMB preUMB; |
ericebert | 0:57690853989a | 401 | preUMB = usedMB; |
ericebert | 0:57690853989a | 402 | while(((U32)(preUMB->preMB)&0x1)) /* Is previous MB as FMB? */ |
ericebert | 0:57690853989a | 403 | { /* No,get previous MB */ |
ericebert | 0:57690853989a | 404 | preUMB = (P_UMB)((U32)(preUMB->preMB)-1); |
ericebert | 0:57690853989a | 405 | } |
ericebert | 0:57690853989a | 406 | return (P_FMB)(preUMB->preMB); /* Yes,return previous MB */ |
ericebert | 0:57690853989a | 407 | } |
ericebert | 0:57690853989a | 408 | |
ericebert | 0:57690853989a | 409 | #endif |