Skip to content

Commit 15d1247

Browse files
committed
limit free list extension in secure mode to reduce memory usage
1 parent 672656a commit 15d1247

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

‎src/alloc/alloc-generic.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ static inline mi_page_t* mi_page_queue_lookup_free_first(mi_theap_t* theap, mi_p
210210
mi_page_t* page = pq->first;
211211
if mi_likely(page!=NULL && mi_page_free_quick_collect(page)) {
212212
// fast path
213-
#if MI_SECURE_RANDOM_EXTEND // in secure mode, we extend half the time to increase randomness
214-
if (page->capacity < page->reserved && ((_mi_theap_random_next(theap) & 1) == 1)) {
213+
#if MI_SECURE_RANDOM_EXTEND // in secure mode, we extend half the time to increase randomness (but only if at least half used to limit the working set)
214+
if (page->capacity < page->reserved && mi_page_used(page) >= page->capacity/2 && ((_mi_theap_random_next(theap) & 1) == 1)) {
215215
(void)_mi_page_extend_free(theap, page); // ok if this fails
216216
mi_assert_internal(mi_page_immediate_available(page));
217217
}

‎src/alloc/page.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,8 @@ static mi_decl_noinline void mi_page_free_list_extend( mi_page_t* const page, co
956956

957957
#define MI_MAX_EXTEND_SIZE (8*1024) // heuristic, one or two OS pages seems to work well.
958958
#if MI_SECURE_SHUFFLE_FREELIST
959-
#define MI_MIN_EXTEND(bsize) ((bsize) <= MI_SMALL_MAX_OBJ_SIZE ? 32 : 8) // extend at least by this many (for free list randomization)
959+
// extend at least by this many (for free list randomization): 32 blocks, but at most `2*MI_MAX_EXTEND_SIZE` bytes (and at least 4 blocks)
960+
#define MI_MIN_EXTEND(bsize) ((bsize) <= (2*MI_MAX_EXTEND_SIZE)/32 ? 32 : ((bsize) >= (2*MI_MAX_EXTEND_SIZE)/4 ? 4 : (2*MI_MAX_EXTEND_SIZE)/(bsize)))
960961
#else
961962
#define MI_MIN_EXTEND(bsize) (1)
962963
#endif

0 commit comments

Comments
 (0)