Commit 08eaf02c authored by Lucas Plichon's avatar Lucas Plichon
Browse files

un solide alloc medium, need free (et crash 1 fois/2, alignement mémoire ?)

parent 1f6d2714
......@@ -18,10 +18,10 @@ unsigned long knuth_mmix_one_round(unsigned long in)
void *mark_memarea_and_get_user_ptr(void *ptr, unsigned long size, MemKind k)
{
unsigned long magic_number = (knuth_mmix_one_round((unsigned long) ptr) & ~(0b11UL)) | k;
((unsigned long*) ptr)[0] = size;
((unsigned long*) ptr)[1] = magic_number;
((unsigned long*) ptr)[size/8 - 2] = magic_number;
((unsigned long*) ptr)[size/8 - 1] = size;
*((unsigned long*) ptr) = size;
*((unsigned long*) ptr+1) = magic_number;
*((unsigned long*) ptr + size/8 - 2) = magic_number;
*((unsigned long*) ptr + size/8 - 1) = size;
// changed from + 2 to + 16 because taille + magic = 8B != 8b
return ptr + 16;
}
......@@ -29,17 +29,18 @@ void *mark_memarea_and_get_user_ptr(void *ptr, unsigned long size, MemKind k)
Alloc
mark_check_and_get_alloc(void *ptr)
{
void* data_beginning = ((char*) ptr) -16;
unsigned long size = *((unsigned long*) (ptr - 2 * sizeof(unsigned long)));
unsigned long magic_number = *((unsigned long*) (ptr - sizeof(unsigned long)));
unsigned long magic_number = *(unsigned long*)((char*) data_beginning + 8);
MemKind k = magic_number & 0b11UL;
unsigned long expected_magic_number = (knuth_mmix_one_round((unsigned long) (ptr - 2 * sizeof(unsigned long))) & ~(0b11UL)) | k;
assert(magic_number == expected_magic_number);
// assertion of marquage coherence for magic_number & size
printf("size: %lx, expected: %lx\n", size, expected_magic_number);
unsigned long size_at_end = ((unsigned long*) ptr)[size/8 - 3];
assert(size == size_at_end);
Alloc a = { ptr - 2 * sizeof(unsigned long), k, size };
return a;
}
......
......@@ -29,26 +29,26 @@ emalloc_medium(unsigned long size)
assert(size > SMALLALLOC);
/* Step 1: assert disponibility of nearest size block */
int nearest_puiss2 = puiss2(size);
printf("0. puiss2(size) : %i\n", nearest_puiss2);
printf("0. arena.TZL[nearest puiss 2] = %p\n", arena.TZL[nearest_puiss2]);
//printf("0. puiss2(size) : %i\n", nearest_puiss2);
//printf("0. arena.TZL[nearest puiss 2] = %p\n", arena.TZL[nearest_puiss2]);
if (arena.TZL[nearest_puiss2]){
printf("1. medium : first condition reached\n");
mark_memarea_and_get_user_ptr(arena.TZL[nearest_puiss2], nearest_puiss2, MEDIUM_KIND);
//printf("1. medium : first condition reached, NEAR SIZED BLOCK AVAILABLE\n");
return mark_memarea_and_get_user_ptr(arena.TZL[nearest_puiss2], nearest_puiss2, MEDIUM_KIND);
/* Step 2: find a higher size block*/
} else {
printf("2.medium : second condition reached\n");
//printf("2.medium : second condition reached TRYING TO FIND A LARGER BLOCK\n");
while (!arena.TZL[nearest_puiss2]){
if (nearest_puiss2 > FIRST_ALLOC_MEDIUM_EXPOSANT + arena.medium_next_exponant){
printf("2.medium : max size reached, reload\n");
if (nearest_puiss2 >= FIRST_ALLOC_MEDIUM_EXPOSANT + arena.medium_next_exponant){
//printf("2.medium : no higher size find, reallocation\n");
mem_realloc_medium();
printf("2.MAX BOI : %i\n", FIRST_ALLOC_MEDIUM_EXPOSANT + arena.medium_next_exponant);
printf("2.near power 2 : %i\n", nearest_puiss2);
printf("2.arena.TZL[%i] = %p\n", nearest_puiss2 -1 , arena.TZL[ nearest_puiss2 -1 ]);
emalloc_medium(size);
// printf("2.MAX BOI : %i\n", FIRST_ALLOC_MEDIUM_EXPOSANT + arena.medium_next_exponant);
// printf("2.near power 2 : %i\n", nearest_puiss2);
// printf("2.arena.TZL[%i] = %p\n", nearest_puiss2 -1 , arena.TZL[ nearest_puiss2 -1 ]);
return emalloc_medium(size);
}
nearest_puiss2++;
}
......@@ -57,26 +57,26 @@ emalloc_medium(unsigned long size)
while (nearest_puiss2 != 0){
/* we put second half of the block in arena.TZL[puiss2 -1] */
int puiss2_start = nearest_puiss2;
printf("3.medium : third condition reached\n");
printf("3.near power 2 : %i\n", nearest_puiss2);
printf("3.arena.TZL[near_power_2] = %p\n", arena.TZL[ nearest_puiss2 ]);
printf("%lu\n", (unsigned long) arena.TZL[nearest_puiss2]);
printf("%lu\n", (unsigned long) 1 << nearest_puiss2);
// printf("3.medium : third condition reached, HIGHER SIZED BLOCK FOUND, NOW BUDDY\n");
// printf("3.near power 2 : %i\n", nearest_puiss2);
// printf("3.arena.TZL[%i] = %p\n",nearest_puiss2, arena.TZL[ nearest_puiss2 ]);
unsigned long buddy_adress = (unsigned long) arena.TZL[puiss2_start] ^ (unsigned long) (1 << nearest_puiss2);
printf("3.arena.TZL[near_power_2 - 1] = %p\n", arena.TZL[ nearest_puiss2 -1 ]);
printf("buddy adress: %lu\n", buddy_adress);
// printf("3.arena.TZL[%i] = %p\n", nearest_puiss2 -1, arena.TZL[ nearest_puiss2 -1 ]);
// printf("buddy adress: %lx\n", buddy_adress);
arena.TZL[nearest_puiss2 -1] = (void *) buddy_adress;
printf("3.arena.TZL[near_power_2 - 1] = %p\n", arena.TZL[ nearest_puiss2 -1 ]);
printf("\n\n");
// printf("3.arena.TZL[%i] = %p\n", nearest_puiss2 -1, arena.TZL[ nearest_puiss2 -1 ]);
// printf("44.arena.TZL[17] = %p\n", arena.TZL[ 17 ]);
// printf("\n\n");
nearest_puiss2--;
//break;
}
return emalloc_medium(size);
}
return (void *) 0;
//return (void *) 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment