1919#include " include/v8-date.h"
2020#include " include/v8-embedder-state-scope.h"
2121#include " include/v8-extension.h"
22+ #include " include/v8-external-memory-accounter.h"
2223#include " include/v8-fast-api-calls.h"
2324#include " include/v8-function.h"
2425#include " include/v8-json.h"
@@ -9728,6 +9729,29 @@ void BigInt::ToWordsArray(int* sign_bit, int* word_count,
97289729 *word_count = base::checked_cast<int >(unsigned_word_count);
97299730}
97309731
9732+ int64_t Isolate::AdjustAmountOfExternalAllocatedMemoryImpl (
9733+ int64_t change_in_bytes) {
9734+ // Try to check for unreasonably large or small values from the embedder.
9735+ static constexpr int64_t kMaxReasonableBytes = int64_t (1 ) << 60 ;
9736+ static constexpr int64_t kMinReasonableBytes = -kMaxReasonableBytes ;
9737+ static_assert (kMaxReasonableBytes >= i::JSArrayBuffer::kMaxByteLength );
9738+ CHECK (kMinReasonableBytes <= change_in_bytes &&
9739+ change_in_bytes < kMaxReasonableBytes );
9740+
9741+ i::Isolate* i_isolate = reinterpret_cast <i::Isolate*>(this );
9742+ const uint64_t amount =
9743+ i_isolate->heap ()->UpdateExternalMemory (change_in_bytes);
9744+
9745+ if (change_in_bytes <= 0 ) {
9746+ return amount;
9747+ }
9748+
9749+ if (amount > i_isolate->heap ()->external_memory_limit_for_interrupt ()) {
9750+ HandleExternalMemoryInterrupt ();
9751+ }
9752+ return amount;
9753+ }
9754+
97319755void Isolate::HandleExternalMemoryInterrupt () {
97329756 i::Heap* heap = reinterpret_cast <i::Isolate*>(this )->heap ();
97339757 if (heap->gc_state () != i::Heap::NOT_IN_GC) return ;
@@ -10437,25 +10461,7 @@ void Isolate::GetStackSample(const RegisterState& state, void** frames,
1043710461
1043810462int64_t Isolate::AdjustAmountOfExternalAllocatedMemory (
1043910463 int64_t change_in_bytes) {
10440- // Try to check for unreasonably large or small values from the embedder.
10441- static constexpr int64_t kMaxReasonableBytes = int64_t (1 ) << 60 ;
10442- static constexpr int64_t kMinReasonableBytes = -kMaxReasonableBytes ;
10443- static_assert (kMaxReasonableBytes >= i::JSArrayBuffer::kMaxByteLength );
10444- CHECK (kMinReasonableBytes <= change_in_bytes &&
10445- change_in_bytes < kMaxReasonableBytes );
10446-
10447- i::Isolate* i_isolate = reinterpret_cast <i::Isolate*>(this );
10448- const uint64_t amount =
10449- i_isolate->heap ()->UpdateExternalMemory (change_in_bytes);
10450-
10451- if (change_in_bytes <= 0 ) {
10452- return amount;
10453- }
10454-
10455- if (amount > i_isolate->heap ()->external_memory_limit_for_interrupt ()) {
10456- HandleExternalMemoryInterrupt ();
10457- }
10458- return amount;
10464+ return AdjustAmountOfExternalAllocatedMemoryImpl (change_in_bytes);
1045910465}
1046010466
1046110467void Isolate::SetEventLogger (LogEventCallback that) {
@@ -12336,24 +12342,26 @@ bool V8_EXPORT ValidateCallbackInfo(const PropertyCallbackInfo<void>& info) {
1233612342 return ValidatePropertyCallbackInfo (info);
1233712343}
1233812344
12339- ExternalMemoryAccounterBase::~ExternalMemoryAccounterBase () {
12340- #ifdef DEBUG
12345+ } // namespace internal
12346+
12347+ ExternalMemoryAccounter::~ExternalMemoryAccounter () {
12348+ #ifdef V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
1234112349 DCHECK_EQ (amount_of_external_memory_, 0U );
1234212350#endif
1234312351}
1234412352
12345- ExternalMemoryAccounterBase::ExternalMemoryAccounterBase (
12346- ExternalMemoryAccounterBase && other) V8_NOEXCEPT {
12347- #if DEBUG
12353+ ExternalMemoryAccounter::ExternalMemoryAccounter (
12354+ ExternalMemoryAccounter && other) {
12355+ #if V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
1234812356 amount_of_external_memory_ =
1234912357 std::exchange (other.amount_of_external_memory_ , 0U );
1235012358 isolate_ = std::exchange (other.isolate_ , nullptr );
1235112359#endif
1235212360}
1235312361
12354- ExternalMemoryAccounterBase& ExternalMemoryAccounterBase ::operator =(
12355- ExternalMemoryAccounterBase && other) V8_NOEXCEPT {
12356- #if DEBUG
12362+ ExternalMemoryAccounter& ExternalMemoryAccounter ::operator =(
12363+ ExternalMemoryAccounter && other) {
12364+ #if V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
1235712365 if (this == &other) {
1235812366 return *this ;
1235912367 }
@@ -12365,33 +12373,32 @@ ExternalMemoryAccounterBase& ExternalMemoryAccounterBase::operator=(
1236512373 return *this ;
1236612374}
1236712375
12368- void ExternalMemoryAccounterBase ::Increase (Isolate* isolate, size_t size) {
12369- #ifdef DEBUG
12376+ void ExternalMemoryAccounter ::Increase (Isolate* isolate, size_t size) {
12377+ #ifdef V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
1237012378 DCHECK (isolate == isolate_ || isolate_ == nullptr );
1237112379 isolate_ = isolate;
1237212380 amount_of_external_memory_ += size;
1237312381#endif
12374- reinterpret_cast <v8::Isolate*>(isolate)
12375- -> AdjustAmountOfExternalAllocatedMemory ( static_cast <int64_t >(size));
12382+ isolate-> AdjustAmountOfExternalAllocatedMemoryImpl (
12383+ static_cast <int64_t >(size));
1237612384}
1237712385
12378- void ExternalMemoryAccounterBase ::Update (Isolate* isolate, int64_t delta) {
12379- #ifdef DEBUG
12386+ void ExternalMemoryAccounter ::Update (Isolate* isolate, int64_t delta) {
12387+ #ifdef V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
1238012388 DCHECK (isolate == isolate_ || isolate_ == nullptr );
1238112389 DCHECK_GE (static_cast <int64_t >(amount_of_external_memory_), -delta);
1238212390 isolate_ = isolate;
1238312391 amount_of_external_memory_ += delta;
1238412392#endif
12385- reinterpret_cast <v8::Isolate*>(isolate)
12386- ->AdjustAmountOfExternalAllocatedMemory (delta);
12393+ isolate->AdjustAmountOfExternalAllocatedMemoryImpl (delta);
1238712394}
1238812395
12389- void ExternalMemoryAccounterBase ::Decrease (Isolate* isolate, size_t size) {
12390- DisallowGarbageCollection no_gc;
12396+ void ExternalMemoryAccounter ::Decrease (Isolate* isolate, size_t size) {
12397+ internal:: DisallowGarbageCollection no_gc;
1239112398 if (size == 0 ) {
1239212399 return ;
1239312400 }
12394- #ifdef DEBUG
12401+ #ifdef V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
1239512402 DCHECK_EQ (isolate, isolate_);
1239612403 DCHECK_GE (amount_of_external_memory_, size);
1239712404 amount_of_external_memory_ -= size;
@@ -12400,7 +12407,12 @@ void ExternalMemoryAccounterBase::Decrease(Isolate* isolate, size_t size) {
1240012407 i_isolate->heap ()->UpdateExternalMemory (-static_cast <int64_t >(size));
1240112408}
1240212409
12403- } // namespace internal
12410+ int64_t
12411+ ExternalMemoryAccounter::GetTotalAmountOfExternalAllocatedMemoryForTesting (
12412+ const Isolate* isolate) {
12413+ const i::Isolate* i_isolate = reinterpret_cast <const i::Isolate*>(isolate);
12414+ return i_isolate->heap ()->external_memory ();
12415+ }
1240412416
1240512417template <>
1240612418bool V8_EXPORT V8_WARN_UNUSED_RESULT
0 commit comments