这个=。=全是泪,不说了,写下来的结果似乎是57/60,已经不错了那=。=
至于mdriver测试如下
mdriver -V
92/100
好了,废话不多说了,代码如下
/*
* mm.c
*the data structure is explict list,and use the binary tree
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include "mm.h"
#include "memlib.h"
team_t team = {
};
#define WSIZE 4
#define DSIZE 8
//max,you know it~.~
#define MAX(x,y) ((x)>(y)? (x): (y))
//get and put the value from the address p~.~
#define GET(p) (*(size_t *)(p))
#define PUT(p,val) (*(size_t *)(p)=(val))
//get the size of the block from the header p of the block~.~
#define SIZE(p) ((GET(p))&~0x7)
#define PACK(size,alloc) ((size)|(alloc))
//get everyting from the block bp~.~
#define ALLOC(bp) (GET(bp)&0x1)
#define HEAD(bp) ((void *)(bp)-WSIZE)
#define LEFT(bp) ((void *)(bp))
#define RIGHT(bp) ((void *)(bp)+WSIZE)
#define PRNT(bp) ((void *)(bp)+DSIZE)
#define BROS(bp) ((void *)(bp)+(3*WSIZE))
#define FOOT(bp) ((void *)(bp)+SIZE(HEAD(bp))-DSIZE)
//get the size aligned~.~
#define ALIGNED(size) (((size) + 0x7) & ~0x7)
//get the size or the allocness of the block bp~.~
#define GET_HEAD_SIZE(bp) SIZE(HEAD(bp))
#define GET_HEAD_ALLOC(bp) (GET(HEAD(bp))&0x1)
//get the previous or next block~.~
#define PREV_BLKP(bp) ((void *)(bp)-SIZE(((void *)(bp)-DSIZE)))
#define NEXT_BLKP(bp) ((void *)(bp)+GET_HEAD_SIZE(bp))
//get or set the left or right child of bp~.~
#define PUT_LEFT_CHILD(bp,val) (PUT(LEFT(bp),(int)val))
#define PUT_RIGHT_CHILD(bp,val) (PUT(RIGHT(bp),(int)val))
#define GET_LEFT_CHILD(bp) (GET(LEFT(bp)))
#define GET_RIGHT_CHILD(bp) (GET(RIGHT(bp)))
//get or set the head or foot of bp~.~
#define PUT_HEAD(bp,val) (PUT(HEAD(bp),(int)val))
#define PUT_FOOT(bp,val) (PUT(FOOT(bp),(int)val))
#define PUT_PACK_HEAD(bp,size,alloc) (PUT_HEAD(bp,PACK(size,alloc)))
#define PUT_PACK_FOOT(bp,size,alloc) (PUT_FOOT(bp,PACK(size,alloc)))
#define GET_HEAD(bp) (GET(HEAD(bp)))
#define GET_FOOT(bp) (GET(FOOT(bp)))
//get the parent or brother of the block bp~.~
#define PUT_PAR(bp,val) (PUT(PRNT(bp),(int)val))
#define PUT_BROS(bp,val) (PUT(BROS(bp),(int)val))
#define GET_PAR(bp) (GET(PRNT(bp)))
#define GET_BRO(bp) (GET(BROS(bp)))
int mm_init ();
void *mm_malloc (size_t size);
void mm_free (void *bp);
void *mm_realloc (void *bp,size_t size);
//declear the function and variable~.~
static void *coalesce (void *bp);
static void *extend_heap (size_t size);
static void place (void *ptr,size_t asize);
static void delete_node (void *bp);
static void add_node (void *bp);
static void *find_fit (size_t asize);
static void check(void *bp);
static void mm_check();
static void *heap_list_ptr = 0;
static void *my_tree = 0;
static size_t flag = 0;
//check one address
static void check(void *bp)
{
printf("0.the adress is 0x%x\n",bp);
printf("1.the size of the block is %d\n",GET_HEAD_SIZE(bp));
printf("2.the allocness of the block is %d\n",GET_HEAD_ALLOC(bp));
printf("-1.the size of next block is %d\n",GET_HEAD_SIZE(NEXT_BLKP(bp)));
printf("-2.the allocness of next block is %d\n",GET_HEAD_ALLOC(NEXT_BLKP(bp)));
printf("0x%x[%d/%d] ~ 0x%x[%d/%d]\n",HEAD(bp),GET_HEAD_SIZE(bp),GET_HEAD_ALLOC(bp)\
,FOOT(bp),GET_HEAD_SIZE(bp),GET_HEAD_ALLOC(bp));
printf("0x%x[%d/%d] ~ 0x%x[%d/%d]\n\n",HEAD(NEXT_BLKP(bp)),\
GET_HEAD_SIZE(NEXT_BLKP(bp)),GET_HEAD_ALLOC(NEXT_BLKP(bp))\
,FOOT(bp),GET_HEAD_SIZE(NEXT_BLKP(bp)),GET_HEAD_ALLOC(NEXT_BLKP(bp)));
}
//check all the heap
static void mm_check()
{
void *buff = 0;
buff = heap_list_p