-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory-alloc.c
More file actions
80 lines (61 loc) · 1.44 KB
/
memory-alloc.c
File metadata and controls
80 lines (61 loc) · 1.44 KB
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
#include <stdio.h>
#include <pthread.h>
#include <unistd.h> // For sbrk
typedef char ALIGN[16];
struct head_t {
size_t size;
unsigned is_free;
struct head_t *next;
};
typedef struct head_t head_t;
head_t *head, *tail;
pthread_mutex_t global_malloc_lock;
head_t *get_free_block(size_t size) {
head_t *curr = head;
while (curr) {
if (curr->is_free && curr->size >= size) {
return curr;
}
curr = curr->next;
}
return NULL;
}
void *malloc(size_t size) {
size_t total_size;
void *block;
head_t *header;
if (!size) {
return NULL;
}
pthread_mutex_lock(&global_malloc_lock);
header = get_free_block(size);
if (header) {
header->is_free = 0;
pthread_mutex_unlock(&global_malloc_lock);
return (void *)(header + 1);
}
total_size = sizeof(head_t) + size;
block = sbrk(total_size);
if (block == (void *) -1) {
pthread_mutex_unlock(&global_malloc_lock);
return NULL;
}
header = (head_t *)block;
header->size = size;
header->is_free = 0;
header->next = NULL;
if (!head) {
head = header;
}
if (tail) {
tail->next = header;
}
pthread_mutex_unlock(&global_malloc_lock);
return (void *)(header + 1);
}
int main() {
void *ptr1 = malloc(100);
void *ptr2 = malloc(50);
printf("Allocated memory at %p and %p\n", ptr1, ptr2);
return 0;
}