Esempio n. 1
0
File: DKbt_p.c Progetto: fangbin/mem
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;
    
}
Esempio n. 2
0
int get_free_block(jfs_t *jfs) 
{
    int groups, i;

    if (jfs->d_img->size % (BLOCKSIZE * BLOCKS_PER_GROUP) == 0) {
	groups = jfs->d_img->size / (BLOCKSIZE * BLOCKS_PER_GROUP);
    } else {
	groups = 1 + jfs->d_img->size / (BLOCKSIZE * BLOCKS_PER_GROUP);
    }

    for (i = 0; i < groups; i++) {
	struct blockgroup *grp;
	char block[BLOCKSIZE];
	int freeblock;
	/* read the blockgroup header */
	jfs_read_block(jfs, block, i * BLOCKS_PER_GROUP);
	grp = (struct blockgroup *)block;
	freeblock = -1;
	freeblock = find_free_block(grp->block_bitmap);
	if (freeblock >= 0) {
	    set_block_used(grp->block_bitmap, freeblock);
	    jfs_write_block(jfs, block, i * BLOCKS_PER_GROUP);
	    /* if there's a free block in this group, then return it */
	    freeblock += i * BLOCKS_PER_GROUP;
	    /* printf("allocating free block: %d\n", freeblock);*/
	    return freeblock;
	} 
    }
    return -1;
}