Skip to content

Commit e0127f3

Browse files
Chris RohlfChris Rohlf
authored andcommitted
use thread zone cache when looking up a chunk to zone mapping, clang format
1 parent e56d3e4 commit e0127f3

4 files changed

Lines changed: 35 additions & 7 deletions

File tree

src/iso_alloc.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ INTERNAL_HIDDEN iso_alloc_root *iso_alloc_new_root(void) {
328328
r->guard_below = p;
329329
create_guard_page(r->guard_below);
330330

331-
r->guard_above = (void *) ROUND_UP_PAGE((uintptr_t)(p + sizeof(iso_alloc_root) + r->system_page_size));
331+
r->guard_above = (void *) ROUND_UP_PAGE((uintptr_t) (p + sizeof(iso_alloc_root) + r->system_page_size));
332332
create_guard_page(r->guard_above);
333333
return r;
334334
}
@@ -354,7 +354,7 @@ INTERNAL_HIDDEN void iso_alloc_initialize_global_root(void) {
354354
/* Allocate memory with guard pages to hold zone data */
355355
void *p = mmap_rw_pages(_root->zones_size, false, NULL);
356356
create_guard_page(p);
357-
create_guard_page((void *) (uintptr_t)(p + _root->zones_size) - g_page_size);
357+
create_guard_page((void *) (uintptr_t) (p + _root->zones_size) - g_page_size);
358358

359359
_root->zones = (void *) (p + g_page_size);
360360
name_mapping(p, _root->zones_size, "isoalloc zone metadata");
@@ -1188,6 +1188,20 @@ INTERNAL_HIDDEN iso_alloc_big_zone *iso_find_big_zone(void *p) {
11881188
INTERNAL_HIDDEN iso_alloc_zone *iso_find_zone_bitmap_range(void *p) {
11891189
iso_alloc_zone *zone = NULL;
11901190

1191+
#if THREAD_SUPPORT && THREAD_ZONE_CACHE
1192+
/* Hot Path: Check the thread cache for a zone this
1193+
* thread recently used for an alloc/free operation */
1194+
for(int64_t i = 0; i < thread_zone_cache_count; i++) {
1195+
UNMASK_ZONE_PTRS(thread_zone_cache[i].zone);
1196+
zone = thread_zone_cache[i].zone;
1197+
if(zone->bitmap_start <= p && (zone->bitmap_start + zone->bitmap_size) > p) {
1198+
MASK_ZONE_PTRS(zone);
1199+
return zone;
1200+
}
1201+
MASK_ZONE_PTRS(zone);
1202+
}
1203+
#endif
1204+
11911205
for(int32_t i = 0; i < _root->zones_used; i++) {
11921206
zone = &_root->zones[i];
11931207

@@ -1207,6 +1221,20 @@ INTERNAL_HIDDEN iso_alloc_zone *iso_find_zone_bitmap_range(void *p) {
12071221
INTERNAL_HIDDEN iso_alloc_zone *iso_find_zone_range(void *p) {
12081222
iso_alloc_zone *zone = NULL;
12091223

1224+
#if THREAD_SUPPORT && THREAD_ZONE_CACHE
1225+
/* Hot Path: Check the thread cache for a zone this
1226+
* thread recently used for an alloc/free operation */
1227+
for(int64_t i = 0; i < thread_zone_cache_count; i++) {
1228+
UNMASK_ZONE_PTRS(thread_zone_cache[i].zone);
1229+
zone = thread_zone_cache[i].zone;
1230+
if(zone->user_pages_start <= p && (zone->user_pages_start + ZONE_USER_SIZE) > p) {
1231+
MASK_ZONE_PTRS(zone);
1232+
return zone;
1233+
}
1234+
MASK_ZONE_PTRS(zone);
1235+
}
1236+
#endif
1237+
12101238
for(int32_t i = 0; i < _root->zones_used; i++) {
12111239
zone = &_root->zones[i];
12121240

@@ -1373,7 +1401,7 @@ INTERNAL_HIDDEN FLATTEN void iso_free_chunk_from_zone(iso_alloc_zone *zone, void
13731401
LOG_AND_ABORT("Chunk at 0x%p of zone[%d] is not %d byte aligned", p, zone->index, ALIGNMENT);
13741402
}
13751403

1376-
uint64_t chunk_offset = (uint64_t)(p - zone->user_pages_start);
1404+
uint64_t chunk_offset = (uint64_t) (p - zone->user_pages_start);
13771405

13781406
/* Ensure the pointer is a multiple of chunk size */
13791407
if(UNLIKELY((chunk_offset % zone->chunk_size) != 0)) {

src/iso_alloc_profiler.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ INTERNAL_HIDDEN uint64_t _iso_alloc_zone_leak_detector(iso_alloc_zone *zone, boo
115115

116116
if(profile == false) {
117117
LOG("Zone[%d] Total number of %d byte chunks(%d) used and free'd (%lu) (%d percent)", zone->index, zone->chunk_size, GET_CHUNK_COUNT(zone),
118-
was_used, (int32_t)((float) was_used / (GET_CHUNK_COUNT(zone)) * 100.0));
118+
was_used, (int32_t) ((float) was_used / (GET_CHUNK_COUNT(zone)) * 100.0));
119119
}
120120

121121
MASK_ZONE_PTRS(zone);
@@ -127,7 +127,7 @@ INTERNAL_HIDDEN uint64_t _iso_alloc_zone_leak_detector(iso_alloc_zone *zone, boo
127127
* in use and previously used by this zone */
128128
if(profile == true) {
129129
uint64_t total = (in_use + was_used);
130-
return (uint64_t)((float) total / (GET_CHUNK_COUNT(zone)) * 100.0);
130+
return (uint64_t) ((float) total / (GET_CHUNK_COUNT(zone)) * 100.0);
131131
}
132132
#endif
133133
return in_use;

src/iso_alloc_sanity.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ INTERNAL_HIDDEN void *_iso_alloc_sample(size_t size) {
228228

229229
sane_alloc->guard_below = p;
230230
create_guard_page(sane_alloc->guard_below);
231-
sane_alloc->guard_above = (void *) ROUND_UP_PAGE((uintptr_t)(p + (g_page_size * 2)));
231+
sane_alloc->guard_above = (void *) ROUND_UP_PAGE((uintptr_t) (p + (g_page_size * 2)));
232232
create_guard_page(sane_alloc->guard_above);
233233

234234
p = (p + g_page_size);

src/iso_alloc_search.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ INTERNAL_HIDDEN void _iso_alloc_search_stack(uint8_t *stack_start) {
8484
LOG_AND_ABORT("Chunk at 0x%p of zone[%d] is not %d byte aligned", p, zone->index, ALIGNMENT);
8585
}
8686

87-
uint64_t chunk_offset = (uint64_t)(p - (uint64_t *) zone->user_pages_start);
87+
uint64_t chunk_offset = (uint64_t) (p - (uint64_t *) zone->user_pages_start);
8888
LOG("zone[%d] user_pages_start=%p value=%p %lu %d", zone->index, zone->user_pages_start, p, chunk_offset, zone->chunk_size);
8989

9090
/* Ensure the pointer is a multiple of chunk size */

0 commit comments

Comments
 (0)