From e2a480a95825d9b9ab3b8ec28c24477f6f8f735b Mon Sep 17 00:00:00 2001 From: wangchen Date: Wed, 27 Sep 2023 20:08:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=86=85=E5=AD=98=E5=88=86=E9=85=8D?= =?UTF-8?q?=E5=99=A8=E9=9A=8F=E6=9C=BA=E5=8C=96=20Close=20#I84SCL=20Signed?= =?UTF-8?q?-off-by:=20wangchen=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../partition_allocator/partition_bucket.cc | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/base/allocator/partition_allocator/partition_bucket.cc b/base/allocator/partition_allocator/partition_bucket.cc index 776b7d04d4..83677bc3c0 100644 --- a/base/allocator/partition_allocator/partition_bucket.cc +++ b/base/allocator/partition_allocator/partition_bucket.cc @@ -790,6 +790,37 @@ PartitionBucket::ProvisionMoreSlotsAndAllocOne( // Add all slots that fit within so far committed pages to the free list. PartitionFreelistEntry* prev_entry = nullptr; + +#if defined(OHOS_ENABLE_RANDOM) + uintptr_t random_slot; + int num = (commit_end - next_slot) / size; + int i; + + size_t random[num]; + size_t tmp; + uint32_t rand; + uint32_t randseed = RandomValue(); + size_t free_list_entries_added = 0; + + for (i = 0; i < num; i++) { + random[i] = i; + } + + for (i = num - 1; i > 0; i--) { + rand = randseed % i; + tmp = random[i]; + random[i] = random[rand]; + random[rand] = tmp; + } + + for (i = 0; i < num; i++) { + random_slot = next_slot + size * random[i]; + if (LIKELY(size <= kMaxMemoryTaggingSize)) { + random_slot = memory::TagMemoryRangeRandomly(random_slot, size); + } + auto* entry = + new (reinterpret_cast(random_slot)) PartitionFreelistEntry(); +#else uintptr_t next_slot_end = next_slot + size; size_t free_list_entries_added = 0; while (next_slot_end <= commit_end) { @@ -798,6 +829,7 @@ PartitionBucket::ProvisionMoreSlotsAndAllocOne( } auto* entry = new (reinterpret_cast(next_slot)) PartitionFreelistEntry(); +#endif if (!slot_span->get_freelist_head()) { PA_DCHECK(!prev_entry); PA_DCHECK(!free_list_entries_added); @@ -806,8 +838,10 @@ PartitionBucket::ProvisionMoreSlotsAndAllocOne( PA_DCHECK(free_list_entries_added); prev_entry->SetNext(entry); } +#if !defined(OHOS_ENABLE_RANDOM) next_slot = next_slot_end; next_slot_end = next_slot + size; +#endif prev_entry = entry; #if DCHECK_IS_ON() free_list_entries_added++; -- Gitee