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:
parent
b0e31a9ce3
commit
121655e9ee
@ -18,6 +18,7 @@ status_t heap_debug_stop_wall_checking();
|
||||
void heap_debug_set_memory_reuse(bool enabled);
|
||||
void heap_debug_set_paranoid_validation(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_walls();
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
|
||||
static bool sDebuggerCalls = true;
|
||||
static size_t sDefaultAlignment = 0;
|
||||
|
||||
|
||||
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
|
||||
heap_debug_validate_heaps()
|
||||
{
|
||||
@ -937,6 +945,14 @@ __init_heap_post_env(void)
|
||||
if (mode != NULL) {
|
||||
if (strchr(mode, 'r'))
|
||||
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*
|
||||
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)
|
||||
return memalign(0, newSize);
|
||||
return memalign(sDefaultAlignment, newSize);
|
||||
|
||||
return guarded_heap_realloc(address, newSize);
|
||||
}
|
||||
|
@ -44,6 +44,7 @@ static bool sParanoidValidation = false;
|
||||
static thread_id sWallCheckThread = -1;
|
||||
static bool sStopWallChecking = false;
|
||||
static bool sUseGuardPage = false;
|
||||
static bool sDefaultAlignment = 0;
|
||||
|
||||
|
||||
void
|
||||
@ -1465,7 +1466,7 @@ heap_realloc(heap_allocator *heap, void *address, void **newAddress,
|
||||
newSize -= sizeof(addr_t) + sizeof(heap_leak_check_info);
|
||||
|
||||
// if not, allocate a new chunk of memory
|
||||
*newAddress = memalign(0, newSize);
|
||||
*newAddress = memalign(sDefaultAlignment, newSize);
|
||||
if (*newAddress == NULL) {
|
||||
// we tried but it didn't work out, but still the operation is done
|
||||
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
|
||||
heap_debug_validate_heaps()
|
||||
{
|
||||
@ -1839,6 +1847,14 @@ __init_heap_post_env(void)
|
||||
sUseGuardPage = true;
|
||||
if (strchr(mode, 'r'))
|
||||
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)
|
||||
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)
|
||||
{
|
||||
if (address == NULL)
|
||||
return memalign(0, newSize);
|
||||
return memalign(sDefaultAlignment, newSize);
|
||||
|
||||
if (newSize == 0) {
|
||||
free(address);
|
||||
|
Loading…
x
Reference in New Issue
Block a user