-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc.c
More file actions
85 lines (70 loc) · 1.69 KB
/
malloc.c
File metadata and controls
85 lines (70 loc) · 1.69 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
81
82
83
84
85
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "utils.h"
#include "malloc.h"
#include "init.h"
void *malloc(size_t size)
{
t_block *block;
t_block *next;
size_t required_size;
size_t bin_index;
// Add the size of our struct to the required size
// to get the actual size required in memory
required_size = size + sizeof(t_block);
fprintf(stderr, "NOTRE PRTINTF!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
// Return NULL if the user requested more than the page size
if (required_size > g_page_size)
{
return (NULL);
}
// If no memory was allocated yet, allocate our first page.
// If it fails, return NULL
if (allocated_mem == NULL)
{
allocated_mem = init_mem();
if (allocated_mem == NULL)
{
return (NULL);
}
}
// Determine which bin this block will be stored in
bin_index = get_bin_index(required_size);
// TODO: move to allocate.c ?
// Look for an available block in the bin
if (bins_array[bin_index] != NULL)
{
// Remove the block from the bin and return it
block = bins_array[bin_index];
block->is_free = 0;
next = block->next;
block->next = NULL;
block->prev = NULL;
// Update the bin
bins_array[bin_index] = next;
if (next != NULL)
{
next->prev = NULL;
}
// Return a pointer offset by the size of our block data
return (block + 1);
}
// No available block was found
return (NULL);
}
unsigned int get_bin_index(size_t size)
{
unsigned int result;
unsigned int max;
max = pow(2, g_bins_count - 1);
result = (unsigned int) pow(2, ceil(log(size)/log(2)));
if (result > max)
{
return (max);
}
else
{
return (result);
}
}