Skip to content

Commit bb20e02

Browse files
authored
Merge pull request #15 from SidoShiro/leader-feature-3
Leader feature 3
2 parents 8eb98b9 + f3f73a2 commit bb20e02

9 files changed

Lines changed: 234 additions & 37 deletions

File tree

include/block.h

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
11
#ifndef DISTRIBUTEDMALLOC_BLOCK_H
22
#define DISTRIBUTEDMALLOC_BLOCK_H
33

4-
#include "stdlib.h"
4+
#include <stdlib.h>
5+
#include <stdio.h>
56

7+
/**
8+
* size of block
9+
* address on id node
10+
* address virtual (user view)
11+
* next block
12+
* free : 0 is free, 1 is allocated
13+
*/
614
struct block {
715
unsigned short id;
816
size_t size;
9-
size_t address;
17+
size_t node_address;
18+
size_t virtual_address;
1019
struct block *next;
20+
char free;
1121
};
1222

13-
struct blocks {
23+
struct block_register {
1424
size_t nb_blocks;
1525
struct block **blks;
1626
};
1727

18-
struct blocks *generate_blocks(size_t nb_blks);
28+
struct allocation_register {
29+
size_t size_alloc;
30+
size_t count_alloc;
31+
struct allocation **allocs;
32+
};
33+
34+
struct allocation {
35+
size_t number_parts;
36+
size_t v_address_start;
37+
struct block **parts;
38+
};
39+
40+
struct allocation_register *generate_allocs(size_t size_alloc);
41+
42+
void add_allocation(struct allocation_register *a_r, struct allocation *a);
43+
44+
struct block_register *init_nodes_same_size(unsigned short nb_nodes, size_t size);
45+
46+
struct block_register *generate_blocks(size_t nb_blks);
47+
48+
struct block *generate_block(unsigned short id, size_t size, size_t node_address, size_t virtual_address, char free);
49+
50+
void
51+
add_block(struct block *blk, unsigned short id, size_t size, size_t node_address, size_t virtual_address, char free);
52+
53+
void merge_free_block(struct block_register *blks);
1954

20-
struct block *generate_block(unsigned short id, size_t size, size_t address);
55+
struct block_register *init_nodes_same_size(unsigned short nb_nodes, size_t size);
2156

22-
void add_block(struct block blk, unsigned short id, size_t size, size_t address);
57+
/**
58+
* Splited half 2 is free = 0
59+
* @param b
60+
* @param size
61+
* @return
62+
*/
63+
struct block *split_block_u(struct block *b, size_t size);
2364

2465
#endif /* !DISTRIBUTEDMALLOC_BLOCK_H */

include/leader.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@
99
#include "block.h"
1010

1111
struct leader_resources {
12-
struct blocks *leader_blks;
12+
struct block_register *leader_blks;
13+
struct allocation_register *leader_reg;
1314
struct command_queue *leader_command_queue;
15+
// unsigned short id;
1416
};
1517

1618
struct address_search {
1719
unsigned short id;
18-
size_t address_r;
19-
size_t address;
20+
size_t r_address;
21+
size_t v_address;
22+
size_t n_address;
2023
size_t size;
2124
};
2225

23-
void leader_loop(struct node *n, unsigned short terminal_id);
26+
void leader_loop(struct node *n, unsigned short terminal_id, unsigned short nb_nodes);
2427

2528
#endif /* !DISTRIBUTEDMALLOC_LEADER_H */

run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
rm -rf build/*
12
cd build
23
cmake ..
34
make -j 6

src/cli/cli.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ void execute(char **args, unsigned short leader) {
106106
printf("Execute DMALLOC of %zu\n", size);
107107
struct data_size *d_s = generate_data_size(size);
108108
send_command(OP_MALLOC, d_s, leader);
109+
free(d_s);
109110
} else {
110111
error_msg("m require an argument 'size' which can be casted as a positive integer");
111112
}
@@ -144,6 +145,7 @@ void execute(char **args, unsigned short leader) {
144145
printf("Execute Write at %zu of %s : %zu bytes\n", address, args[3], datasize);
145146
struct data_write *d_w = generate_data_write(address, datasize, args[3]);
146147
send_command(OP_WRITE, d_w, leader);
148+
free(d_w);
147149
} else {
148150
error_msg("w requires an argument 'datasize' which can be casted as a positive integer");
149151
}
@@ -168,6 +170,7 @@ void execute(char **args, unsigned short leader) {
168170
printf("Execute Read at %zu, %zu bytes\n", address, datasize);
169171
struct data_read *d_r = generate_data_read(address, datasize);
170172
send_command(OP_READ, d_r, leader);
173+
free(d_r);
171174
} else {
172175
error_msg("r requires an argument 'datasize' which can be casted as a positive integer");
173176
}

src/cli/user.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ void send_write(void *data, unsigned short leader) {
1818
debug("Send Data For Write OP", 0);
1919
MPI_Isend(d_w->data, d_w->size, MPI_BYTE, m->id_t, 0, MPI_COMM_WORLD, &r);
2020
free(m);
21-
free(data);
2221
}
2322

2423
void send_malloc(void *data, unsigned short leader) {
@@ -35,15 +34,23 @@ void send_malloc(void *data, unsigned short leader) {
3534
MPI_Request r2;
3635
void *buff = generate_message(0, 0, 0, 0, 0, OP_NONE);
3736
MPI_Irecv(buff, sizeof(struct message), MPI_BYTE, leader, 0, MPI_COMM_WORLD, &r2);
37+
/*
3838
while (0 != MPI_Wait(&r, &st)) {
3939
char *msg = "Address from malloc operation of size ";
4040
char size_str[256];
4141
snprintf(size_str, sizeof(size_str), "%zu", d_s->size);
4242
strcat(msg, size_str);
4343
debug(msg, DEF_NODE_USER);
4444
}
45+
*/
46+
if (0 == MPI_Wait(&r2, &st)) {
47+
struct message *m2 = buff;
48+
printf("user: request malloc of size %zu, is at address %zu on the network\n",
49+
m2->size,
50+
m2->address);
51+
}
4552
free(m);
46-
free(data);
53+
free(buff);
4754
}
4855

4956
void send_read(void *data, unsigned short leader) {
@@ -68,7 +75,6 @@ void send_read(void *data, unsigned short leader) {
6875
debug(msg, DEF_NODE_USER);
6976
}
7077
free(m);
71-
free(data);
7278
}
7379

7480
void send_command(enum operation op, void *data, unsigned short leader) {

src/main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int main(int argc, char **argv) {
5353
// Start Leader !
5454
if (n->id == leader) {
5555
n->isleader = 1;
56-
leader_loop(n, DEF_NODE_USER);
56+
leader_loop(n, DEF_NODE_USER, size - 1);
5757
}
5858

5959
/*

src/network/block.c

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,78 @@
11
#include "block.h"
22

33

4-
struct blocks *generate_blocks(size_t nb_blks) {
5-
struct blocks *blks = malloc(sizeof(struct blocks));
6-
blks->blks = malloc(nb_blks * sizeof(struct block*));
4+
struct block_register *generate_blocks(size_t nb_blks) {
5+
struct block_register *blks = malloc(sizeof(struct block_register));
6+
blks->blks = malloc(nb_blks * sizeof(struct block *));
77
blks->nb_blocks = nb_blks;
88
return blks;
99
}
1010

11-
struct block *generate_block(unsigned short id, size_t size, size_t address) {
11+
struct block *generate_block(unsigned short id, size_t size, size_t node_address,
12+
size_t virtual_address, char free) {
1213
struct block *blk = malloc(sizeof(struct block));
1314
blk->id = id;
1415
blk->size = size;
15-
blk->address = address;
16+
blk->node_address = node_address;
17+
blk->virtual_address = virtual_address;
18+
blk->free = free;
1619
blk->next = NULL;
1720
return blk;
1821
}
1922

20-
void add_block(struct block blk, unsigned short id, size_t size, size_t address) {
21-
struct block *n_blk = generate_block(id, size, address);
22-
blk.next = n_blk;
23+
void add_block(struct block *blk, unsigned short id, size_t size, size_t node_address,
24+
size_t virtual_address, char free) {
25+
struct block *n_blk = generate_block(id, size, node_address, virtual_address, free);
26+
blk->next = n_blk;
2327
(void) blk;
2428
}
29+
30+
void merge_free_block(struct block_register *blks);
31+
32+
struct block_register *init_nodes_same_size(unsigned short nb_nodes, size_t size) {
33+
struct block_register *blks = generate_blocks(nb_nodes);
34+
size_t v_address = 0;
35+
for (size_t i = 0; i < blks->nb_blocks; i++) {
36+
// Skip 0 (user), so from 1 to nb_nodes + 1
37+
blks->blks[i] = generate_block(i + 1, size, 0, v_address, 0);
38+
v_address += size;
39+
}
40+
printf("Global init has memory size of %zu bytes", v_address);
41+
return blks;
42+
}
43+
44+
struct block *split_block_u(struct block *b, size_t size) {
45+
if (!b) {
46+
printf("FATAL ALLOCATION ERROR\n");
47+
}
48+
if (b->size < size) {
49+
b->size = size;
50+
size_t su = size - b->size;
51+
struct block *b_next = b->next;
52+
add_block(b, b->id, su, b->node_address + size, b->virtual_address + size, 0);
53+
b->next->next = b_next;
54+
return b;
55+
}
56+
return NULL;
57+
}
58+
59+
struct allocation_register *generate_allocs(size_t size_alloc) {
60+
struct allocation_register *a = malloc(sizeof(struct allocation_register));
61+
a->size_alloc = size_alloc;
62+
a->count_alloc = 0;
63+
a->allocs = malloc(sizeof(struct allocation) * size_alloc);
64+
return a;
65+
}
66+
67+
void add_allocation(struct allocation_register *a_r, struct allocation *a) {
68+
if (a_r->count_alloc >= a_r->size_alloc) {
69+
a_r->allocs[a_r->count_alloc] = a;
70+
} else {
71+
// x2 size
72+
a_r->size_alloc = a_r->size_alloc * 2;
73+
a_r->allocs = realloc(a_r->allocs, a_r->size_alloc * sizeof(struct allocation));
74+
75+
}
76+
a_r->allocs[a_r->count_alloc] = a;
77+
a_r->count_alloc++;
78+
}

0 commit comments

Comments
 (0)