コード例 #1
0
ファイル: malloclab.c プロジェクト: ktlwik/System-Programming
static void place(void *bp, size_t asize){

	size_t csize = HDRSIZE(bp);



	if ((csize - asize) >= BSIZE) {

		PUT(HDRP(bp), PACK(asize, 1));

		PUT(FTRP(bp), PACK(asize, 1));

		del(bp); // deleting current block

		bp = NEXT_BLKP(bp);

		PUT(HDRP(bp), PACK(csize-asize, 0));

		PUT(FTRP(bp), PACK(csize-asize, 0));

		coalesce(bp);

	}

	else {

		PUT(HDRP(bp), PACK(csize, 1));

		PUT(FTRP(bp), PACK(csize, 1));

		del(bp);

	}

}
コード例 #2
0
ファイル: xform_ah.c プロジェクト: dcui/FreeBSD-9.3_kernel
size_t
ah_hdrsiz(struct secasvar *sav)
{
	size_t size;

	if (sav != NULL) {
		int authsize;
		IPSEC_ASSERT(sav->tdb_authalgxform != NULL, ("null xform"));
		/*XXX not right for null algorithm--does it matter??*/
		authsize = AUTHSIZE(sav);
		size = roundup(authsize, sizeof (u_int32_t)) + HDRSIZE(sav);
	} else {
		/* default guess */
		size = sizeof (struct ah) + sizeof (u_int32_t) + 16;
	}
	return size;
}
コード例 #3
0
ファイル: malloclab.c プロジェクト: ktlwik/System-Programming
static void *find_fit(size_t asize){

	void *bp;



	/* for loop through list to find first fit */

	for (bp = lptr; GET_HDRALLOC(bp) == 0; bp = NEXT_PTR(bp))

		if (asize <= (size_t) HDRSIZE(bp))

			return bp;

	return NULL; /* returns NULL if can't find it in the list */

}
コード例 #4
0
ファイル: malloclab.c プロジェクト: ktlwik/System-Programming
void mm_free(void *bp){

	/* just return if the pointer is NULL */

	if(bp == NULL) return;

	size_t size = HDRSIZE(bp);



	/* set this block back to free and coalese */

	PUT(HDRP(bp), PACK(size, 0));

	PUT(FTRP(bp), PACK(size, 0));

	coalesce(bp); // merging block if we need

}
コード例 #5
0
ファイル: malloclab.c プロジェクト: ktlwik/System-Programming
static void *coalesce(void *bp){

	size_t prev_alloc;

	prev_alloc = GET_FTRALLOC(PREV_BLKP(bp)) || PREV_BLKP(bp) == bp;

	size_t next_alloc = GET_HDRALLOC(NEXT_BLKP(bp));

	size_t size = HDRSIZE(bp);



	/* Case 1 next block is free */

	if (prev_alloc && !next_alloc) {

		size += HDRSIZE(NEXT_BLKP(bp));

		del(NEXT_BLKP(bp)); // delete next block pointer

		PUT(HDRP(bp), PACK(size, 0));

		PUT(FTRP(bp), PACK(size, 0));

	}/* Case 2 prev block is free */

    if (!prev_alloc && next_alloc) {

		size += HDRSIZE(PREV_BLKP(bp));

		bp = PREV_BLKP(bp);

		del(bp); // delete block

		PUT(HDRP(bp), PACK(size, 0)); // merge it sizes to the previous block

		PUT(FTRP(bp), PACK(size, 0));

	}/* Case 3 both blocks are free */

    if (!prev_alloc && !next_alloc) {

		size += HDRSIZE(PREV_BLKP(bp)) + HDRSIZE(NEXT_BLKP(bp));

		del(PREV_BLKP(bp)); // delete previous block

		del(NEXT_BLKP(bp)); // delete next block

        // merge all 3 sizes to the current block

		bp = PREV_BLKP(bp);

		PUT(HDRP(bp), PACK(size, 0));

		PUT(FTRP(bp), PACK(size, 0));

	}/* lastly insert bp into free list and return bp */

	insert(bp);

	return bp;

}
コード例 #6
0
ファイル: malloclab.c プロジェクト: ktlwik/System-Programming
void *mm_realloc (void *bp, size_t size){

	if(size == 0) { // if size == 0 then we can just free bp and return the pointer

		mm_free (bp);

		return NULL;

	}

    if(bp == NULL) { // if bp == NULL then we just allocating a block for appropriate size

		bp = mm_malloc(size);

		return bp;

	}

    // this is the normal case that we will usually face

    size_t cursz = HDRSIZE(bp); // size of the current block

    size_t nsz = ALIGN(size + OVERHEAD); // alligning size, so that it will satisfy our requirements of block size

    /* if the size fits we just return bp */

    if(nsz <= cursz) return bp; // if the size we need to allocate is less or equal than the size we have at current block

    else { // then we can just allocate a block of specific size

            // when our block size is bigger than our current size at bpointer bp

        size_t next_alloc = GET_HDRALLOC(NEXT_BLKP(bp));

        size_t csize;

        size_t asize;

        /* if next block + current free block >= current size (is ok to fit) then we just fitting to it our block */

        if(!next_alloc && ((csize = cursz + HDRSIZE(NEXT_BLKP(bp)))) >= nsz){

            del(NEXT_BLKP(bp)); // we need to delete next block pointer

            PUT(HDRP(bp), PACK(csize, 1)); // and merge them

            PUT(FTRP(bp), PACK(csize, 1));

            return bp;

        }

        /* if the next block is also free and this block is right before the epilogue block */

        if(!next_alloc && ((HDRSIZE(NEXT_BLKP(NEXT_BLKP(bp)))) == 0)){

            csize = nsz - cursz + HDRSIZE(NEXT_BLKP(bp)); // is equal to our new size - current siz + SIZE of next block

            void *temp = extend_heap(csize); // we need to extend heaop by csize

            asize = cursz + HDRSIZE(temp); // our new size curesize + the ammount we extended

            PUT(HDRP(bp), PACK(asize, 1));

            PUT(FTRP(bp), PACK(asize, 1));

            return bp;

        }

        /* the similar case to previous one but here bp already is right before the epilogue */

        if(HDRSIZE(NEXT_BLKP(bp)) == 0){

            csize = nsz - cursz;

            void *temp = extend_heap(csize);

            asize = cursz + HDRSIZE(temp);

            PUT(HDRP(bp), PACK(asize, 1));

            PUT(FTRP(bp), PACK(asize, 1));

            return bp;

        }

        /* if there was not enough space for our new block then we need just ack for additional size */

        void *newbp = mm_malloc(nsz);  /* allocating a space we need */

        place(newbp, nsz); /* placing it */

        memcpy(newbp, bp, nsz); /* copy to newbp from bp nsz bytes */

        mm_free(bp); /* free a block at pointer bp */

        return newbp; /* returning our new pointer */

    }

    /* in any other case returning NULL */

	return NULL;

}