[haiku-commits] haiku: hrev43454 - src/system/libroot/posix/malloc_debug

  • From: mmlr@xxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sat, 10 Dec 2011 17:24:18 +0100 (CET)

hrev43454 adds 2 changesets to branch 'master'
old head: 3cb6104e08a5555b081d147136ff38ea3dcf0f21
new head: 268ddbd76f963f8abca784bfa33f43d779181c72

----------------------------------------------------------------------------

3de3806: Update the guarded heap areas after fork.
  
  We don't actually use them for anything yet though.

268ddbd: Fix a few function signatures in the guarded heap.
  
  * Not including malloc.h caused the memalign() signature to not be a C
    signature, therefore leading to linking errors. Fix the missing
    include and explicitly add extern "C" as well.
  * Some remaining asterisk style cleanup.

                                            [ Michael Lotz <mmlr@xxxxxxxx> ]

----------------------------------------------------------------------------

1 files changed, 30 insertions(+), 8 deletions(-)
.../libroot/posix/malloc_debug/guarded_heap.cpp    |   38 ++++++++++++---

############################################################################

Commit:      3de380692acab727237c7ca4dbafa2a1ccf3244c
URL:         http://cgit.haiku-os.org/haiku/commit/?id=3de3806
Author:      Michael Lotz <mmlr@xxxxxxxx>
Date:        Sat Dec 10 16:16:55 2011 UTC

Update the guarded heap areas after fork.

We don't actually use them for anything yet though.

----------------------------------------------------------------------------

diff --git a/src/system/libroot/posix/malloc_debug/guarded_heap.cpp 
b/src/system/libroot/posix/malloc_debug/guarded_heap.cpp
index 98efe3d..8ad3cdb 100644
--- a/src/system/libroot/posix/malloc_debug/guarded_heap.cpp
+++ b/src/system/libroot/posix/malloc_debug/guarded_heap.cpp
@@ -890,6 +890,20 @@ heap_debug_get_allocation_info(void *address, size_t *size,
 // #pragma mark - Init
 
 
+static void
+init_after_fork()
+{
+       // The memory has actually been copied (or is in a copy on write state) 
but
+       // but the area ids have changed.
+       for (guarded_heap_area* area = sGuardedHeap.areas; area != NULL;
+                       area = area->next) {
+               area->area = area_for(area);
+               if (area->area < 0)
+                       panic("failed to find area for heap area %p after 
fork", area);
+       }
+}
+
+
 extern "C" status_t
 __init_heap(void)
 {
@@ -904,6 +918,13 @@ __init_heap(void)
        sigemptyset(&action.sa_mask);
        sigaction(SIGSEGV, &action, NULL);
 
+       atfork(&init_after_fork);
+               // Note: Needs malloc(). Hence we need to be fully initialized.
+               // TODO: We should actually also install a hook that is called 
before
+               // fork() is being executed. In a multithreaded app it would 
need to
+               // acquire *all* allocator locks, so that we don't fork() an
+               // inconsistent state.
+
        return B_OK;
 }
 

############################################################################

Revision:    hrev43454
Commit:      268ddbd76f963f8abca784bfa33f43d779181c72
URL:         http://cgit.haiku-os.org/haiku/commit/?id=268ddbd
Author:      Michael Lotz <mmlr@xxxxxxxx>
Date:        Sat Dec 10 16:20:34 2011 UTC

Fix a few function signatures in the guarded heap.

* Not including malloc.h caused the memalign() signature to not be a C
  signature, therefore leading to linking errors. Fix the missing
  include and explicitly add extern "C" as well.
* Some remaining asterisk style cleanup.

----------------------------------------------------------------------------

diff --git a/src/system/libroot/posix/malloc_debug/guarded_heap.cpp 
b/src/system/libroot/posix/malloc_debug/guarded_heap.cpp
index 8ad3cdb..051d913 100644
--- a/src/system/libroot/posix/malloc_debug/guarded_heap.cpp
+++ b/src/system/libroot/posix/malloc_debug/guarded_heap.cpp
@@ -4,6 +4,7 @@
  */
 
 
+#include <malloc.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -943,7 +944,7 @@ __init_heap_post_env(void)
 // #pragma mark - Public API
 
 
-extern "C" void *
+extern "C" void*
 sbrk_hook(long)
 {
        debug_printf("sbrk not supported on malloc debug\n");
@@ -952,7 +953,7 @@ sbrk_hook(long)
 }
 
 
-void*
+extern "C" void*
 memalign(size_t alignment, size_t size)
 {
        if (size == 0)
@@ -962,14 +963,14 @@ memalign(size_t alignment, size_t size)
 }
 
 
-void*
+extern "C" void*
 malloc(size_t size)
 {
        return memalign(0, size);
 }
 
 
-void
+extern "C" void
 free(void* address)
 {
        if (!guarded_heap_free(address))
@@ -977,7 +978,7 @@ free(void* address)
 }
 
 
-void*
+extern "C" void*
 realloc(void* address, size_t newSize)
 {
        if (newSize == 0) {
@@ -992,10 +993,10 @@ realloc(void* address, size_t newSize)
 }
 
 
-void *
+extern "C" void*
 calloc(size_t numElements, size_t size)
 {
-       void *address = malloc(numElements * size);
+       void* address = malloc(numElements * size);
        if (address != NULL)
                memset(address, 0, numElements * size);
 
@@ -1003,7 +1004,7 @@ calloc(size_t numElements, size_t size)
 }
 
 
-extern "C" void *
+extern "C" void*
 valloc(size_t size)
 {
        return memalign(B_PAGE_SIZE, size);


Other related posts:

  • » [haiku-commits] haiku: hrev43454 - src/system/libroot/posix/malloc_debug - mmlr