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