|
@@ -30,10 +30,10 @@ void *first_fit_search(size_t size, NODE *starting_node) {
|
|
|
void *custom_malloc(heap_t* heap, size_t size);
|
|
|
|
|
|
void *custom_malloc(heap_t* heap, size_t size) {
|
|
|
- NODE *ptr;
|
|
|
+ leaHdr *ptr;
|
|
|
int fixed_list_id, i, found;
|
|
|
MAPTABLE_NODE *current_maptable_node;
|
|
|
- NODE *current_node;
|
|
|
+ leaHdr *current_node;
|
|
|
|
|
|
ptr = NULL;
|
|
|
|
|
@@ -51,9 +51,9 @@ void *custom_malloc(heap_t* heap, size_t size) {
|
|
|
}
|
|
|
}
|
|
|
ptr = current_maptable_node->fixed_list_head;
|
|
|
- current_maptable_node->fixed_list_head = ((NODE *) ptr)->next;
|
|
|
- set_requested_size(ptr->data, size);
|
|
|
- markInUse(ptr->data);
|
|
|
+ current_maptable_node->fixed_list_head = ptr->next;
|
|
|
+ set_requested_size(ptr, size);
|
|
|
+ markInUse(ptr);
|
|
|
}
|
|
|
|
|
|
if(ptr == NULL) {
|
|
@@ -62,18 +62,18 @@ void *custom_malloc(heap_t* heap, size_t size) {
|
|
|
// first fit from free list
|
|
|
current_node = heap->free_list_head;
|
|
|
|
|
|
- if(current_node != NULL && getSize(current_node->data) >= size) {
|
|
|
+ if(current_node != NULL && getSize(current_node) >= size) {
|
|
|
ptr = current_node;
|
|
|
- heap->free_list_head = ((NODE *) ptr)->next;
|
|
|
- set_requested_size(ptr->data, size);
|
|
|
- markInUse(ptr->data);
|
|
|
+ heap->free_list_head = ptr->next;
|
|
|
+ set_requested_size(ptr, size);
|
|
|
+ markInUse(ptr);
|
|
|
}
|
|
|
while(current_node && !found) {
|
|
|
- if(getSize(current_node->next->data) >= size) {
|
|
|
+ if(getSize(current_node->next) >= size) {
|
|
|
ptr = current_node->next;
|
|
|
current_node->next = ptr->next;
|
|
|
- set_requested_size(ptr->data, size);
|
|
|
- markInUse(ptr->data);
|
|
|
+ set_requested_size(ptr, size);
|
|
|
+ markInUse(ptr);
|
|
|
found = 1;
|
|
|
}
|
|
|
current_node = current_node->next;
|
|
@@ -87,9 +87,46 @@ void *custom_malloc(heap_t* heap, size_t size) {
|
|
|
|
|
|
posix_unlock(heap);
|
|
|
|
|
|
- return ptr->data;
|
|
|
+ return ptr;
|
|
|
}
|
|
|
|
|
|
+void custom_free(heap_t* heap, void *ptr);
|
|
|
+
|
|
|
+void custom_free(heap_t* heap, void *ptr) {
|
|
|
+ size_t size;
|
|
|
+ int fixed_list_id, i;
|
|
|
+ MAPTABLE_NODE *current_maptable_node;
|
|
|
+ leaHdr *test;
|
|
|
+
|
|
|
+
|
|
|
+ size = getSize((char *) ptr);
|
|
|
+ fixed_list_id = map_size_to_list(heap, size);
|
|
|
+
|
|
|
+ posix_lock(heap);
|
|
|
+
|
|
|
+ if(fixed_list_id != -1) {
|
|
|
+ current_maptable_node = heap->maptable_head;
|
|
|
+ if(fixed_list_id == 0) {
|
|
|
+ test = getHeader(ptr);
|
|
|
+ test->next = current_maptable_node->fixed_list_head;
|
|
|
+ //getHeader((char *) ptr)->next = current_maptable_node->fixed_list_head;
|
|
|
+ current_maptable_node->fixed_list_head = ptr;
|
|
|
+ } else {
|
|
|
+ for(i = 1; i < fixed_list_id; i++) {
|
|
|
+ current_maptable_node = current_maptable_node->next;
|
|
|
+ }
|
|
|
+ getHeader(ptr)->next = current_maptable_node->fixed_list_head;
|
|
|
+ current_maptable_node->fixed_list_head = ptr;
|
|
|
+ }
|
|
|
+ } else { // put it in the free list
|
|
|
+ getHeader(ptr)->next = heap->free_list_head;
|
|
|
+ heap->free_list_head = getHeader(ptr);
|
|
|
+ }
|
|
|
+
|
|
|
+ markFree((char *) ptr);
|
|
|
+
|
|
|
+ posix_unlock(heap);
|
|
|
+}
|
|
|
|
|
|
int main(void) {
|
|
|
allocator_t *myallocator;
|
|
@@ -102,4 +139,5 @@ int main(void) {
|
|
|
printf("This thread accesses heap %d\n", heap_id);
|
|
|
myheap = &myallocator->heaps[heap_id];
|
|
|
p = custom_malloc(myheap, 32);
|
|
|
+ custom_free(myheap, p);
|
|
|
}
|