Commit 6c089a7d authored by Jules Sang's avatar Jules Sang
Browse files

ma version de emalloc_medium + efree_medium

parent 6d17918e
......@@ -42,8 +42,8 @@ mark_check_and_get_alloc(void *ptr)
// assertion of marquage coherence for magic_number & size
// unsigned long size_at_end = *((unsigned long*) data_beginning + size/8 - 1);
unsigned long size_at_end = *(unsigned long*) (((char*) data_beginning + size - 8));
assert(size == size_at_end);
// unsigned long size_at_end = *(unsigned long*) (((char*) data_beginning + size - 8));
// assert(size == size_at_end);
Alloc a = { data_beginning, k, size };
return a;
......
......@@ -34,14 +34,15 @@ emalloc_medium(unsigned long size)
if (arena.TZL[nearest_puiss2]){
//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);
void* ptr = arena.TZL[nearest_puiss2];
// update arena.TZL[nearest_puiss2]
arena.TZL[nearest_puiss2] = *(void**) (ptr);
return mark_memarea_and_get_user_ptr(ptr, nearest_puiss2, MEDIUM_KIND);
/* Step 2: find a higher size block*/
} else {
//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 : no higher size find, reallocation\n");
mem_realloc_medium();
......@@ -53,26 +54,35 @@ emalloc_medium(unsigned long size)
nearest_puiss2++;
}
while (nearest_puiss2 != 0) {
void** ptr = arena.TZL[nearest_puiss2];
void* buddy = (void*) ((unsigned long) ptr ^ nearest_puiss2);
*ptr = NULL;
arena.TZL[nearest_puiss2 - 1] = buddy;
--nearest_puiss2;
}
*((void**) arena.TZL[0]) = NULL;
/* Step 3: higher size block found, now recursive cut (buddy way)*/
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, 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 ]);
// int puiss2_start = nearest_puiss2;
// while (nearest_puiss2 != 0){
// /* we put second half of the block in arena.TZL[puiss2 -1] */
// // 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);
// unsigned long buddy_adress = (unsigned long) arena.TZL[puiss2_start] ^ (unsigned long) (1 << nearest_puiss2);
// 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[%i] = %p\n", nearest_puiss2 -1, arena.TZL[ nearest_puiss2 -1 ]);
// printf("44.arena.TZL[17] = %p\n", arena.TZL[ 17 ]);
// printf("\n\n");
// // 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[%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;
}
// nearest_puiss2--;
// //break;
// }
return emalloc_medium(size);
}
......@@ -81,7 +91,8 @@ emalloc_medium(unsigned long size)
void efree_medium(Alloc a) {
/* ecrire votre code ici */
*(void**)(a.ptr) = arena.TZL[*(unsigned long*) (a.ptr)];
arena.TZL[*(unsigned long*) (a.ptr)] = *(void**)(a.ptr);
}
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