-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[vm/ffi] Add Pointer<Struct>.value =
for copying full structs
#44768
Comments
Pointer<Struct>.ref =
for copying full structsPointer<Struct>.value =
for copying full structs
@lrhn what do you think about |
@dcharkes For reference: I cloned the cpp client library: https://github.com/ros2/rclcpp The c library has functions that start with rcl and rmw, and the struct values are assigned to pointer dereferences which is detected by the regex. There is one false positive in the grep I think, but I might be missing a few when the rcl or rmw gets wrapped onto the next line. |
@dcharkes I found a similar example to @TimWhiting 's use case. This one comes from the AWS C core library that, for me, is used by the AWS encryption SDK (partial, but sufficient and workable, implementation coming to pub.dev shortly). From its hash table implementation: https://github.com/awslabs/aws-c-common/blob/main/include/aws/common/hash_table.h The function to I could wrap this in my own function, but the SDK already builds dynamic libraries for Windows, Linux and MacOS which I've been able to use directly (up to this point) by simply struct aws_hash_iter {
const struct aws_hash_table *map;
struct aws_hash_element element;
size_t slot;
size_t limit;
enum aws_hash_iter_status status;
// some lines deleted
};
/**
* Returns an iterator to be used for iterating through a hash table.
* Iterator will already point to the first element of the table it finds,
* which can be accessed as iter.element.
*
* This function cannot fail, but if there are no elements in the table,
* the returned iterator will return true for aws_hash_iter_done(&iter).
*/
AWS_COMMON_API
struct aws_hash_iter aws_hash_iter_begin(const struct aws_hash_table *map);
/**
* Returns true if iterator is done iterating through table, false otherwise.
* If this is true, the iterator will not include an element of the table.
*/
AWS_COMMON_API
bool aws_hash_iter_done(const struct aws_hash_iter *iter);
/**
* Updates iterator so that it points to next element of hash table.
*
* This and the two previous functions are designed to be used together with
* the following idiom:
*
* for (struct aws_hash_iter iter = aws_hash_iter_begin(&map);
* !aws_hash_iter_done(&iter); aws_hash_iter_next(&iter)) {
* const key_type key = *(const key_type *)iter.element.key;
* value_type value = *(value_type *)iter.element.value;
* // etc.
* }
*
* Note that calling this on an iter which is "done" is idempotent:
* i.e. it will return another iter which is "done".
*/
AWS_COMMON_API
void aws_hash_iter_next(struct aws_hash_iter *iter); |
We do not have a way of bulk-copying the full contents of a
Struct
indart:ffi
.One use case where this is creating the need for boilerplate is when getting passed a struct by value from C, and having to pass back a pointer to a struct. @TimWhiting can you quantify how common this pattern is? #36730 (comment)
A workaround currently is to define a struct that nests the struct by value.
This accesses the
TypedData
and/orPointer
objects wrapped by theStruct
s and usessizeOf<NativeType>()
and_memCopy
to copy the bytes:sdk/sdk/lib/_internal/vm/lib/ffi_patch.dart
Lines 127 to 162 in ba01596
We should enable using this
_memCopy
for fullStruct
s instead of only fields.sdk/sdk/lib/ffi/ffi.dart
Lines 540 to 551 in ba01596
For the specific use case the code would look like:
The text was updated successfully, but these errors were encountered: