Skip to content
Snippets Groups Projects
Commit 7914f92f authored by HongJiHo's avatar HongJiHo
Browse files

submission

parent 22f2a890
No related branches found
No related tags found
No related merge requests found
main: main.c alloc.c
gcc -w -g O2 $^ -o $@
.PHONY: clean
clean:
rm -rf *~ main
#include "alloc.h"
void *m_malloc(size_t size){
meta* ptr = head;
meta* chunk;
//align the requested size in 4bytes units
size = align_size(size);
//no memory
if(head == NULL){
meta* tmp;
tmp = sbrk(sizeof(meta) + size);
tmp->next = NULL;
tmp->prev = NULL;
tmp->size = size;
tmp->free = 0; //chunk used
head = tmp;
tail = tmp;
return (tmp + 1);
}
//heap empty
else if(fit == 0){ //first fit
while(ptr-> free == 0){
if(ptr == NULL)
break;
if(ptr->size > (sizeof(meta) + size))
break;
ptr = ptr->next;
}
chunk = ptr;
}
else if(fit == 1){ //Best fit
while(ptr){
if(ptr->size > size*sizeof(meta) && ptr->free == 1){ //if there is fit chunk
if(chunk == NULL)
chunk = ptr;
else if(chunk->size > ptr->size) //we want smallest chunk size
chunk = ptr;
}
ptr = ptr->next;
}
}
else if(fit == 2){ //Worst fit
while(ptr){
if(ptr->size > size*sizeof(meta) && ptr->free == 1){
if(chunk == NULL)
chunk = ptr;
else if(chunk->size < ptr->size) //we want biggest chunk size
chunk = ptr;
}
ptr = ptr->next;
}
}
else
return -1;
//memory allocation
if(chunk == NULL){ //new memory space
meta* tmp;
tmp = sbrk(sizeof(meta) + size);
tmp->next = NULL;
tmp->prev = tail;
tmp->size = size;
//change tail
tail->next = tmp;
tail = tmp;
}
else{
meta* tmp;
tmp = (meta*)(chunk->next - (sizeof(meta) + size));
tmp->prev = chunk->prev;
tmp->prev->next = tmp;
tmp->next = chunk->next;
if(chunk->next)
chunk->next->prev = tmp;
tmp->size = size;
}
return (tmp + sizeof(meta));
}
void m_free(void *ptr){
meta* ptr = head;
if(ptr == NULL) //if memory space free
return;
}
void* m_realloc(void* ptr, size_t size){
}
size_t align_size(size_t size){
uint32_t tmp = 0;
tmp = size % 4;
return (size + (4-tmp));
}
#ifndef _ALLOC_H_ #ifndef _ALLOC_H_
#define _ALLOC_H_ #define _ALLOC_H_
typedef struct meta_struct { #include<stdint.h>
static meta* head = NULL;
static meta* tail = NULL;
int fit = 0; //0:first fit, 1:Best fit, 2:Worst fit
typedef struct meta_struct {
struct meta_struct* prev;
struct meta_struct* next;
uint32_t free; //if 0: chunk used, 1: chunk is free
uint32_t size;
} meta; } meta;
//return : pointer beginning of the alloceted space
void *m_malloc(size_t size);
void m_free(void *ptr);
void* m_realloc(void* ptr, size_t size);
#endif #endif
#include "alloc.h"
int main(int argc, char* argv[])
{
char buf[1024];
char string[1024];
char *ptr;
int commandNum;
FILE *fd = fopen(argv[1], "r");
fgets(buf, 1024, fd);
buf[strle(buf) - 1] = "\0";
ptr = strtok(buf, " ");
commandNum = atoi(ptr);
//get fit type
ptr = strtok(NULL, " ");
if(strcmp(ptr, "F") == 0) //First fit
fit = 0;
else if(strcmp(ptr, "B") == 0) //Best fit
fit = 1;
else if(strcmp(ptr, "w") == 0) //Worst fit
fit = 2;
else{
printf("invalid fit type");
return -1;
}
//read command
for(int i = 0; i < commandNum; i++){
fgets(buf, 1024, fd);
ptr = strtok(buf, " ");
if(strcmp(ptr, "s") == 0){ //string command
ptr = strtok(NULL, "\n");
strcpy(string, ptr);
}
else if(strcmp(ptr, "f") == 0){ //nth area free
}
else if(strcmp(ptr, "r") == 0){ //n, m : realloc m byte in nth area
}
else if(strcmp(ptr, "e") == 0){ //alloc n byte without space
}
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment