Skip to content
Snippets Groups Projects
Select Git revision
  • d72a19f8f327c5fb8f8be2cf5b756cff58e1d761
  • master default
2 results

alloc.c

Blame
  • Forked from HyukSang Kwon / 1801_OS_assignment4
    Source project has a limited visibility.
    alloc.c 3.26 KiB
    #include "alloc.h"
    
    #include <stdio.h>
    #include <stdint.h>
    #include <string.h>
    
    void *m_malloc(uint32_t size){
      meta* temp;
      if(size%4 != 0)
        size += (4-size%4);
    
      if(head == NULL){
        temp = sbrk(size);
        temp -> next_memory = NULL;
        temp -> prev_memory = NULL;
        temp -> size = size;
        temp -> free = 0;
        strcpy(temp -> comment,tmp_comment);
        temp -> fit= fit;
    
        head = temp;
        first = temp;
      }
      else{
        meta* prev = NULL;
        meta* next = NULL;
        meta* tmp_head = head;
          while(1){
    	if(tmp_head->next_memory == NULL){
    	  if(prev == NULL){
    	    prev = tmp_head;
    	  }
    	  break;
    	}
    	else if(tmp_head -> next_memory -> free == 1 && tmp_head -> next_memory -> size > size){
              if( prev == NULL){
    	    prev = tmp_head;
    	    if(fit == "F")
    	      break;
    	  }
    	  else{
    	    if(fit == "B")
    	      prev = (prev -> size < tmp_head -> next_memory -> size) ? prev : tmp_head;
    	    else if(fit == "W")
    	      prev = (prev -> size > tmp_head -> next_memory -> size) ? prev : tmp_head;
    	  }
    	}
    	tmp_head = tmp_head-> next_memory;
          }
        tmp_head = prev -> next_memory;
        next = tmp_head -> next_memory;
        if(tmp_head -> size == size){
          prev -> next_memory -> free = 0;
        }
        else{
          temp -> prev_memory = prev;
          temp -> next_memory = tmp_head;
          temp -> size = size;
          temp -> free = 0;
          strcpy(temp -> comment,tmp_comment);
          temp -> fit = fit;
    
          tmp_head -> size -= size; 
          tmp_head -> prev_memory = temp;
    
          prev -> next_memory = temp;
        }
      }
    }
    
    void m_realoc(void* p,uint32_t size){
      if(size%4 != 0)
        size += (4-size%4);
    
      int index = *(int *)p;
      int count = 0;
      meta* temp = head;
    
      while(1){
        if(count == index)
          break;
        
        temp = temp -> next_memory;
        if(temp -> free == 0)
          count++;
      }
      if(temp -> next_memory == NULL){
        temp -> size = size;
      }
      else if(size < (temp -> next_memory -> size) + temp -> size){
    	temp -> next_memory -> size -= (size - temp->size);
    	temp -> size = size;
      }
      else if(size = (temp -> next_memory -> size) + temp -> size){
      	temp -> size = size;
    	temp -> next_memory = temp -> next_memory -> next_memory;
      }
      else
        m_malloc(size);
    }
    
    void m_free(void *ptr){
      int index = *(int *)ptr;
      int count = 0;
      meta* tmp_head;
      meta* temp = head;
      int size = head -> size;
      int break_point = 0;
       
        if(temp -> next_memory != NULL){
          if(temp -> next_memory -> free == 1){
            size += temp -> next_memory -> size;
            tmp_head -> next_memory = temp -> next_memory -> next_memory;
    	temp -> next_memory -> next_memory -> prev_memory = tmp_head;
          }
        }
        else
          tmp_head -> next_memory = NULL;
          
        if(temp -> prev_memory != NULL){
          if(temp -> prev_memory -> free == 1){
            size += temp -> prev_memory -> size;
            tmp_head -> prev_memory = temp -> prev_memory -> prev_memory;
    	temp -> prev_memory -> prev_memory -> next_memory = tmp_head;
          }
        }
        else
          tmp_head -> prev_memory = NULL;
        
      tmp_head -> free = 1;
      tmp_head -> size = size;
      strcpy(temp -> comment," ");
      if( index == 0)
        head == tmp_head -> next_memory;
    }
    
    
    void read(){
      meta* temp = first;
      
      while(1){
        if(temp -> next_memory == NULL)
          break;
    
        printf("%d %d %s\n", temp -> free, temp -> size, temp -> comment);
      }
    }