t_block *block_copy(t_block *block) { t_context *C=ctx_get(); scene_store(C->scene,1); t_node *clone_node = block_make(block->id.name,block->type); t_block *clone_block = clone_node->data; clone_block->block_state.draw_outline = block->block_state.draw_outline; _add_block(C,clone_block); vcp(clone_block->pos,block->pos); t_link *l; t_brick *b; for(l=block->bricks->first;l;l=l->next) { b=l->data; brick_copy(clone_block,b); } scene_store(C->scene,0); return clone_block; }
t_block *block_clone(t_block *block) { t_context *C=ctx_get(); C->scene->store=1; t_node *clone_node=block_make(block->name,block->type); t_block *clone_block=clone_node->data; clone_block->state.draw_outline = block->state.draw_outline; _add_block(C,clone_block); t_link *l; t_brick *b; for(l=block->bricks->first;l;l=l->next) { b=l->data; brick_clone(clone_block,b); } C->scene->store=0; return clone_block; }
static int add_block(ospfs_inode_t *oi) { // current number of blocks in file uint32_t n = ospfs_size2nblocks(oi->oi_size); int32_t next_block = n + 1; int32_t retval = -ENOSPC; uint32_t block_no = 0; uint32_t *ptr; // keep track of allocations to free in case of -ENOSPC uint32_t allocated[2] = { 0, 0 }; /* EXERCISE: Your code here */ if (0 < next_block && next_block <= OSPFS_NDIRECT) { if (_add_block(&oi->oi_direct[next_block - 1])) { retval = 0; } goto out; } // The block to be allocated has to be in indirect or indirect2 next_block -= OSPFS_NDIRECT; if (0 < next_block && next_block <= OSPFS_NINDIRECT) { if (!oi->oi_indirect) { if (!(allocated[0] = _add_block(&oi->oi_indirect))) { goto out; } } ptr = ospfs_block(oi->oi_indirect); if (!_add_block(&ptr[next_block - 1])) { if (allocated[0]) { free_block(allocated[0]); oi->oi_indirect = 0; } goto out; } retval = 0; goto out; } // next block must lie in indirect2 next_block -= OSPFS_NINDIRECT; if (0 < next_block && next_block <= (OSPFS_NINDIRECT * OSPFS_NINDIRECT)) { uint32_t *ptrl1, *ptrl2; uint32_t level1 = (next_block - 1) / OSPFS_NINDIRECT; uint32_t level2 = (next_block - 1) % OSPFS_NINDIRECT; if (!oi->oi_indirect2) { if (!(allocated[0] = _add_block(&oi->oi_indirect2))) { goto out; } } ptrl1 = ospfs_block(oi->oi_indirect2); if (!ptrl1[level1]) { if (!(allocated[1] = _add_block(&ptrl1[level1]))) { if (allocated[0]) { free_block(allocated[0]); oi->oi_indirect2 = 0; } goto out; } } ptrl2 = ospfs_block(ptrl1[level1]); if (!ptr[level2]) { if (!_add_block(&ptr[level2])) { if (allocated[0]) { free_block(allocated[0]); oi->oi_indirect2 = 0; } if (allocated[1]) { free_block(allocated[1]); ((uint32_t *) ospfs_block(allocated[1])) [level1] = 0; } goto out; } retval = 0; goto out; } } out: return retval; }