* Use mutex and rw_lock and the proper AutoLockers as in the kernel version, as

these interfaces are now available.
* Don't be quite so paranoid by default, the checks that are on by default
  should be enough to detect most memory corruptions.

This makes the debug heap way more usable, so much that you can even use it as
your normal everyday heap without noticing much performance impact (it has quite 
a bit of additional memory overhead though).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@33544 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2009-10-12 11:29:25 +00:00
parent 93b63126e1
commit 3bb69a8235
2 changed files with 13 additions and 46 deletions
@@ -1,6 +1,7 @@
SubDir HAIKU_TOP src system libroot posix malloc_debug ; SubDir HAIKU_TOP src system libroot posix malloc_debug ;
UsePrivateSystemHeaders ; UsePrivateSystemHeaders ;
UsePrivateHeaders shared ;
MergeObject posix_malloc.o : MergeObject posix_malloc.o :
heap.cpp heap.cpp
+11 -45
View File
@@ -14,6 +14,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <locks.h>
#include <syscalls.h> #include <syscalls.h>
#include <Debug.h> #include <Debug.h>
@@ -42,39 +43,6 @@ panic(const char *format, ...)
} }
class MutexLocker {
public:
MutexLocker(sem_id lock)
:
fLock(lock)
{
Lock();
}
~MutexLocker()
{
if (fLocked)
Unlock();
}
void Lock()
{
fLocked = acquire_sem(fLock) == B_OK;
}
void Unlock()
{
fLocked = false;
release_sem(fLock);
}
sem_id fLock;
bool fLocked;
};
typedef MutexLocker ReadLocker;
typedef MutexLocker WriteLocker;
#define ROUNDUP(x, y) (((x) + (y) - 1) / (y) * (y)) #define ROUNDUP(x, y) (((x) + (y) - 1) / (y) * (y))
#define HEAP_INITIAL_SIZE 1 * 1024 * 1024 #define HEAP_INITIAL_SIZE 1 * 1024 * 1024
@@ -125,15 +93,15 @@ typedef struct heap_page_s {
} heap_page; } heap_page;
typedef struct heap_bin_s { typedef struct heap_bin_s {
sem_id lock; mutex lock;
uint32 element_size; uint32 element_size;
uint16 max_free_count; uint16 max_free_count;
heap_page * page_list; // sorted so that the desired page is always first heap_page * page_list; // sorted so that the desired page is always first
} heap_bin; } heap_bin;
typedef struct heap_allocator_s { typedef struct heap_allocator_s {
sem_id area_lock; rw_lock area_lock;
sem_id page_lock; mutex page_lock;
const char *name; const char *name;
uint32 bin_count; uint32 bin_count;
@@ -382,7 +350,7 @@ heap_validate_walls()
heap_allocator *heap = sHeaps[classIndex]; heap_allocator *heap = sHeaps[classIndex];
ReadLocker areaReadLocker(heap->area_lock); ReadLocker areaReadLocker(heap->area_lock);
for (uint32 i = 0; i < heap->bin_count; i++) for (uint32 i = 0; i < heap->bin_count; i++)
acquire_sem(heap->bins[i].lock); mutex_lock(&heap->bins[i].lock);
MutexLocker pageLocker(heap->page_lock); MutexLocker pageLocker(heap->page_lock);
// go through all the pages in all the areas // go through all the pages in all the areas
@@ -476,7 +444,7 @@ heap_validate_walls()
pageLocker.Unlock(); pageLocker.Unlock();
for (uint32 i = 0; i < heap->bin_count; i++) for (uint32 i = 0; i < heap->bin_count; i++)
release_sem(heap->bins[i].lock); mutex_unlock(&heap->bins[i].lock);
} }
} }
@@ -486,7 +454,7 @@ heap_validate_heap(heap_allocator *heap)
{ {
ReadLocker areaReadLocker(heap->area_lock); ReadLocker areaReadLocker(heap->area_lock);
for (uint32 i = 0; i < heap->bin_count; i++) for (uint32 i = 0; i < heap->bin_count; i++)
acquire_sem(heap->bins[i].lock); mutex_lock(&heap->bins[i].lock);
MutexLocker pageLocker(heap->page_lock); MutexLocker pageLocker(heap->page_lock);
uint32 totalPageCount = 0; uint32 totalPageCount = 0;
@@ -651,7 +619,7 @@ heap_validate_heap(heap_allocator *heap)
pageLocker.Unlock(); pageLocker.Unlock();
for (uint32 i = 0; i < heap->bin_count; i++) for (uint32 i = 0; i < heap->bin_count; i++)
release_sem(heap->bins[i].lock); mutex_unlock(&heap->bins[i].lock);
} }
@@ -809,7 +777,7 @@ heap_create_allocator(const char *name, addr_t base, size_t size,
continue; continue;
heap_bin *bin = &heap->bins[heap->bin_count]; heap_bin *bin = &heap->bins[heap->bin_count];
bin->lock = create_sem(1, "heap bin lock"); mutex_init(&bin->lock, "heap bin lock");
bin->element_size = binSize; bin->element_size = binSize;
bin->max_free_count = heap->page_size / binSize; bin->max_free_count = heap->page_size / binSize;
bin->page_list = NULL; bin->page_list = NULL;
@@ -822,8 +790,8 @@ heap_create_allocator(const char *name, addr_t base, size_t size,
base += heap->bin_count * sizeof(heap_bin); base += heap->bin_count * sizeof(heap_bin);
size -= heap->bin_count * sizeof(heap_bin); size -= heap->bin_count * sizeof(heap_bin);
heap->page_lock = create_sem(1, "heap page lock"); rw_lock_init(&heap->area_lock, "heap area lock");
heap->area_lock = create_sem(1, "heap area lock"); mutex_init(&heap->page_lock, "heap page lock");
heap_add_area(heap, -1, base, size); heap_add_area(heap, -1, base, size);
return heap; return heap;
@@ -1570,8 +1538,6 @@ __init_heap(void)
base += partSize; base += partSize;
} }
heap_debug_set_paranoid_validation(true);
heap_debug_start_wall_checking();
return B_OK; return B_OK;
} }