Commit be387750 authored by Bastien Legoy's avatar Bastien Legoy
Browse files

Buddy has gone too far

parent d43a35d6
......@@ -28,13 +28,15 @@ emalloc_medium(unsigned long size)
assert(size < LARGEALLOC);
assert(size > SMALLALLOC);
unsigned long memory_size = size + 32;
// Compute the TZL Index
unsigned int index = puiss2(size);
unsigned int index = puiss2(memory_size);
// Verify Index is not out-of-bound
if (index > FIRST_ALLOC_MEDIUM_EXPOSANT + arena.medium_next_exponant)
{
if (mem_realloc_medium() < size)
if (mem_realloc_medium() < memory_size)
{
return emalloc_medium(size);
}
......@@ -43,7 +45,7 @@ emalloc_medium(unsigned long size)
*(void**) arena.TZL[FIRST_ALLOC_MEDIUM_EXPOSANT + arena.medium_next_exponant] = NULL;
}
//
// If there is no remaining Memory Block in List
if (arena.TZL[index] == NULL)
{
// Find the Closest Filled Index
......@@ -63,7 +65,7 @@ emalloc_medium(unsigned long size)
return emalloc_medium(size);
}
// Half each element in ]index,closest_index]
// Half one element in ]index,closest_index]
for (unsigned int ii = closest_index; ii > index; --ii)
{
// Take next Head
......@@ -72,7 +74,9 @@ emalloc_medium(unsigned long size)
// Split and Construct next List
arena.TZL[ii - 1] = head_element;
*(void**) head_element = (head_element + (1 << (ii - 1)));
unsigned long offset = 1 << (ii - 1);
*(void**) head_element = (head_element + offset);
*(void**) (head_element + offset) = NULL;
}
}
......@@ -80,17 +84,18 @@ emalloc_medium(unsigned long size)
void* head_element = arena.TZL[index];
arena.TZL[index] = *(void**) arena.TZL[index];
return mark_memarea_and_get_user_ptr(head_element, size, MEDIUM_KIND);
return mark_memarea_and_get_user_ptr(head_element, memory_size, MEDIUM_KIND);
}
void efree_medium(Alloc a) {
// assert(size < LARGEALLOC);
// assert(size > SMALLALLOC);
// Compute Index & Buddy
void* ptr = (a.ptr - 16);
unsigned int index = puiss2(a.size);
void* buddy = (void*)((unsigned long)a.ptr ^ a.size);
void* buddy = (void*)( ((unsigned long)ptr) ^ (1UL << index) );
// Foreach element in Chained List
void* previous_head = NULL;
......@@ -105,7 +110,7 @@ void efree_medium(Alloc a) {
// Remove from List
*(void**) previous_head = *(void**) head;
// Add new Block
void* target_head = ((unsigned long)(a.ptr) < (unsigned long)(buddy)) ? a.ptr : buddy;
void* target_head = ((unsigned long)ptr < (unsigned long)buddy) ? ptr : buddy;
*(void**) target_head = arena.TZL[index + 1];
arena.TZL[index + 1] = target_head;
// Recursive Call
......@@ -123,8 +128,8 @@ void efree_medium(Alloc a) {
// If Buddy is NOT contained in the List:
// - add Ptr in List
*(void**) a.ptr = arena.TZL[index];
arena.TZL[index] = a.ptr;
*(void**) ptr = arena.TZL[index];
arena.TZL[index] = 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