







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








l
st^
exam (ARM 0131)
st^
exam (6:00-7:15)
l
l
l
l
l
blocks
l
l
l
16/
24/
8/
24/
0/
8/
l
l
l
l
#include <stdio.h>#include <unistd.h>typedef struct _header {
unsigned int size:29;unsigned int unusedBits:2;unsigned int allocated:1;struct _header *prev; } header;#define HEAP_INCR (1024 * 1024)static header *heap;
void mm_init() {
header *end;heap = (header *) sbrk(HEAP_INCR );heap->size = (HEAP_INCR-
(2*sizeof(header)))>>3;
heap->allocated = 0;heap->prev = NULL;end = heap + heap->size - 1;end->allocated = 0;end->size = 0;end->prev = heap; }
} else if (!curr->size) {
/* no room in the heap, grow heap */temp = (header ) sbrk(HEAP_INCR);if (!temp) return NULL;curr->size = (HEAP_INCR- sizeof(header))>>3;curr->allocated = 0;/ update new end */ } } }
void mm_free(void *ptr) {
header *curr, *prev, *next, *nn;curr = ((header ) ptr) - 1;prev = curr->prev;next = curr + curr->size + 1;curr->allocated = 0;if (!next->allocated) { / merge next into current */
curr->size += next->size + 1;nn = next + next->size + 1;nn->prev = curr;next = nn;
/* for double merge case */
} if (prev && !prev->allocated) { /* merge current into prev */
prev->size += curr->size + 1;next->prev = prev; } }
l
l