Skip to content

Commit 21e222d

Browse files
authored
Merge pull request #18 from SidoShiro/malloc-multipart
Malloc multipart
2 parents d7824b6 + 0a33365 commit 21e222d

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

src/network/leader.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
#include "debug.h"
66
#include "utils.h"
77

8+
#include <sys/types.h>
9+
#include <unistd.h>
810
#include <mpi.h>
11+
#include <stdint.h>
912

1013
size_t get_message_size() {
1114
return (sizeof(unsigned short) * 3 + 2 * sizeof(size_t) + sizeof(enum operation));
@@ -75,10 +78,10 @@ size_t alloc_memory(size_t size, struct leader_resources *l_r) {
7578
if (0 == b->free) {
7679
if (size == b->size) {
7780
b->free = 1;
78-
struct allocation *a = malloc(sizeof(struct allocation));
81+
struct allocation *a = malloc(32 + sizeof(struct allocation));
7982
a->number_parts = 1;
8083
a->v_address_start = b->virtual_address;
81-
a->parts = malloc(a->number_parts * sizeof(struct allocation *));
84+
a->parts = malloc(34 + (a->number_parts * sizeof(struct allocation *)));
8285
a->parts[0] = b;
8386
add_allocation(l_r->leader_reg, a);
8487
return b->virtual_address;
@@ -102,10 +105,34 @@ size_t alloc_memory(size_t size, struct leader_resources *l_r) {
102105
}
103106
}
104107
// Multi - Parts
108+
struct allocation *a = malloc(32 + sizeof(struct allocation));
109+
a->number_parts = 0;
110+
a->parts = NULL;
111+
a->v_address_start = SIZE_MAX;
112+
ssize_t m_size = size;
105113
for (size_t i = 0; i < blks->nb_blocks; i++) {
106-
114+
struct block *b = blks->blks[i];
115+
while (b && m_size > 0) {
116+
if (b->free == 0) {
117+
b->free = 1;
118+
m_size -= b->size;
119+
if (m_size < 0) {
120+
b = split_block_u(b, -1 * m_size);
121+
}
122+
if (a->v_address_start == SIZE_MAX)
123+
a->v_address_start = b->virtual_address;
124+
a->number_parts++;
125+
a->parts = realloc(a->parts, 34 + (a->number_parts * sizeof(struct allocation *)));
126+
a->parts[a->number_parts - 1] = b;
127+
}
128+
b = b->next;
129+
}
130+
if (m_size <= 0) {
131+
add_allocation(l_r->leader_reg, a);
132+
return a->v_address_start;
133+
}
107134
}
108-
return 999999999;
135+
return SIZE_MAX;
109136
}
110137

111138
struct address_search *search_at_address(size_t address, struct leader_resources *l_r) {

0 commit comments

Comments
 (0)