/* * mm_malloc - Allocate a block with at least size bytes of payload * This function takes into account alignment and how the heap space * is organized during any given malloc call. * * The adjusted block size is calculated by taking the max of the * minimum size (24 bytes) and the requested size (aligned size + 8). * * It then searches the free list until it finds a place to put the block. * Using this block pointer, it places the block in this spot. * * If no space was found, we must ask for more memory and then place at * the block at the start of the new heap memory. * * This function takes a payload size as a parameter and returns * a pointer to the start of the alocated block. */ void *mm_malloc(size_t size) { size_t asize; /* adjusted block size */ size_t extendsize; /* amount to extend heap if no fit */ char *bp; /* Ignore spurious requests */ if (size <= 0) return NULL; /* Adjust block size to include overhead and alignment reqs */ asize = MAX(ALIGN(size) + DSIZE, MINIMUM); /* Search the free list for a fit */ if ((bp = findFit(asize))) { place(bp, asize); return bp; } /* No fit found. Get more memory and place the block */ extendsize = MAX(asize, CHUNKSIZE); //return NULL if unable to get heap space if ((bp = extendHeap(extendsize/WSIZE)) == NULL) return NULL; place(bp, asize); return bp; }
/* * mm_malloc - The allocator * malloc allocates free blocks on the heap. * It first aligns the size by rounding size to * the nearest multiple of ALIGNMENT(8) and then * adds DSIZE to this value to accomodate the header * and footer. We then call findFit to find a free block * that can accomodate this size. After doing so, we * set the allocated bit in the header and return the * current block pointer. */ void *mm_malloc(size_t size) { size_t asize; /* adjusted block size */ size_t extendsize; /* amount to extend heap if no fit */ char *bp; /* Block pointer to be returned. */ /* Ignore spurious requests */ if (size <= 0) return NULL; /* Adjust block size to include overhead(by adding DSIZE) for header+footer * and alignment reqs */ asize = MAX(ALIGN(size) + DSIZE, MINBLOCKSIZE); /* Search the free list for a fit */ if ((bp = findFit(asize)) != NULL) { place(bp, asize); //mm_checkheap(0); return bp; } /* No fit found. Get more memory and place the block */ extendsize = MAX(asize, CHUNKSIZE); /* Return NULL if unable to get heap space */ if ((bp = extendHeap(extendsize/WSIZE)) == NULL) return NULL; place(bp, asize); //mm_checkheap(0); return bp; }
void *memmalloc(size_t size) { size_t asize; size_t extendsize; char *bp; if (size == 0) return NULL; //块大小要能够包含头部和尾部以及前后向指针 if (size <= DSIZE) asize = 4 * DSIZE; else asize = DSIZE * ((size + (DSIZE)+(DSIZE - 1)) / DSIZE); //寻找合适大小的块 if ((bp = (char*)findFit(asize)) != NULL) { place(bp, asize); return bp; } //没有合适大小的块就要重新申请 extendsize = MAX(asize,CHUNKSIZE); if ((bp = (char*)extendHeap(extendsize/WSIZE)) == NULL) return NULL; //将新分配的块放在空闲队列头部 PUT(bp, &head); PUT(bp + (1 * WSIZE), (void*)head.next); if (head.next != NULL) PUT((void*)head.next, FTRP(bp) + DSIZE); head.next = bp; place(bp, asize); return bp; }
/* * malloc: adjust the size to the minimum block size or a * multiple of alignment. Search the free list for a fit, * if not, extend the heap */ void *malloc (size_t size) { //checkheap(1); // Let's make sure the heap is ok! size_t asize; // adjust size size_t extendsize; char *bp; if (size <= 0){ return NULL; } // Adjust the size to at least the minimum blk size and // a multiple of alignment 8 if (size <= (DSIZE + WSIZE)){ asize = 2 * DSIZE; } else{ asize = DSIZE*((size + DSIZE + (DSIZE - 1)) / DSIZE); } // search the freelist for a fit if ((bp = findFit(asize)) != NULL){ place(bp, asize); return bp; } // If no fit, extend the heap if(asize>CHUNKSIZE) extendsize = asize; else extendsize = CHUNKSIZE; if ((bp = extendHeap(extendsize / WSIZE)) == NULL) return NULL; place(bp, asize); return bp; }