This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[fuchsia] [ffi] Basic support for Channels and Handles #27849
Merged
fluttergithubbot
merged 4 commits into
flutter:master
from
iskakaushik:dart_zircon_channel_support
Aug 16, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
shell/platform/fuchsia/dart-pkg/zircon/lib/src/zd_channel.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| part of zircon; | ||
|
|
||
| @pragma('vm:entry-point') | ||
| class ZDChannel { | ||
| static ZDChannel? create([int options = 0]) { | ||
| final Pointer<zircon_dart_handle_pair_t>? channelPtr = | ||
| zirconFFIBindings?.zircon_dart_channel_create(options); | ||
| if (channelPtr == null || channelPtr.address == 0) { | ||
| throw Exception('Unable to create a channel'); | ||
| } | ||
| return ZDChannel._(ZDHandlePair._(channelPtr)); | ||
| } | ||
|
|
||
| static int _write(ZDHandle channel, ByteData data, List<ZDHandle> handles) { | ||
| final Pointer<zircon_dart_handle_list_t> handleList = | ||
| zirconFFIBindings!.zircon_dart_handle_list_create(); | ||
| handles.forEach((ZDHandle handle) { | ||
| zirconFFIBindings! | ||
| .zircon_dart_handle_list_append(handleList, handle._ptr); | ||
| }); | ||
|
|
||
| final Uint8List dataAsBytes = data.buffer.asUint8List(); | ||
| final Pointer<zircon_dart_byte_array_t> byteArray = | ||
| zirconFFIBindings!.zircon_dart_byte_array_create(dataAsBytes.length); | ||
| for (int i = 0; i < dataAsBytes.length; i++) { | ||
| zirconFFIBindings!.zircon_dart_byte_array_set_value( | ||
| byteArray, i, dataAsBytes.elementAt(i)); | ||
| } | ||
| int ret = zirconFFIBindings! | ||
| .zircon_dart_channel_write(channel._ptr, byteArray, handleList); | ||
|
|
||
| zirconFFIBindings!.zircon_dart_byte_array_free(byteArray); | ||
| zirconFFIBindings!.zircon_dart_handle_list_free(handleList); | ||
| return ret; | ||
| } | ||
|
|
||
| int writeLeft(ByteData data, List<ZDHandle> handles) { | ||
| return _write(handlePair.left, data, handles); | ||
| } | ||
|
|
||
| int writeRight(ByteData data, List<ZDHandle> handles) { | ||
| return _write(handlePair.right, data, handles); | ||
| } | ||
|
|
||
| @pragma('vm:entry-point') | ||
| ZDChannel._(this.handlePair); | ||
|
|
||
| final ZDHandlePair handlePair; | ||
|
|
||
| @override | ||
| String toString() => 'Channel(handlePair=$handlePair)'; | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
shell/platform/fuchsia/dart-pkg/zircon/lib/src/zd_handle.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| part of zircon; | ||
|
|
||
| @pragma('vm:entry-point') | ||
| class ZDHandle { | ||
| @pragma('vm:entry-point') | ||
| ZDHandle._(this._ptr) { | ||
| _attachFinalizer(); | ||
| } | ||
|
|
||
| void _attachFinalizer() { | ||
| // TODO (kaushikiska): fix external allocation size. | ||
| final int? ret = zirconFFIBindings?.zircon_dart_handle_attach_finalizer( | ||
| this, _ptr.cast(), 128); | ||
| if (ret != 1) { | ||
| throw Exception('Unable to attach finalizer to handle'); | ||
| } | ||
| } | ||
|
|
||
| int get handle => _ptr.ref.handle; | ||
|
|
||
| final Pointer<zircon_dart_handle_t> _ptr; | ||
|
|
||
| bool isValid() { | ||
| int? ret = zirconFFIBindings?.zircon_dart_handle_is_valid(_ptr); | ||
| return ret == 1; | ||
| } | ||
|
|
||
| bool close() { | ||
| assert(isValid()); | ||
| if (isValid()) { | ||
| int? ret = zirconFFIBindings?.zircon_dart_handle_close(_ptr); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's assert(isValid()) here, then do the if check. 99% of the time, isValid() being false here indicates a coding error in the dart application This is the behavior that the existing implementation has (DCHECK on handle validity, then proceed forward with an if) |
||
| return ret == 1; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @override | ||
| bool operator ==(Object other) { | ||
| return other is ZDHandle && other.handle == handle; | ||
| } | ||
|
|
||
| @override | ||
| int get hashCode => handle.hashCode; | ||
|
|
||
| @override | ||
| String toString() => 'ZDHandle(handle=$handle)'; | ||
| } | ||
|
|
||
| @pragma('vm:entry-point') | ||
| class ZDHandlePair { | ||
| @pragma('vm:entry-point') | ||
| ZDHandlePair._(this._ptr) | ||
| : left = ZDHandle._(_ptr.ref.left), | ||
| right = ZDHandle._(_ptr.ref.right) { | ||
| _attachFinalizer(); | ||
| } | ||
|
|
||
| void _attachFinalizer() { | ||
| // TODO (kaushikiska): fix external allocation size. | ||
| final int? ret = zirconFFIBindings | ||
| ?.zircon_dart_handle_pair_attach_finalizer(this, _ptr.cast(), 128); | ||
| if (ret != 1) { | ||
| throw Exception('Unable to attach finalizer to handle'); | ||
| } | ||
| } | ||
|
|
||
| final Pointer<zircon_dart_handle_pair_t> _ptr; | ||
| final ZDHandle left; | ||
| final ZDHandle right; | ||
|
|
||
| @override | ||
| String toString() => 'ZDHandlePair(left=$left, right=$right)'; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "basic_types.h" | ||
|
|
||
| #include <cstdint> | ||
| #include <cstdlib> | ||
|
|
||
| #include "flutter/fml/logging.h" | ||
|
|
||
| zircon_dart_byte_array_t* zircon_dart_byte_array_create(uint32_t size) { | ||
| zircon_dart_byte_array_t* arr = static_cast<zircon_dart_byte_array_t*>( | ||
| malloc(sizeof(zircon_dart_byte_array_t))); | ||
| arr->length = size; | ||
| arr->data = static_cast<uint8_t*>(malloc(size * sizeof(uint8_t))); | ||
| return arr; | ||
| } | ||
|
|
||
| void zircon_dart_byte_array_set_value(zircon_dart_byte_array_t* arr, | ||
| uint32_t index, | ||
| uint8_t value) { | ||
| FML_CHECK(arr); | ||
| FML_CHECK(arr->length > index); | ||
| arr->data[index] = value; | ||
| } | ||
|
|
||
| void zircon_dart_byte_array_free(zircon_dart_byte_array_t* arr) { | ||
| FML_CHECK(arr); | ||
| free(arr->data); | ||
| free(arr); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_FFI_BASIC_TYPES_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_FFI_BASIC_TYPES_H_ | ||
|
|
||
| #include "macros.h" | ||
|
|
||
| #include <stdint.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| typedef struct zircon_dart_byte_array_t { | ||
| uint8_t* data; | ||
| uint32_t length; | ||
| } zircon_dart_byte_array_t; | ||
|
|
||
| ZIRCON_FFI_EXPORT zircon_dart_byte_array_t* zircon_dart_byte_array_create( | ||
| uint32_t size); | ||
|
|
||
| ZIRCON_FFI_EXPORT void zircon_dart_byte_array_set_value( | ||
| zircon_dart_byte_array_t* arr, | ||
| uint32_t index, | ||
| uint8_t value); | ||
|
|
||
| ZIRCON_FFI_EXPORT void zircon_dart_byte_array_free( | ||
| zircon_dart_byte_array_t* arr); | ||
|
|
||
| #ifdef __cplusplus | ||
| } // extern "C" | ||
| #endif | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_FUCHSIA_DART_PKG_ZIRCON_FFI_BASIC_TYPES_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why keep both ends of the channel-pair inside of one object? Conceptually, a channel represents only one half of the pair, and very often dart code will only possess one half with no ability to refer to the other half
The old implementation has a shim object called "ChannelPair" to handle this