Skip to content

Commit e262e1c

Browse files
Anton BikineevV8 LUCI CQ
authored andcommitted
[zone] Provide a way to configure allocator for zone backings
The CL provides a way for the embedder to hook in a special malloc-like allocator that will be used for zone allocations. An alternative approach would be to use weak functions with branches, checking whether the functions were available at link-time. Those branches could be optimized away with LTOs, so they would essentially be free. However, the weak function approach is not portable (e.g. there is no easy way to emulate it with msvc). The approach can be revisited if indirect call turns out to be expensive (e.g. on hardware with weak branch target predictors). The CL is a prerequisite for running PCScan in the renderer process. Bug: chromium:1249550 Change-Id: I221dcb2486c13e8e6e6761839ba391978319bde4 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3172760 Reviewed-by: Igor Sheludko <[email protected]> Reviewed-by: Michael Lippautz <[email protected]> Commit-Queue: Anton Bikineev <[email protected]> Cr-Commit-Position: refs/heads/main@{#77012}
1 parent 74ca05b commit e262e1c

5 files changed

Lines changed: 36 additions & 6 deletions

File tree

include/v8-platform.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,18 @@ class PageAllocator {
516516
virtual bool CanAllocateSharedPages() { return false; }
517517
};
518518

519+
/**
520+
* V8 Allocator used for allocating zone backings.
521+
*/
522+
class ZoneBackingAllocator {
523+
public:
524+
using MallocFn = void* (*)(size_t);
525+
using FreeFn = void (*)(void*);
526+
527+
virtual MallocFn GetMallocFn() const { return ::malloc; }
528+
virtual FreeFn GetFreeFn() const { return ::free; }
529+
};
530+
519531
/**
520532
* V8 Platform abstraction layer.
521533
*
@@ -534,6 +546,14 @@ class Platform {
534546
return nullptr;
535547
}
536548

549+
/**
550+
* Allows the embedder to specify a custom allocator used for zones.
551+
*/
552+
virtual ZoneBackingAllocator* GetZoneBackingAllocator() {
553+
static ZoneBackingAllocator default_allocator;
554+
return &default_allocator;
555+
}
556+
537557
/**
538558
* Enables the embedder to respond in cases where V8 can't allocate large
539559
* blocks of memory. V8 retries the failed allocation once after calling this

src/utils/allocation.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,10 @@ char* StrNDup(const char* str, size_t n) {
141141
return result;
142142
}
143143

144-
void* AllocWithRetry(size_t size) {
144+
void* AllocWithRetry(size_t size, MallocFn malloc_fn) {
145145
void* result = nullptr;
146146
for (int i = 0; i < kAllocationTries; ++i) {
147-
result = base::Malloc(size);
147+
result = malloc_fn(size);
148148
if (result != nullptr) break;
149149
if (!OnCriticalMemoryPressure(size)) break;
150150
}

src/utils/allocation.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,11 @@ class FreeStoreAllocationPolicy {
9090
}
9191
};
9292

93+
using MallocFn = void* (*)(size_t);
94+
9395
// Performs a malloc, with retry logic on failure. Returns nullptr on failure.
9496
// Call free to release memory allocated with this function.
95-
void* AllocWithRetry(size_t size);
97+
void* AllocWithRetry(size_t size, MallocFn = base::Malloc);
9698

9799
V8_EXPORT_PRIVATE void* AlignedAlloc(size_t size, size_t alignment);
98100
V8_EXPORT_PRIVATE void AlignedFree(void* ptr);

src/zone/accounting-allocator.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ std::unique_ptr<v8::base::BoundedPageAllocator> CreateBoundedAllocator(
6565

6666
} // namespace
6767

68-
AccountingAllocator::AccountingAllocator() {
68+
AccountingAllocator::AccountingAllocator()
69+
: zone_backing_malloc_(
70+
V8::GetCurrentPlatform()->GetZoneBackingAllocator()->GetMallocFn()),
71+
zone_backing_free_(
72+
V8::GetCurrentPlatform()->GetZoneBackingAllocator()->GetFreeFn()) {
6973
if (COMPRESS_ZONES_BOOL) {
7074
v8::PageAllocator* platform_page_allocator = GetPlatformPageAllocator();
7175
VirtualMemory memory = ReserveAddressSpace(platform_page_allocator);
@@ -86,7 +90,7 @@ Segment* AccountingAllocator::AllocateSegment(size_t bytes,
8690
kZonePageSize, PageAllocator::kReadWrite);
8791

8892
} else {
89-
memory = AllocWithRetry(bytes);
93+
memory = AllocWithRetry(bytes, zone_backing_malloc_);
9094
}
9195
if (memory == nullptr) return nullptr;
9296

@@ -110,7 +114,7 @@ void AccountingAllocator::ReturnSegment(Segment* segment,
110114
if (COMPRESS_ZONES_BOOL && supports_compression) {
111115
CHECK(FreePages(bounded_page_allocator_.get(), segment, segment_size));
112116
} else {
113-
base::Free(segment);
117+
zone_backing_free_(segment);
114118
}
115119
}
116120

src/zone/accounting-allocator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <atomic>
99
#include <memory>
1010

11+
#include "include/v8-platform.h"
1112
#include "src/base/macros.h"
1213
#include "src/logging/tracing-flags.h"
1314

@@ -71,6 +72,9 @@ class V8_EXPORT_PRIVATE AccountingAllocator {
7172

7273
std::unique_ptr<VirtualMemory> reserved_area_;
7374
std::unique_ptr<base::BoundedPageAllocator> bounded_page_allocator_;
75+
76+
ZoneBackingAllocator::MallocFn zone_backing_malloc_ = nullptr;
77+
ZoneBackingAllocator::FreeFn zone_backing_free_ = nullptr;
7478
};
7579

7680
} // namespace internal

0 commit comments

Comments
 (0)