19
19
#include "include/v8-date.h"
20
20
#include "include/v8-embedder-state-scope.h"
21
21
#include "include/v8-extension.h"
22
+ #include "include/v8-external-memory-accounter.h"
22
23
#include "include/v8-fast-api-calls.h"
23
24
#include "include/v8-function.h"
24
25
#include "include/v8-json.h"
@@ -9728,6 +9729,29 @@ void BigInt::ToWordsArray(int* sign_bit, int* word_count,
9728
9729
*word_count = base::checked_cast<int>(unsigned_word_count);
9729
9730
}
9730
9731
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
+
9731
9755
void Isolate::HandleExternalMemoryInterrupt() {
9732
9756
i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap();
9733
9757
if (heap->gc_state() != i::Heap::NOT_IN_GC) return;
@@ -10437,25 +10461,7 @@ void Isolate::GetStackSample(const RegisterState& state, void** frames,
10437
10461
10438
10462
int64_t Isolate::AdjustAmountOfExternalAllocatedMemory(
10439
10463
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);
10459
10465
}
10460
10466
10461
10467
void Isolate::SetEventLogger(LogEventCallback that) {
@@ -12336,24 +12342,26 @@ bool V8_EXPORT ValidateCallbackInfo(const PropertyCallbackInfo<void>& info) {
12336
12342
return ValidatePropertyCallbackInfo(info);
12337
12343
}
12338
12344
12339
- ExternalMemoryAccounterBase::~ExternalMemoryAccounterBase() {
12340
- #ifdef DEBUG
12345
+ } // namespace internal
12346
+
12347
+ ExternalMemoryAccounter::~ExternalMemoryAccounter() {
12348
+ #ifdef V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
12341
12349
DCHECK_EQ(amount_of_external_memory_, 0U);
12342
12350
#endif
12343
12351
}
12344
12352
12345
- ExternalMemoryAccounterBase::ExternalMemoryAccounterBase (
12346
- ExternalMemoryAccounterBase && other) V8_NOEXCEPT {
12347
- #if DEBUG
12353
+ ExternalMemoryAccounter::ExternalMemoryAccounter (
12354
+ ExternalMemoryAccounter && other) {
12355
+ #if V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
12348
12356
amount_of_external_memory_ =
12349
12357
std::exchange(other.amount_of_external_memory_, 0U);
12350
12358
isolate_ = std::exchange(other.isolate_, nullptr);
12351
12359
#endif
12352
12360
}
12353
12361
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
12357
12365
if (this == &other) {
12358
12366
return *this;
12359
12367
}
@@ -12365,33 +12373,32 @@ ExternalMemoryAccounterBase& ExternalMemoryAccounterBase::operator=(
12365
12373
return *this;
12366
12374
}
12367
12375
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
12370
12378
DCHECK(isolate == isolate_ || isolate_ == nullptr);
12371
12379
isolate_ = isolate;
12372
12380
amount_of_external_memory_ += size;
12373
12381
#endif
12374
- reinterpret_cast<v8::Isolate*>(isolate)
12375
- ->AdjustAmountOfExternalAllocatedMemory( static_cast<int64_t>(size));
12382
+ isolate->AdjustAmountOfExternalAllocatedMemoryImpl(
12383
+ static_cast<int64_t>(size));
12376
12384
}
12377
12385
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
12380
12388
DCHECK(isolate == isolate_ || isolate_ == nullptr);
12381
12389
DCHECK_GE(static_cast<int64_t>(amount_of_external_memory_), -delta);
12382
12390
isolate_ = isolate;
12383
12391
amount_of_external_memory_ += delta;
12384
12392
#endif
12385
- reinterpret_cast<v8::Isolate*>(isolate)
12386
- ->AdjustAmountOfExternalAllocatedMemory(delta);
12393
+ isolate->AdjustAmountOfExternalAllocatedMemoryImpl(delta);
12387
12394
}
12388
12395
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;
12391
12398
if (size == 0) {
12392
12399
return;
12393
12400
}
12394
- #ifdef DEBUG
12401
+ #ifdef V8_ENABLE_MEMORY_ACCOUNTING_CHECKS
12395
12402
DCHECK_EQ(isolate, isolate_);
12396
12403
DCHECK_GE(amount_of_external_memory_, size);
12397
12404
amount_of_external_memory_ -= size;
@@ -12400,7 +12407,12 @@ void ExternalMemoryAccounterBase::Decrease(Isolate* isolate, size_t size) {
12400
12407
i_isolate->heap()->UpdateExternalMemory(-static_cast<int64_t>(size));
12401
12408
}
12402
12409
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
+ }
12404
12416
12405
12417
template <>
12406
12418
bool V8_EXPORT V8_WARN_UNUSED_RESULT
0 commit comments