dart-sdk/runtime/vm/finalizable_data.h
Daco Harkes 5278383736 [vm] Native API: Make Dart_NewWeakPersistentHandle not auto delete
Changes Dart_NewWeakPersistentHandle to no longer auto delete the
weak persistent handle.

Changes the signatures of WeakPersistentHandleFinalizers to no longer
have access to the handle.

Flutter PR: https://github.com/flutter/engine/pull/19843
g3 presubmit: cl/318028238

Issue: https://github.com/dart-lang/sdk/issues/42312

TEST=runtime/vm/dart_api_impl_test.cc

Change-Id: I3f77db9954d9486759f903b78c03a494f73c68ba
Cq-Include-Trybots:dart/try:vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64-try,app-kernel-linux-debug-x64-try,vm-kernel-linux-debug-ia32-try,vm-kernel-win-debug-x64-try,vm-kernel-win-debug-ia32-try,vm-kernel-precomp-linux-debug-x64-try,vm-kernel-precomp-android-release-arm64-try,vm-kernel-asan-linux-release-x64-try,vm-kernel-msan-linux-release-x64-try,vm-kernel-precomp-msan-linux-release-x64-try,vm-kernel-linux-release-simarm-try,vm-kernel-linux-release-simarm64-try,vm-kernel-precomp-android-release-arm_x64-try,vm-kernel-precomp-obfuscate-linux-release-x64-try,dart-sdk-linux-try,analyzer-analysis-server-linux-try,analyzer-linux-release-try,front-end-linux-release-x64-try,vm-kernel-precomp-win-release-x64-try,vm-kernel-mac-debug-x64-try,vm-precomp-ffi-qemu-linux-release-arm-try,vm-kernel-nnbd-linux-debug-x64-try,analyzer-nnbd-linux-release-try,front-end-nnbd-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/151525
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2020-11-03 10:27:44 +00:00

91 lines
2.6 KiB
C++

// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_FINALIZABLE_DATA_H_
#define RUNTIME_VM_FINALIZABLE_DATA_H_
#include "include/dart_api.h"
#include "platform/growable_array.h"
#include "vm/globals.h"
namespace dart {
struct FinalizableData {
void* data;
void* peer;
Dart_HandleFinalizer callback;
Dart_HandleFinalizer successful_write_callback;
};
class MessageFinalizableData {
public:
MessageFinalizableData()
: records_(0), get_position_(0), take_position_(0), external_size_(0) {}
~MessageFinalizableData() {
for (intptr_t i = take_position_; i < records_.length(); i++) {
records_[i].callback(nullptr, records_[i].peer);
}
}
/// If [successful_write_callback] is provided, it's invoked when message
/// was serialized successfully.
/// [callback] is invoked when serialization failed.
void Put(intptr_t external_size,
void* data,
void* peer,
Dart_HandleFinalizer callback,
Dart_HandleFinalizer successful_write_callback = nullptr) {
FinalizableData finalizable_data;
finalizable_data.data = data;
finalizable_data.peer = peer;
finalizable_data.callback = callback;
finalizable_data.successful_write_callback = successful_write_callback;
records_.Add(finalizable_data);
external_size_ += external_size;
}
// Retrieve the next FinalizableData, but still run its finalizer when |this|
// is destroyed.
FinalizableData Get() {
ASSERT(get_position_ < records_.length());
return records_[get_position_++];
}
// Retrieve the next FinalizableData, and skip its finalizer when |this| is
// destroyed.
FinalizableData Take() {
ASSERT(take_position_ < records_.length());
return records_[take_position_++];
}
void SerializationSucceeded() {
for (intptr_t i = 0; i < records_.length(); i++) {
if (records_[i].successful_write_callback != nullptr) {
records_[i].successful_write_callback(nullptr, records_[i].peer);
}
}
}
void DropFinalizers() {
records_.Clear();
get_position_ = 0;
take_position_ = 0;
external_size_ = 0;
}
intptr_t external_size() const { return external_size_; }
private:
MallocGrowableArray<FinalizableData> records_;
intptr_t get_position_;
intptr_t take_position_;
intptr_t external_size_;
DISALLOW_COPY_AND_ASSIGN(MessageFinalizableData);
};
} // namespace dart
#endif // RUNTIME_VM_FINALIZABLE_DATA_H_