From 1262df652f766782253fed51526ba60609859719 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 1 Sep 2011 14:18:38 -0700 Subject: [PATCH] Reduce the amount of locking in the kernel's memory region The only thing here that really needs locking on malloc and free is access to the allocation list, which is only used for TRACK_ALLOCATIONS. Improves bench/task-perf-vector-party by 70%. --- src/rt/memory_region.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/rt/memory_region.cpp b/src/rt/memory_region.cpp index c9c7c72d9c7..1527addb0f6 100644 --- a/src/rt/memory_region.cpp +++ b/src/rt/memory_region.cpp @@ -37,7 +37,6 @@ void memory_region::dec_alloc() { void memory_region::free(void *mem) { // printf("free: ptr 0x%" PRIxPTR" region=%p\n", (uintptr_t) mem, this); if (!mem) { return; } - if (_synchronized) { _lock.lock(); } alloc_header *alloc = get_header(mem); assert(alloc->magic == MAGIC); if (_live_allocations < 1) { @@ -45,7 +44,6 @@ void memory_region::free(void *mem) { } release_alloc(mem); _srv->free(alloc); - if (_synchronized) { _lock.unlock(); } } void * @@ -78,7 +76,6 @@ memory_region::realloc(void *mem, size_t size) { void * memory_region::malloc(size_t size, const char *tag, bool zero) { - if (_synchronized) { _lock.lock(); } size_t old_size = size; size += sizeof(alloc_header); alloc_header *mem = (alloc_header *)_srv->malloc(size); @@ -91,7 +88,6 @@ memory_region::malloc(size_t size, const char *tag, bool zero) { memset(mem->data, 0, old_size); } - if (_synchronized) { _lock.unlock(); } return mem->data; } @@ -145,6 +141,7 @@ memory_region::release_alloc(void *mem) { assert(alloc->magic == MAGIC); #ifdef TRACK_ALLOCATIONS + if (_synchronized) { _lock.lock(); } if (_allocation_list[alloc->index] != alloc) { printf("free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n", (uintptr_t) &alloc->data, alloc->tag); @@ -155,6 +152,7 @@ memory_region::release_alloc(void *mem) { _allocation_list[alloc->index] = NULL; alloc->index = -1; } + if (_synchronized) { _lock.unlock(); } #endif dec_alloc(); } @@ -164,7 +162,9 @@ memory_region::claim_alloc(void *mem) { alloc_header *alloc = get_header(mem); assert(alloc->magic == MAGIC); #ifdef TRACK_ALLOCATIONS + if (_synchronized) { _lock.lock(); } alloc->index = _allocation_list.append(alloc); + if (_synchronized) { _lock.unlock(); } #endif add_alloc(); }