Commit a715f9e4 authored by Jules Sang's avatar Jules Sang
Browse files

cleaned the code

parent 6c089a7d
......@@ -19,11 +19,8 @@ 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) = size;
// *((unsigned long*) ptr+1) = magic_number;
*(unsigned long*) (((char*) ptr) + 8) = magic_number;
// *((unsigned long*) ptr + size/8 - 2)= magic_number;
*(unsigned long*) (((char*) ptr) + size - 16) = magic_number;
// *((unsigned long*) ptr + size/8 - 1) = size;
*(unsigned long*) (((char*) ptr) + size - 8) = size;
return ((char*) ptr) + 16;
}
......@@ -31,20 +28,15 @@ void *mark_memarea_and_get_user_ptr(void *ptr, unsigned long size, MemKind k)
Alloc
mark_check_and_get_alloc(void *ptr)
{
// size and magic number areas are 16B before ptr
void* data_beginning = ((char*) ptr) - 16;
unsigned long size = *((unsigned long *) data_beginning);
unsigned long magic_number = *(unsigned long*)((char*) data_beginning + 8);
MemKind k = magic_number & 0b11UL;
// check magic number's coherence
unsigned long expected_magic_number = (knuth_mmix_one_round((unsigned long) data_beginning) & ~(0b11UL)) | k;
assert(magic_number == expected_magic_number);
// 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);
Alloc a = { data_beginning, k, size };
return a;
}
......
......@@ -29,64 +29,38 @@ 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]);
if (arena.TZL[nearest_puiss2]){
//printf("1. medium : first condition reached, NEAR SIZED BLOCK AVAILABLE\n");
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*/
/* Step 2: find a higher sized block*/
} else {
//printf("2.medium : second condition reached TRYING TO FIND A LARGER BLOCK\n");
// iterate while the list in arena.TZL[nearest_puiss2] is empty and nearest_puiss2 < FIRST_ALLOC_MEDIUM_EXPOSANT + arena.medium_next_exponant
while (!arena.TZL[nearest_puiss2]){
// if nearest_puiss2 exceeds the maximum index, reallocate a block to the end of arena.TZL
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 ]);
// this time, the last block will be found
return emalloc_medium(size);
}
nearest_puiss2++;
}
// split the big block in order to have one block of each lower size
while (nearest_puiss2 != 0) {
void** ptr = arena.TZL[nearest_puiss2];
void* buddy = (void*) ((unsigned long) ptr ^ nearest_puiss2);
// there is no next element of the list since we are filling it
*ptr = NULL;
// the buddy is placed in the lower index list and will be split
arena.TZL[nearest_puiss2 - 1] = buddy;
--nearest_puiss2;
}
*((void**) arena.TZL[0]) = NULL;
/* Step 3: higher size block found, now recursive cut (buddy way)*/
// 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);
// // 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;
// }
return emalloc_medium(size);
}
//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