astroboy astroboy
/
CoOS_LWIP
Quick and dirty CoOS + LWIP ( Webserver )
CoOS/kernel/mm.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 mm.c |
astroboy | 0:94897d537b31 | 4 | * @version V1.1.4 |
astroboy | 0:94897d537b31 | 5 | * @date 2011.04.20 |
astroboy | 0:94897d537b31 | 6 | * @brief memory 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_MM_EN > 0 |
astroboy | 0:94897d537b31 | 22 | /*---------------------------- Variable Define -------------------------------*/ |
astroboy | 0:94897d537b31 | 23 | MM MemoryTbl[CFG_MAX_MM] = {{0}};/*!< Table which save memory control block. */ |
astroboy | 0:94897d537b31 | 24 | U32 MemoryIDVessel = 0; /*!< Memory ID container. */ |
astroboy | 0:94897d537b31 | 25 | |
astroboy | 0:94897d537b31 | 26 | /** |
astroboy | 0:94897d537b31 | 27 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 28 | * @brief Create a memory partition |
astroboy | 0:94897d537b31 | 29 | * @param[in] memBuf Specify memory partition head address. |
astroboy | 0:94897d537b31 | 30 | * @param[in] blockSize Specify memory block size. |
astroboy | 0:94897d537b31 | 31 | * @param[in] blockNum Specify memory block number. |
astroboy | 0:94897d537b31 | 32 | * @param[out] None |
astroboy | 0:94897d537b31 | 33 | * @retval E_CREATE_FAIL Create memory partition fail. |
astroboy | 0:94897d537b31 | 34 | * @retval others Create memory partition successful. |
astroboy | 0:94897d537b31 | 35 | * |
astroboy | 0:94897d537b31 | 36 | * @par Description |
astroboy | 0:94897d537b31 | 37 | * @details This function is called to create a memory partition. |
astroboy | 0:94897d537b31 | 38 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 39 | */ |
astroboy | 0:94897d537b31 | 40 | OS_MMID CoCreateMemPartition(U8* memBuf,U32 blockSize,U32 blockNum) |
astroboy | 0:94897d537b31 | 41 | { |
astroboy | 0:94897d537b31 | 42 | U8 i,j; |
astroboy | 0:94897d537b31 | 43 | U8 *memory; |
astroboy | 0:94897d537b31 | 44 | P_MemBlk memBlk; |
astroboy | 0:94897d537b31 | 45 | memory = memBuf; |
astroboy | 0:94897d537b31 | 46 | |
astroboy | 0:94897d537b31 | 47 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 48 | if(memBuf == Co_NULL) |
astroboy | 0:94897d537b31 | 49 | { |
astroboy | 0:94897d537b31 | 50 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 51 | } |
astroboy | 0:94897d537b31 | 52 | if(blockSize == 0) |
astroboy | 0:94897d537b31 | 53 | { |
astroboy | 0:94897d537b31 | 54 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 55 | } |
astroboy | 0:94897d537b31 | 56 | if((blockSize&0x3) != 0) |
astroboy | 0:94897d537b31 | 57 | { |
astroboy | 0:94897d537b31 | 58 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 59 | } |
astroboy | 0:94897d537b31 | 60 | if(blockNum<=1) |
astroboy | 0:94897d537b31 | 61 | { |
astroboy | 0:94897d537b31 | 62 | return E_CREATE_FAIL; |
astroboy | 0:94897d537b31 | 63 | } |
astroboy | 0:94897d537b31 | 64 | #endif |
astroboy | 0:94897d537b31 | 65 | |
astroboy | 0:94897d537b31 | 66 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 67 | for(i = 0; i < CFG_MAX_MM; i++) |
astroboy | 0:94897d537b31 | 68 | { |
astroboy | 0:94897d537b31 | 69 | if((MemoryIDVessel & (1 << i)) == 0) /* Is free memory ID? */ |
astroboy | 0:94897d537b31 | 70 | { |
astroboy | 0:94897d537b31 | 71 | MemoryIDVessel |= (1<<i); /* Yes,assign ID to this memory block */ |
astroboy | 0:94897d537b31 | 72 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 73 | MemoryTbl[i].memAddr = memory;/* Initialize memory control block*/ |
astroboy | 0:94897d537b31 | 74 | MemoryTbl[i].freeBlock = memory; |
astroboy | 0:94897d537b31 | 75 | MemoryTbl[i].blockSize = blockSize; |
astroboy | 0:94897d537b31 | 76 | MemoryTbl[i].blockNum = blockNum; |
astroboy | 0:94897d537b31 | 77 | memBlk = (P_MemBlk)memory; /* Bulid list in this memory block*/ |
astroboy | 0:94897d537b31 | 78 | for(j=0;j<blockNum-1;j++) |
astroboy | 0:94897d537b31 | 79 | { |
astroboy | 0:94897d537b31 | 80 | memory = memory+blockSize; |
astroboy | 0:94897d537b31 | 81 | memBlk->nextBlock = (P_MemBlk)memory; |
astroboy | 0:94897d537b31 | 82 | memBlk = memBlk->nextBlock; |
astroboy | 0:94897d537b31 | 83 | } |
astroboy | 0:94897d537b31 | 84 | memBlk->nextBlock = Co_NULL; |
astroboy | 0:94897d537b31 | 85 | return i; /* Return memory block ID */ |
astroboy | 0:94897d537b31 | 86 | } |
astroboy | 0:94897d537b31 | 87 | } |
astroboy | 0:94897d537b31 | 88 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 89 | return E_CREATE_FAIL; /* Error return */ |
astroboy | 0:94897d537b31 | 90 | } |
astroboy | 0:94897d537b31 | 91 | |
astroboy | 0:94897d537b31 | 92 | |
astroboy | 0:94897d537b31 | 93 | /** |
astroboy | 0:94897d537b31 | 94 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 95 | * @brief Delete a memory partition |
astroboy | 0:94897d537b31 | 96 | * @param[in] mmID Specify memory partition that want to delete. |
astroboy | 0:94897d537b31 | 97 | * @param[out] None |
astroboy | 0:94897d537b31 | 98 | * @retval E_INVALID_ID The memory partition id passed was invalid,delete fail. |
astroboy | 0:94897d537b31 | 99 | * @retval E_OK Delete successful. |
astroboy | 0:94897d537b31 | 100 | * |
astroboy | 0:94897d537b31 | 101 | * @par Description |
astroboy | 0:94897d537b31 | 102 | * @details This function is called to Delete a memory partition. |
astroboy | 0:94897d537b31 | 103 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 104 | */ |
astroboy | 0:94897d537b31 | 105 | StatusType CoDelMemoryPartition(OS_MMID mmID) |
astroboy | 0:94897d537b31 | 106 | { |
astroboy | 0:94897d537b31 | 107 | P_MM memCtl; |
astroboy | 0:94897d537b31 | 108 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 109 | if(mmID >= CFG_MAX_MM) |
astroboy | 0:94897d537b31 | 110 | { |
astroboy | 0:94897d537b31 | 111 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 112 | } |
astroboy | 0:94897d537b31 | 113 | if( ((1<<mmID)&MemoryIDVessel) == 0) |
astroboy | 0:94897d537b31 | 114 | { |
astroboy | 0:94897d537b31 | 115 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 116 | } |
astroboy | 0:94897d537b31 | 117 | #endif |
astroboy | 0:94897d537b31 | 118 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 119 | memCtl = &MemoryTbl[mmID]; /* Release memory control block */ |
astroboy | 0:94897d537b31 | 120 | MemoryIDVessel &= ~(1<<mmID); |
astroboy | 0:94897d537b31 | 121 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 122 | |
astroboy | 0:94897d537b31 | 123 | memCtl->memAddr = Co_NULL; |
astroboy | 0:94897d537b31 | 124 | memCtl->freeBlock = Co_NULL; |
astroboy | 0:94897d537b31 | 125 | memCtl->blockSize = 0; |
astroboy | 0:94897d537b31 | 126 | memCtl->blockNum = 0; |
astroboy | 0:94897d537b31 | 127 | return E_OK; /* Return OK */ |
astroboy | 0:94897d537b31 | 128 | } |
astroboy | 0:94897d537b31 | 129 | |
astroboy | 0:94897d537b31 | 130 | |
astroboy | 0:94897d537b31 | 131 | /** |
astroboy | 0:94897d537b31 | 132 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 133 | * @brief Get free block number in a memory partition |
astroboy | 0:94897d537b31 | 134 | * @param[in] mmID Specify memory partition. |
astroboy | 0:94897d537b31 | 135 | * |
astroboy | 0:94897d537b31 | 136 | * @param[out] E_INVALID_ID Invalid ID was passed and get counter failure. |
astroboy | 0:94897d537b31 | 137 | * @param[out] E_OK Get current counter successful. |
astroboy | 0:94897d537b31 | 138 | * @retval fbNum The number of free block. |
astroboy | 0:94897d537b31 | 139 | * |
astroboy | 0:94897d537b31 | 140 | * @par Description |
astroboy | 0:94897d537b31 | 141 | * @details This function is called to get free block number in a memory |
astroboy | 0:94897d537b31 | 142 | * partition. |
astroboy | 0:94897d537b31 | 143 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 144 | */ |
astroboy | 0:94897d537b31 | 145 | U32 CoGetFreeBlockNum(OS_MMID mmID,StatusType* perr) |
astroboy | 0:94897d537b31 | 146 | { |
astroboy | 0:94897d537b31 | 147 | U32 fbNum; |
astroboy | 0:94897d537b31 | 148 | P_MM memCtl; |
astroboy | 0:94897d537b31 | 149 | P_MemBlk memBlk; |
astroboy | 0:94897d537b31 | 150 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 151 | if(mmID >= CFG_MAX_MM) |
astroboy | 0:94897d537b31 | 152 | { |
astroboy | 0:94897d537b31 | 153 | *perr = E_INVALID_ID; |
astroboy | 0:94897d537b31 | 154 | return 0; |
astroboy | 0:94897d537b31 | 155 | } |
astroboy | 0:94897d537b31 | 156 | if( ((1<<mmID)&MemoryIDVessel) == 0) |
astroboy | 0:94897d537b31 | 157 | { |
astroboy | 0:94897d537b31 | 158 | *perr = E_INVALID_ID; /* Invalid memory id,return 0 */ |
astroboy | 0:94897d537b31 | 159 | return 0; |
astroboy | 0:94897d537b31 | 160 | } |
astroboy | 0:94897d537b31 | 161 | #endif |
astroboy | 0:94897d537b31 | 162 | memCtl = &MemoryTbl[mmID]; |
astroboy | 0:94897d537b31 | 163 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 164 | memBlk = (P_MemBlk)(memCtl->freeBlock);/* Get the free item in memory list*/ |
astroboy | 0:94897d537b31 | 165 | fbNum = 0; |
astroboy | 0:94897d537b31 | 166 | while(memBlk != Co_NULL) /* Get counter of free item */ |
astroboy | 0:94897d537b31 | 167 | { |
astroboy | 0:94897d537b31 | 168 | fbNum++; |
astroboy | 0:94897d537b31 | 169 | memBlk = memBlk->nextBlock; /* Get next free iterm */ |
astroboy | 0:94897d537b31 | 170 | } |
astroboy | 0:94897d537b31 | 171 | OsSchedUnlock(); /* Unlock schedul */ |
astroboy | 0:94897d537b31 | 172 | *perr = E_OK; |
astroboy | 0:94897d537b31 | 173 | return fbNum; /* Return the counter of free item */ |
astroboy | 0:94897d537b31 | 174 | } |
astroboy | 0:94897d537b31 | 175 | |
astroboy | 0:94897d537b31 | 176 | |
astroboy | 0:94897d537b31 | 177 | /** |
astroboy | 0:94897d537b31 | 178 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 179 | * @brief Get a memory buffer from memory partition |
astroboy | 0:94897d537b31 | 180 | * @param[in] mmID Specify memory partition that want to assign buffer. |
astroboy | 0:94897d537b31 | 181 | * @param[out] None |
astroboy | 0:94897d537b31 | 182 | * @retval Co_NULL Assign buffer fail. |
astroboy | 0:94897d537b31 | 183 | * @retval others Assign buffer successful,and return the buffer pointer. |
astroboy | 0:94897d537b31 | 184 | * |
astroboy | 0:94897d537b31 | 185 | * @par Description |
astroboy | 0:94897d537b31 | 186 | * @details This function is called to Delete a memory partition. |
astroboy | 0:94897d537b31 | 187 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 188 | */ |
astroboy | 0:94897d537b31 | 189 | void* CoGetMemoryBuffer(OS_MMID mmID) |
astroboy | 0:94897d537b31 | 190 | { |
astroboy | 0:94897d537b31 | 191 | P_MM memCtl; |
astroboy | 0:94897d537b31 | 192 | P_MemBlk memBlk; |
astroboy | 0:94897d537b31 | 193 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 194 | if(mmID >= CFG_MAX_MM) |
astroboy | 0:94897d537b31 | 195 | { |
astroboy | 0:94897d537b31 | 196 | return Co_NULL; |
astroboy | 0:94897d537b31 | 197 | } |
astroboy | 0:94897d537b31 | 198 | if( ((1<<mmID)&MemoryIDVessel) == 0) |
astroboy | 0:94897d537b31 | 199 | { |
astroboy | 0:94897d537b31 | 200 | return Co_NULL; |
astroboy | 0:94897d537b31 | 201 | } |
astroboy | 0:94897d537b31 | 202 | #endif |
astroboy | 0:94897d537b31 | 203 | memCtl = &MemoryTbl[mmID]; |
astroboy | 0:94897d537b31 | 204 | OsSchedLock(); /* Lock schedule */ |
astroboy | 0:94897d537b31 | 205 | if(memCtl->freeBlock == Co_NULL ) /* Is there no free item in memory list */ |
astroboy | 0:94897d537b31 | 206 | { |
astroboy | 0:94897d537b31 | 207 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 208 | return Co_NULL; /* Yes,error return */ |
astroboy | 0:94897d537b31 | 209 | } |
astroboy | 0:94897d537b31 | 210 | memBlk = (P_MemBlk)memCtl->freeBlock; /* Get free memory block */ |
astroboy | 0:94897d537b31 | 211 | memCtl->freeBlock = (U8*)memBlk->nextBlock; /* Reset the first free item */ |
astroboy | 0:94897d537b31 | 212 | OsSchedUnlock(); /* Unlock schedule */ |
astroboy | 0:94897d537b31 | 213 | return memBlk; /* Return free memory block address */ |
astroboy | 0:94897d537b31 | 214 | } |
astroboy | 0:94897d537b31 | 215 | |
astroboy | 0:94897d537b31 | 216 | |
astroboy | 0:94897d537b31 | 217 | |
astroboy | 0:94897d537b31 | 218 | /** |
astroboy | 0:94897d537b31 | 219 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 220 | * @brief Free a memory buffer to memory partition |
astroboy | 0:94897d537b31 | 221 | * @param[in] mmID Specify memory partition. |
astroboy | 0:94897d537b31 | 222 | * @param[in] buf Specify memory buffer that want to free. |
astroboy | 0:94897d537b31 | 223 | * @param[out] None |
astroboy | 0:94897d537b31 | 224 | * @retval E_INVALID_ID The memory partition id passed was invalid. |
astroboy | 0:94897d537b31 | 225 | * @retval E_INVALID_PARAMETER The parameter passed was invalid. |
astroboy | 0:94897d537b31 | 226 | * @retval E_OK Free successful. |
astroboy | 0:94897d537b31 | 227 | * |
astroboy | 0:94897d537b31 | 228 | * @par Description |
astroboy | 0:94897d537b31 | 229 | * @details This function is called to Delete a memory partition. |
astroboy | 0:94897d537b31 | 230 | ******************************************************************************* |
astroboy | 0:94897d537b31 | 231 | */ |
astroboy | 0:94897d537b31 | 232 | StatusType CoFreeMemoryBuffer(OS_MMID mmID,void* buf) |
astroboy | 0:94897d537b31 | 233 | { |
astroboy | 0:94897d537b31 | 234 | P_MM memCtl; |
astroboy | 0:94897d537b31 | 235 | P_MemBlk memBlk; |
astroboy | 0:94897d537b31 | 236 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 237 | if(mmID >= CFG_MAX_MM) |
astroboy | 0:94897d537b31 | 238 | { |
astroboy | 0:94897d537b31 | 239 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 240 | } |
astroboy | 0:94897d537b31 | 241 | if( ((1<<mmID)&MemoryIDVessel) == 0) |
astroboy | 0:94897d537b31 | 242 | { |
astroboy | 0:94897d537b31 | 243 | return E_INVALID_ID; |
astroboy | 0:94897d537b31 | 244 | } |
astroboy | 0:94897d537b31 | 245 | if(buf == Co_NULL) |
astroboy | 0:94897d537b31 | 246 | { |
astroboy | 0:94897d537b31 | 247 | return E_INVALID_PARAMETER; |
astroboy | 0:94897d537b31 | 248 | } |
astroboy | 0:94897d537b31 | 249 | #endif |
astroboy | 0:94897d537b31 | 250 | |
astroboy | 0:94897d537b31 | 251 | memCtl = &MemoryTbl[mmID]; |
astroboy | 0:94897d537b31 | 252 | #if CFG_PAR_CHECKOUT_EN >0 /* Check validity of parameter */ |
astroboy | 0:94897d537b31 | 253 | if((U32)buf < (U32)(memCtl->memAddr)) |
astroboy | 0:94897d537b31 | 254 | { |
astroboy | 0:94897d537b31 | 255 | return E_INVALID_PARAMETER; |
astroboy | 0:94897d537b31 | 256 | } |
astroboy | 0:94897d537b31 | 257 | if((U32)buf > (U32)(memCtl->memAddr + memCtl->blockSize*memCtl->blockNum)) |
astroboy | 0:94897d537b31 | 258 | { |
astroboy | 0:94897d537b31 | 259 | return E_INVALID_PARAMETER; |
astroboy | 0:94897d537b31 | 260 | } |
astroboy | 0:94897d537b31 | 261 | if(((U32)buf - (U32)(memCtl->memAddr))%(memCtl->blockSize) != 0) |
astroboy | 0:94897d537b31 | 262 | { |
astroboy | 0:94897d537b31 | 263 | return E_INVALID_PARAMETER; |
astroboy | 0:94897d537b31 | 264 | } |
astroboy | 0:94897d537b31 | 265 | #endif |
astroboy | 0:94897d537b31 | 266 | memBlk = (P_MemBlk)buf; /* Reset the first free item */ |
astroboy | 0:94897d537b31 | 267 | OsSchedLock(); |
astroboy | 0:94897d537b31 | 268 | memBlk->nextBlock = (P_MemBlk)memCtl->freeBlock; |
astroboy | 0:94897d537b31 | 269 | memCtl->freeBlock = buf; |
astroboy | 0:94897d537b31 | 270 | OsSchedUnlock(); |
astroboy | 0:94897d537b31 | 271 | return E_OK; /* Return OK */ |
astroboy | 0:94897d537b31 | 272 | } |
astroboy | 0:94897d537b31 | 273 | |
astroboy | 0:94897d537b31 | 274 | #endif |
astroboy | 0:94897d537b31 | 275 |