Skip to content

Commit 1262df6

Browse files
committed
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%.
1 parent dabf1be commit 1262df6

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/rt/memory_region.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,13 @@ void memory_region::dec_alloc() {
3737
void memory_region::free(void *mem) {
3838
// printf("free: ptr 0x%" PRIxPTR" region=%p\n", (uintptr_t) mem, this);
3939
if (!mem) { return; }
40-
if (_synchronized) { _lock.lock(); }
4140
alloc_header *alloc = get_header(mem);
4241
assert(alloc->magic == MAGIC);
4342
if (_live_allocations < 1) {
4443
_srv->fatal("live_allocs < 1", __FILE__, __LINE__, "");
4544
}
4645
release_alloc(mem);
4746
_srv->free(alloc);
48-
if (_synchronized) { _lock.unlock(); }
4947
}
5048

5149
void *
@@ -78,7 +76,6 @@ memory_region::realloc(void *mem, size_t size) {
7876

7977
void *
8078
memory_region::malloc(size_t size, const char *tag, bool zero) {
81-
if (_synchronized) { _lock.lock(); }
8279
size_t old_size = size;
8380
size += sizeof(alloc_header);
8481
alloc_header *mem = (alloc_header *)_srv->malloc(size);
@@ -91,7 +88,6 @@ memory_region::malloc(size_t size, const char *tag, bool zero) {
9188
memset(mem->data, 0, old_size);
9289
}
9390

94-
if (_synchronized) { _lock.unlock(); }
9591
return mem->data;
9692
}
9793

@@ -145,6 +141,7 @@ memory_region::release_alloc(void *mem) {
145141
assert(alloc->magic == MAGIC);
146142

147143
#ifdef TRACK_ALLOCATIONS
144+
if (_synchronized) { _lock.lock(); }
148145
if (_allocation_list[alloc->index] != alloc) {
149146
printf("free: ptr 0x%" PRIxPTR " (%s) is not in allocation_list\n",
150147
(uintptr_t) &alloc->data, alloc->tag);
@@ -155,6 +152,7 @@ memory_region::release_alloc(void *mem) {
155152
_allocation_list[alloc->index] = NULL;
156153
alloc->index = -1;
157154
}
155+
if (_synchronized) { _lock.unlock(); }
158156
#endif
159157
dec_alloc();
160158
}
@@ -164,7 +162,9 @@ memory_region::claim_alloc(void *mem) {
164162
alloc_header *alloc = get_header(mem);
165163
assert(alloc->magic == MAGIC);
166164
#ifdef TRACK_ALLOCATIONS
165+
if (_synchronized) { _lock.lock(); }
167166
alloc->index = _allocation_list.append(alloc);
167+
if (_synchronized) { _lock.unlock(); }
168168
#endif
169169
add_alloc();
170170
}

0 commit comments

Comments
 (0)