void * dkbtalloc(size_t n){ /* * The memory we search for has to include the memory control header, but * the user of malloc doesn't need to know this, so we'll just add it in * for them. */ n=n+sizeof(node_t); node_t *p= AVAIL->nlink; /* from the free list head */ node_t * q=NULL; /* to store the block after spliting */ while(p!=NULL){ if (p->size==n){ /* exaclly the same size */ block_remove(p); /* remove p from dll */ set_block_used(p); /* mark p as allocated */ set_prev_used(get_block_pnxt(p));/* set p's next physical block */ return (void*) p; /* return block p */ } if (p->size>n){ /* suitable block is bigger than n */ q=block_split(p,n); /* splict p, q is the new block(right part) */ set_block_free(q); /* set new left block as free */ set_prev_used(q); /* the previous of q is used */ set_block_used(p); block_remove(p); return (void*)p; } p=p->nlink; /* move step further */ } return NULL; }
/*merger two blocks, return left one (address order)*/ node_t * block_merge_right(node_t * b1, node_t * b2){ if((b2!=NULL) &&(block_is_free(b2))){ /*right adjacent block is free */ b1->size+=b2->size; block_remove(b2); /* remove from dll */ return b1; } else return b1; /* right adjecent is end or not free */ }
/*merger two blocks, return left one (address order)*/ node_t * block_merge_left(node_t * b1, node_t * b2){ if ((b1!=AVAIL) && (block_is_free(b1))){ /* left adjacent block is free*/ block_remove(b1); /* remove from dll */ b1->size+=b2->size; /* size includes the header */ return b1; } else if((b1==AVAIL)||(!block_is_free(b1))){ return b2; } return b2; }
bool got_block_hit() { struct listNode* current = first; while ((current != NULL) && !(((bouncer_x + BOUNCER_SIZE > current->x) && (bouncer_x < current->x + BLOCK_W)) && ((bouncer_y + BOUNCER_SIZE > current->y) && (bouncer_y < current->y + BLOCK_H)))) { current = current->next; } if (current == NULL) { return false; } else { block_remove(current->id); return true; } }