| | |
| | | #include "mm.h" |
| | | #include "sem_util.h" |
| | | #include "logger_factory.h" |
| | | #include "bh_api.h" |
| | | #include <sys/sem.h> |
| | | #include <sys/shm.h> |
| | | |
| | |
| | | /* |
| | | * mm_free - Free a block |
| | | */ |
| | | void mm_free(void *ptr) |
| | | void mm_free(void *ptr, int enable) |
| | | { |
| | | if (ptr == 0) |
| | | return; |
| | | if ((ptr == 0) || (*(size_t *)(ptr - SIZE_T_SIZE) == 0x00)) |
| | | return; |
| | | |
| | | /* |
| | | *if (!is_allocated(ptr) ) { |
| | |
| | | *} |
| | | */ |
| | | |
| | | SemUtil::dec_uni(mutex); |
| | | if (enable == true) { |
| | | SemUtil::dec_uni(mutex); |
| | | } |
| | | ptr -= SIZE_T_SIZE; |
| | | size_t size = GET_SIZE(HDRP(ptr)); |
| | | PUT(HDRP(ptr), PACK(size, 0)); |
| | | PUT(FTRP(ptr), PACK(size, 0)); |
| | | *(size_t *)ptr = 0x00; |
| | | coalesce(ptr); |
| | | SemUtil::inc(mutex); |
| | | if (enable == true) { |
| | | SemUtil::inc(mutex); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | /* |
| | | * mm_realloc - Naive implementation of realloc |
| | |
| | | PUT(HDRP(bp), PACK(size, 0)); /* Free block header */ //line:vm:mm:freeblockhdr |
| | | PUT(FTRP(bp), PACK(size, 0)); /* Free block footer */ //line:vm:mm:freeblockftr |
| | | PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */ //line:vm:mm:newepihdr |
| | | |
| | | |
| | | /* Coalesce if the previous block was free */ |
| | | return coalesce(bp); //line:vm:mm:returnblock |
| | | } |
| | | |
| | | static void insert_fblock (void *bp) |
| | | { |
| | | //后进先出的方式插入,即插入链表头位置 |
| | | |
| | | // insert into the header of the free list |
| | | PUT_PTR(SUCCRP(bp), NEXT_FBLKP(heap_listp)); //the successor of bp point to the old first free block |
| | | PUT_PTR(PREDRP(NEXT_FBLKP(heap_listp)), bp); //the predecessor of the old first free block point to bp |
| | |
| | | PUT(FTRP(bp), PACK(csize, 1)); |
| | | rm_fblock(bp); |
| | | } |
| | | return bp; |
| | | |
| | | *(size_t *)bp = inter_key_get(); |
| | | |
| | | return (bp + SIZE_T_SIZE); |
| | | } |
| | | |
| | | static int is_allocated(void *ptr) |
| | |
| | | |
| | | } |
| | | |
| | | void find_mm_data(int val) |
| | | { |
| | | void *bp = heap_listp; |
| | | |
| | | SemUtil::dec(mutex); |
| | | for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) |
| | | { |
| | | if (GET_ALLOC(HDRP(bp))) { |
| | | if ((*(size_t *)bp) == val) { |
| | | mm_free(bp + SIZE_T_SIZE, false); |
| | | } |
| | | } |
| | | } |
| | | SemUtil::inc(mutex); |
| | | |
| | | return; |
| | | } |
| | | |
| | | /* |
| | | * find_fit - Find a fit for a block with size bytes |
| | | */ |
| | |
| | | if (!GET_ALLOC(HDRP(bp)) && (size <= GET_SIZE(HDRP(bp)))) |
| | | { |
| | | return bp; |
| | | } else if (GET_ALLOC(HDRP(bp)) && (GET_SIZE(HDRP(bp)) == 0)) |
| | | { |
| | | break; |
| | | } |
| | | } |
| | | return NULL; /* No fit */ |