Skip to content

Commit d2bb5bc

Browse files
authored
Merge pull request #35 from SidoShiro/allocation-table-size
Allocation table size, good
2 parents 4ff125e + ca57392 commit d2bb5bc

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

include/leader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ void leader_loop(struct node *n, unsigned short terminal_id, unsigned short nb_n
3131

3232
struct allocation *give_for_v_address(struct leader_resources *l_r, size_t v_address, size_t *part);
3333

34+
size_t size_of_allocation(struct allocation *a);
35+
3436
#endif /* !DISTRIBUTEDMALLOC_LEADER_H */

src/network/leader.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ int free_memory(size_t address, struct leader_resources *l_r) {
8181
for (size_t j = 0; j < reg->allocs[i]->number_parts; j++) {
8282
if (reg->allocs[i]->parts[j]->virtual_address <= address
8383
&& reg->allocs[i]->parts[j]->virtual_address + reg->allocs[i]->parts[j]->size > address) {
84+
l_r->availaible_memory += size_of_allocation(reg->allocs[i]);
8485
// Set all block of this alloc as free
8586
for (j = 0; j < reg->allocs[i]->number_parts; j++) {
8687
reg->allocs[i]->parts[j]->free = 0;
@@ -153,14 +154,19 @@ size_t alloc_memory(size_t size, struct leader_resources *l_r) {
153154
a->parts = NULL;
154155
a->v_address_start = SIZE_MAX;
155156
ssize_t m_size = size;
157+
size_t m_t_size = 0;
156158
for (size_t i = 0; i < blks->nb_blocks; i++) {
157159
struct block *b = blks->blks[i];
158160
while (b && b->id != l_r->id && m_size > 0) {
159161
if (b->free == 0) {
160162
b->free = 1;
161-
m_size -= b->size;
162-
if (m_size < 0) {
163-
b = split_block_u(b, -1 * m_size);
163+
if ((ssize_t) b->size >= m_size) {
164+
m_t_size = m_size;
165+
m_size = 0;
166+
b = split_block_u(b, m_t_size);
167+
} else {
168+
m_t_size = m_size - b->size;
169+
m_size -= b->size;
164170
}
165171
if (a->v_address_start == SIZE_MAX)
166172
a->v_address_start = b->virtual_address;

src/utils/utils.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,9 @@ void print_allocations_table(struct leader_resources *l_r) {
3030
size_t size = 0;
3131
size_t total_size = 0;
3232
for (size_t i = 0; i < a_r->count_alloc; i++) {
33-
size = 0;
3433
if (a_r->allocs[i]) {
35-
for (size_t j = 0; j < a_r->allocs[i]->number_parts; j++) {
36-
size += a_r->allocs[i]->parts[j]->size;
37-
total_size += a_r->allocs[i]->parts[j]->size;
38-
}
34+
size = size_of_allocation(a_r->allocs[i]);
35+
total_size += size;
3936
printf("%4zu : %9zu : %5zu : %8zu : ", i, a_r->allocs[i]->v_address_start, a_r->allocs[i]->number_parts,
4037
size);
4138
for (size_t j = 0; j < a_r->allocs[i]->number_parts; j++) {

0 commit comments

Comments
 (0)