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

alloc.c

Blame
  • Forked from HyukSang Kwon / 1801_OS_assignment4
    1 commit ahead of the upstream repository.
    alloc.c 3.21 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* temp = head;
      int size = head -> size;
      int break_point = 0;
       
      while(1){
        if(count == index)
          break;
    		 
      temp = temp -> next_memory;
      if(temp -> free == 0)
           count++;
      }
      while(1){
        if(temp -> next_memory == NULL){
          if(temp -> next_memory -> free == 1){
            size += temp -> next_memory -> size;
            temp -> next_memory = temp -> next_memory -> next_memory;
          }
          else
            break;
        }
        else
          break;
      }
      while(1){
        if(temp -> prev_memory == NULL){
          if(temp -> prev_memory -> free == 1){
            size += temp -> prev_memory -> size;
            temp -> prev_memory = temp -> prev_memory -> prev_memory;
          }
        else
          break;
        }
        else
          break;
      }
      temp -> free = 1;
      strcpy(temp -> comment," ");
      if( index == 0)
        head == temp -> 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);
      }
    }