Moves stack pointer to next block. If no blocks, allocate new one and link it to the storage: */
| 206 | /* Moves stack pointer to next block. |
| 207 | If no blocks, allocate new one and link it to the storage: */ |
| 208 | static void |
| 209 | icvGoNextMemBlock( CvMemStorage * storage ) |
| 210 | { |
| 211 | if( !storage ) |
| 212 | CV_Error( CV_StsNullPtr, "" ); |
| 213 | |
| 214 | if( !storage->top || !storage->top->next ) |
| 215 | { |
| 216 | CvMemBlock *block; |
| 217 | |
| 218 | if( !(storage->parent) ) |
| 219 | { |
| 220 | block = (CvMemBlock *)cvAlloc( storage->block_size ); |
| 221 | } |
| 222 | else |
| 223 | { |
| 224 | CvMemStorage *parent = storage->parent; |
| 225 | CvMemStoragePos parent_pos; |
| 226 | |
| 227 | cvSaveMemStoragePos( parent, &parent_pos ); |
| 228 | icvGoNextMemBlock( parent ); |
| 229 | |
| 230 | block = parent->top; |
| 231 | cvRestoreMemStoragePos( parent, &parent_pos ); |
| 232 | |
| 233 | if( block == parent->top ) /* the single allocated block */ |
| 234 | { |
| 235 | assert( parent->bottom == block ); |
| 236 | parent->top = parent->bottom = 0; |
| 237 | parent->free_space = 0; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | /* cut the block from the parent's list of blocks */ |
| 242 | parent->top->next = block->next; |
| 243 | if( block->next ) |
| 244 | block->next->prev = parent->top; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* link block */ |
| 249 | block->next = 0; |
| 250 | block->prev = storage->top; |
| 251 | |
| 252 | if( storage->top ) |
| 253 | storage->top->next = block; |
| 254 | else |
| 255 | storage->top = storage->bottom = block; |
| 256 | } |
| 257 | |
| 258 | if( storage->top->next ) |
| 259 | storage->top = storage->top->next; |
| 260 | storage->free_space = storage->block_size - sizeof(CvMemBlock); |
| 261 | assert( storage->free_space % CV_STRUCT_ALIGN == 0 ); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /* Remember memory storage position: */ |
no test coverage detected