malloc_debug: Add default alignment option.

This allows for something similar as was implemented in 217f090 but
makes it optional and configurable.

The MALLOC_DEBUG environment variable now can take "a<size>" to set
the default alignment to the specified size. Note that not all
alignments may be supported depending on the heap implementation.
This commit is contained in:
Michael Lotz
2015-04-04 22:55:57 +02:00
parent b0e31a9ce3
commit 121655e9ee
3 changed files with 38 additions and 5 deletions
+1
View File
@@ -18,6 +18,7 @@ status_t heap_debug_stop_wall_checking();
void heap_debug_set_memory_reuse(bool enabled); void heap_debug_set_memory_reuse(bool enabled);
void heap_debug_set_paranoid_validation(bool enabled); void heap_debug_set_paranoid_validation(bool enabled);
void heap_debug_set_debugger_calls(bool enabled); void heap_debug_set_debugger_calls(bool enabled);
void heap_debug_set_default_alignment(size_t defaultAlignment);
void heap_debug_validate_heaps(); void heap_debug_validate_heaps();
void heap_debug_validate_walls(); void heap_debug_validate_walls();
@@ -19,6 +19,7 @@
static bool sDebuggerCalls = true; static bool sDebuggerCalls = true;
static size_t sDefaultAlignment = 0;
static void static void
@@ -833,6 +834,13 @@ heap_debug_set_debugger_calls(bool enabled)
} }
extern "C" void
heap_debug_set_default_alignment(size_t defaultAlignment)
{
sDefaultAlignment = defaultAlignment;
}
extern "C" void extern "C" void
heap_debug_validate_heaps() heap_debug_validate_heaps()
{ {
@@ -937,6 +945,14 @@ __init_heap_post_env(void)
if (mode != NULL) { if (mode != NULL) {
if (strchr(mode, 'r')) if (strchr(mode, 'r'))
heap_debug_set_memory_reuse(false); heap_debug_set_memory_reuse(false);
size_t defaultAlignment = 0;
const char *argument = strchr(mode, 'a');
if (argument != NULL
&& sscanf(argument, "a%" B_SCNuSIZE, &defaultAlignment) == 1
&& defaultAlignment > 0) {
heap_debug_set_default_alignment(defaultAlignment);
}
} }
} }
@@ -966,7 +982,7 @@ memalign(size_t alignment, size_t size)
extern "C" void* extern "C" void*
malloc(size_t size) malloc(size_t size)
{ {
return memalign(0, size); return memalign(sDefaultAlignment, size);
} }
@@ -987,7 +1003,7 @@ realloc(void* address, size_t newSize)
} }
if (address == NULL) if (address == NULL)
return memalign(0, newSize); return memalign(sDefaultAlignment, newSize);
return guarded_heap_realloc(address, newSize); return guarded_heap_realloc(address, newSize);
} }
+19 -3
View File
@@ -44,6 +44,7 @@ static bool sParanoidValidation = false;
static thread_id sWallCheckThread = -1; static thread_id sWallCheckThread = -1;
static bool sStopWallChecking = false; static bool sStopWallChecking = false;
static bool sUseGuardPage = false; static bool sUseGuardPage = false;
static bool sDefaultAlignment = 0;
void void
@@ -1465,7 +1466,7 @@ heap_realloc(heap_allocator *heap, void *address, void **newAddress,
newSize -= sizeof(addr_t) + sizeof(heap_leak_check_info); newSize -= sizeof(addr_t) + sizeof(heap_leak_check_info);
// if not, allocate a new chunk of memory // if not, allocate a new chunk of memory
*newAddress = memalign(0, newSize); *newAddress = memalign(sDefaultAlignment, newSize);
if (*newAddress == NULL) { if (*newAddress == NULL) {
// we tried but it didn't work out, but still the operation is done // we tried but it didn't work out, but still the operation is done
return B_OK; return B_OK;
@@ -1681,6 +1682,13 @@ heap_debug_set_debugger_calls(bool enabled)
} }
extern "C" void
heap_debug_set_default_alignment(size_t defaultAlignment)
{
sDefaultAlignment = defaultAlignment;
}
extern "C" void extern "C" void
heap_debug_validate_heaps() heap_debug_validate_heaps()
{ {
@@ -1839,6 +1847,14 @@ __init_heap_post_env(void)
sUseGuardPage = true; sUseGuardPage = true;
if (strchr(mode, 'r')) if (strchr(mode, 'r'))
heap_debug_set_memory_reuse(false); heap_debug_set_memory_reuse(false);
size_t defaultAlignment = 0;
const char *argument = strchr(mode, 'a');
if (argument != NULL
&& sscanf(argument, "a%" B_SCNuSIZE, &defaultAlignment) == 1
&& defaultAlignment > 0) {
heap_debug_set_default_alignment(defaultAlignment);
}
} }
} }
@@ -1937,7 +1953,7 @@ malloc(size_t size)
if (sUseGuardPage) if (sUseGuardPage)
return heap_debug_malloc_with_guard_page(size); return heap_debug_malloc_with_guard_page(size);
return memalign(0, size); return memalign(sDefaultAlignment, size);
} }
@@ -1978,7 +1994,7 @@ void *
realloc(void *address, size_t newSize) realloc(void *address, size_t newSize)
{ {
if (address == NULL) if (address == NULL)
return memalign(0, newSize); return memalign(sDefaultAlignment, newSize);
if (newSize == 0) { if (newSize == 0) {
free(address); free(address);