123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- #include <stdio.h>
- #include "heap.h"
- #include "other.h"
- #include "posix_lock.h"
- #include "LeaHeader.h"
- #include "dmm_init.h"
- /*
- void *first_fit_search(size_t size, NODE *starting_node);
- void *first_fit_search(size_t size, NODE *starting_node) {
- NODE *node;
- int found;
- void *ptr;
- node = starting_node;
- found = 0;
- if(node != NULL &&
- while(node && !found) {
- if(getSize(node->data) >= size) {
- ptr = node
- }
- }
- }
- */
- void *custom_malloc(heap_t* heap, size_t size);
- void *custom_malloc(heap_t* heap, size_t size) {
- leaHdr *ptr;
- int fixed_list_id, i, found;
- MAPTABLE_NODE *current_maptable_node;
- leaHdr *current_node;
- ptr = NULL;
- posix_lock(heap);
- fixed_list_id = map_size_to_list(heap, size);
-
- // If a fixed list is found, do a first fit
- if(fixed_list_id != -1) {
- current_maptable_node = heap->maptable_head;
- // traverse through the maptable node list
- if(fixed_list_id != 0) {
- for(i = 1; i < fixed_list_id; i++) {
- current_maptable_node = current_maptable_node->next;
- }
- }
- ptr = current_maptable_node->fixed_list_head;
- current_maptable_node->fixed_list_head = ptr->next;
- set_requested_size(ptr, size);
- markInUse(ptr);
- }
- if(ptr == NULL) {
- found = 0;
- // first fit from free list
- current_node = heap->free_list_head;
- if(current_node != NULL && getSize(current_node) >= size) {
- ptr = current_node;
- heap->free_list_head = ptr->next;
- set_requested_size(ptr, size);
- markInUse(ptr);
- }
- while(current_node && !found) {
- if(getSize(current_node->next) >= size) {
- ptr = current_node->next;
- current_node->next = ptr->next;
- set_requested_size(ptr, size);
- markInUse(ptr);
- found = 1;
- }
- current_node = current_node->next;
- }
- if(!found) {
- return sys_alloc(size);
- }
- }
- posix_unlock(heap);
- 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;
- heap_t *myheap;
- int heap_id;
- void *p;
- myallocator = dmm_init();
- heap_id = map_thread_heap();
- printf("This thread accesses heap %d\n", heap_id);
- myheap = &myallocator->heaps[heap_id];
- p = custom_malloc(myheap, 32);
- custom_free(myheap, p);
- }
|