-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc_2.cpp
144 lines (121 loc) · 3.21 KB
/
malloc_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Created by Ruben on 27/06/2023.
//
#include <unistd.h>
#include <cstring>
#define MAX_SIZE 100000000
struct MallocMetadata {
size_t size;
bool is_free;
MallocMetadata* next;
MallocMetadata* prev;
};
size_t num_free_blocks=0;
size_t num_free_bytes=0;
size_t num_allocated_blocks=0;
size_t num_allocated_bytes=0;
size_t num_meta_data_bytes=0;
MallocMetadata* list = nullptr;
//Searches for a free block with at least ‘size’ bytes or allocates (sbrk()) one if none are
void* smalloc(size_t size){
if(size <= 0 || size>MAX_SIZE) {
return NULL;
}
size_t needed_size = size + sizeof(MallocMetadata);
MallocMetadata* curr= list;
//search for a free block
while(curr){
if(curr->is_free && curr->size >= size){
curr->is_free=false;
num_free_blocks--;
num_free_bytes-=curr->size;
return (char*)curr + sizeof(MallocMetadata);
}
curr=curr->next;
}
//if we got here, we need to allocate a new block
void* newptr = sbrk(needed_size);
if(newptr == (void*)-1){
return NULL;
}
MallocMetadata* pMetadata = (MallocMetadata*)newptr;
pMetadata->size=size;
pMetadata->is_free=false;
MallocMetadata* first=list;
if(first==NULL){
list = pMetadata;
}
else{
while(first->next!=NULL){
first=first->next;
}
first->next=pMetadata;
}
pMetadata->prev=first;
pMetadata->next=NULL;
num_allocated_blocks++;
num_allocated_bytes += pMetadata->size;
num_meta_data_bytes += sizeof(MallocMetadata);
return (char*)newptr + sizeof(MallocMetadata);
}
//Allocates a new memory block of size ‘size’ bytes.
void* scalloc(size_t num, size_t size){
void* new_ptr= smalloc(num*size);
if (new_ptr == nullptr){
return nullptr;
}
std::memset(new_ptr,0,num*size);
return new_ptr;
}
//Frees the memory space pointed to by ‘ptr’. If ‘ptr’ is NULL, no operation is performed.
void sfree(void* p){
if (p== nullptr){
return;
}
MallocMetadata* ptr = (MallocMetadata*)((char*) p - sizeof(MallocMetadata));
if (ptr->is_free){
return;
}
ptr->is_free=true;
num_free_blocks++;
num_free_bytes += ptr->size;
return;
}
//Changes the size of the memory block pointed to by ‘ptr’ to ‘size’ bytes and returns a pointer to the
void* srealloc(void* oldp, size_t size){
if (size==0||size>MAX_SIZE){
return nullptr;
}
if (oldp== nullptr){
return smalloc(size);
}
MallocMetadata* ptr = (MallocMetadata*)((char*) oldp - sizeof(MallocMetadata));
if (ptr->size>=size){
return oldp;
}
void* new_ptr = smalloc(size);
if (new_ptr== nullptr){
return nullptr;
}
sfree(oldp);
std::memmove(new_ptr,oldp,ptr->size);
return new_ptr;
}
size_t _num_free_blocks(){
return num_free_blocks;
}
size_t _num_free_bytes(){
return num_free_bytes;
}
size_t _num_allocated_blocks(){
return num_allocated_blocks;
}
size_t _num_allocated_bytes(){
return num_allocated_bytes;
}
size_t _num_meta_data_bytes(){
return num_meta_data_bytes;
}
size_t _size_meta_data(){
return sizeof (MallocMetadata);
}