Skip to content
Snippets Groups Projects
Commit 36358c94 authored by 김기훈's avatar 김기훈
Browse files

last submit_assignment4

parent 8be41b8b
No related branches found
No related tags found
No related merge requests found
#include "alloc.h" #include "alloc.h"
#include <unistd.h>
#include <memory.h>
#include <stdio.h>
void *m_malloc(size_t size);
void m_free(void *ptr);
void *m_realloc(void *ptr, size_t size);
meta *meta1 = NULL;
meta *meta2 = NULL;
int mode = -1;
void select_mode(char cr){
if(cr == 'F'){
mode = 1;
}
else if(cr == 'B'){
mode = 2;
}
else if(cr == 'W'){
mode = 3;
}
}
meta *find_block(size_t size2){
meta *temp = meta1;
meta *block = NULL;
while(temp != 0){
if(temp->free != 0 && temp->size >= size2){
if(mode == 1){
return temp;
}
else if(mode == 2){
if(block == 0 || (block->size - size2 > temp->size - size2)){
block = temp;
}
}
else if(mode == 3){
if(block == 0 || block->size < temp->size){
block = temp;
}
}
}
temp = temp->next;
}
return block;
}
void *m_malloc(size_t size){
if(mode ==-1||size <=0){
return NULL;
}
if(meta1 == NULL){
meta1 = sbrk(size + sizeof(meta));
meta1->prev = NULL;
meta1->next = NULL;
meta1->free = 0;
meta1->size = size;
meta2 = meta1;
return meta1 + sizeof(meta);
}
meta *block = find_block(size);
if(block == 0){
block = sbrk(size + sizeof(meta));
block->prev = meta2;
block->next = NULL;
block->free = 0;
block->size = size;
meta2->next = block;
meta2 = block;
return block + sizeof(meta);
}
block->free = 0;
if(block->size - size > sizeof(meta)){
block->size = size;
meta *block2 = block + sizeof(meta) + size;
block2->prev = block;
block2->next = block->next;
block2->free = 1;
block2->size = block->size - (size + sizeof(meta));
block->next = block2;
}
return block + sizeof(meta);
}
void m_free(void *ptr){
meta *temp = (meta*)ptr - sizeof(meta);
temp->free = 1;
if(temp->prev !=0 && temp->prev->free !=0){
temp->next->prev = temp->prev;
temp->prev->next = temp->next;
temp->prev->size += temp->size;
temp = temp->prev;
}
if(temp->next !=0 && temp->next->free !=0){
temp->size += temp->next->size;
if(temp->next == meta2){
meta2 = temp;
}
temp->next = temp->next->next;
if(temp->next->next != 0){
temp->next->next->prev = temp;
}
}
while(meta2 !=0 && meta2->free !=0){
meta2 = meta2->prev;
if(meta2 !=0){
meta2->next = NULL;
}
else{
meta1 = NULL;
}
brk(meta2);
}
}
void *m_realloc(void *ptr, size_t size){
meta *temp = (meta*)ptr - sizeof(meta);
if(temp->size >= size){
return ptr;
}
void *new_ptr = m_malloc(size);
memcpy(new_ptr, ptr, temp->size);
m_free(ptr);
return new_ptr;
}
#ifndef _ALLOC_H_ #ifndef _ALLOC_H_
#define _ALLOC_H_ #define _ALLOC_H_
#include <unistd.h>
typedef struct meta_struct { typedef struct meta_struct {
struct meta_struct *prev;
struct meta_struct *next;
int free;
size_t size;
}meta; }meta;
meta *meta1;
meta *meta2;
#endif #endif
No preview for this file type
#include "alloc.h" #include "alloc.h"
#include <string.h>
#include <stdio.h>
#include <memory.h>
void *arr1[512];
int temp;
int main() int main()
{ {
int num1;
int cr;
scanf("%d %c", &num1, &cr);
select_mode(cr);
while(num1 != -1){
char class;
scanf("%c", &class);
if(class == 's'){
char arr2[512] = {};
int num2 = 0;
while(1){
char cr2 = getchar();
if(cr2 == '\n'){
break;
}
arr2[num2] = cr2;
num2++;
}
arr1[temp] = m_malloc(num2);
memcpy(arr1[temp], arr2,strlen(arr2));
temp++;
}
else if(class == 'f'){
int num2;
int num3;
scanf("%d %d", &num2, &num3);
arr1[num2] = m_realloc(arr1[num2], num3);
}
else if(class == 'r'){
int num3;
scanf("%d" , &num3);
arr1[temp] = m_malloc(num3);
temp++;
}
num1--;
}
meta *temp2 = meta1;
while(temp2 !=0){
printf("%d %d" , temp2->free, temp2->size);
if(temp2->free == 0){
printf("%s\n", (char*)(temp2 + sizeof(meta)));
}
temp2 = temp2->next;
}
return 0; return 0;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment