From 3d0d4b6200bc16c89e241afb715ce35f15f50020 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 15 Feb 2011 17:54:24 +0000 Subject: [PATCH] * Implemented a way to specify additional debug options via MALLOC_DEBUG when using libroot_debug.so, instead of having to hardcode them in the application via calling private heap functions. * The following options are implemented: 'p' turns on paranoid validation, 'w' triggers periodic wall checking every 500ms ('W' does the same, but every 100ms), 'g' to use guard pages (beware, this will dramatically increase memory usage), and 'r' which forbids reusing of memory, freed memory is never actually freed. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40514 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/libroot/libroot_private.h | 1 + src/system/libroot/libroot_init.c | 1 + .../libroot/posix/malloc/arch-specific.cpp | 7 +++++ .../libroot/posix/malloc_debug/heap.cpp | 26 ++++++++++++++++++- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/headers/private/libroot/libroot_private.h b/headers/private/libroot/libroot_private.h index 0b1c924061..20d035a10e 100644 --- a/headers/private/libroot/libroot_private.h +++ b/headers/private/libroot/libroot_private.h @@ -32,6 +32,7 @@ status_t __flatten_process_args(const char* const* args, int32 argCount, void _call_atexit_hooks_for_range(addr_t start, addr_t size); void __init_env(const struct user_space_program_args *args); void __init_heap(void); +void __init_heap_post_env(void); void __init_time(void); void __arch_init_time(struct real_time_data *data, bool setDefaults); diff --git a/src/system/libroot/libroot_init.c b/src/system/libroot/libroot_init.c index bceb3d9faf..7739b01828 100644 --- a/src/system/libroot/libroot_init.c +++ b/src/system/libroot/libroot_init.c @@ -58,6 +58,7 @@ initialize_before(image_id imageID) __init_time(); __init_heap(); __init_env(__gRuntimeLoader->program_args); + __init_heap_post_env(); __init_pwd_backend(); } diff --git a/src/system/libroot/posix/malloc/arch-specific.cpp b/src/system/libroot/posix/malloc/arch-specific.cpp index cd51a3651b..8cb1f6723b 100644 --- a/src/system/libroot/posix/malloc/arch-specific.cpp +++ b/src/system/libroot/posix/malloc/arch-specific.cpp @@ -119,6 +119,13 @@ __init_heap(void) } +extern "C" void +__init_heap_post_env(void) +{ + // no heap options available +} + + static void insert_chunk(free_chunk *newChunk) { diff --git a/src/system/libroot/posix/malloc_debug/heap.cpp b/src/system/libroot/posix/malloc_debug/heap.cpp index 2dc4d90126..1b0f85f2a9 100644 --- a/src/system/libroot/posix/malloc_debug/heap.cpp +++ b/src/system/libroot/posix/malloc_debug/heap.cpp @@ -2,13 +2,14 @@ * Copyright 2008-2009, Michael Lotz, mmlr@mlotz.ch. * Distributed under the terms of the MIT License. * - * Copyright 2002-2006, Axel Dörfler, axeld@pinc-software.de. + * Copyright 2002-2011, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. * * Copyright 2001, Travis Geiselbrecht. All rights reserved. * Distributed under the terms of the NewOS License. */ + #include #include #include @@ -41,6 +42,7 @@ static bool sReuseMemory = true; static bool sParanoidValidation = false; static thread_id sWallCheckThread = -1; static bool sStopWallChecking = false; +static bool sUseGuardPage = false; void @@ -1817,6 +1819,25 @@ __init_heap(void) } +extern "C" void +__init_heap_post_env(void) +{ + const char *mode = getenv("MALLOC_DEBUG"); + if (mode != NULL) { + if (strchr(mode, 'p')) + heap_debug_set_paranoid_validation(true); + if (strchr(mode, 'w')) + heap_debug_start_wall_checking(500); + else if (strchr(mode, 'W')) + heap_debug_start_wall_checking(100); + if (strchr(mode, 'g')) + sUseGuardPage = true; + if (strchr(mode, 'r')) + heap_debug_set_memory_reuse(false); + } +} + + // #pragma mark - Public API @@ -1908,6 +1929,9 @@ memalign(size_t alignment, size_t size) void * malloc(size_t size) { + if (sUseGuardPage) + return heap_debug_malloc_with_guard_page(size); + return memalign(0, size); }