Skip to content
Snippets Groups Projects
Commit 56e46ac1 authored by 최민정's avatar 최민정
Browse files

finish

parent 81530590
No related branches found
No related tags found
No related merge requests found
#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);
}
}
#ifndef _ALLOC_H_
#define _ALLOC_H_
typedef struct meta_struct {
#include <stdint.h>
typedef struct meta_struct {
struct meta_struct* prev_memory;
struct meta_struct* next_memory;
uint32_t free;
uint32_t size;
char *comment;
char fit;
} meta;
char fit;
char *tmp_comment;
meta* head;
meta* first;
#endif
3 f
S THink like a man of action and act like man of thought.
s Courage is very important. LIke a muscle, it is strngthened by use.
s Life is the art if drqwing sufficient conclusions from insufficientpremises.
2 F
f 0
r 1
main 0 → 100755
File added
#include "alloc.h"
int main()
{
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
uint32_t size_index,size_data;
char type_fit, type_data;
char temp[256];
int num, index;
FILE *fp = fopen("input.txt","rb");
if(fp == NULL){
perror("Can't open the file");
exit(1);
}
else{
while(1){
if(EOF == fscanf(fp,"%d %c",size_index,type_fit))
break;
for(int i = 0; i < size_index; i++){
fscanf(fp,"%c",type_data);
if(type_data == "f"){
fscanf(fp,"%d",index);
m_free(index);
}
else if(type_data == "r"){
fscanf(fp,"%d %d",index,size_data);
m_realoc(index,size_data);
}
else if(type_data == "s"){
fscanf(fp,"%s",temp);
if(temp != NULL){
strcpy(tmp_comment,temp);
size_data = strlen(tmp_comment);
}
m_malloc(size_data);
}
}
read();
}
}
fclose(fp);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment