Skip to content

Commit

Permalink
[ffi] Use @Natives for process lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
dcharkes committed Aug 8, 2024
1 parent 2464aaa commit 96b990a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
4 changes: 3 additions & 1 deletion pkgs/ffi/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
## 2.1.3-wip
## 2.1.3

- Use `package:dart_flutter_team_lints`.
- Migrate from `DynamicLibrary.process()` to `@Native external` functions.
https://github.com/dart-lang/native/issues/1401

## 2.1.2

Expand Down
32 changes: 16 additions & 16 deletions pkgs/ffi/lib/src/allocation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@
import 'dart:ffi';
import 'dart:io';

// Note that ole32.dll is the correct name in both 32-bit and 64-bit.
final DynamicLibrary stdlib = Platform.isWindows
? DynamicLibrary.open('ole32.dll')
: DynamicLibrary.process();

typedef PosixMallocNative = Pointer Function(IntPtr);
typedef PosixMalloc = Pointer Function(int);
final PosixMalloc posixMalloc =
stdlib.lookupFunction<PosixMallocNative, PosixMalloc>('malloc');

@Native<PosixMallocNative>(symbol: 'malloc')
external Pointer<Void> posixMalloc(int size);

typedef PosixCallocNative = Pointer Function(IntPtr num, IntPtr size);
typedef PosixCalloc = Pointer Function(int num, int size);
final PosixCalloc posixCalloc =
stdlib.lookupFunction<PosixCallocNative, PosixCalloc>('calloc');

@Native<PosixCallocNative>(symbol: 'malloc')
external Pointer<Void> posixCalloc(int num, int size);

typedef PosixFreeNative = Void Function(Pointer);
typedef PosixFree = void Function(Pointer);

@Native<Void Function(Pointer)>(symbol: 'free')
external void posixFree(Pointer ptr);

final Pointer<NativeFunction<PosixFreeNative>> posixFreePointer =
stdlib.lookup('free');
final PosixFree posixFree = posixFreePointer.asFunction();
Native.addressOf(posixFree);

// Note that ole32.dll is the correct name in both 32-bit and 64-bit.
final DynamicLibrary ole32lib = DynamicLibrary.open('ole32.dll');

typedef WinCoTaskMemAllocNative = Pointer Function(Size);
typedef WinCoTaskMemAlloc = Pointer Function(int);
final WinCoTaskMemAlloc winCoTaskMemAlloc =
stdlib.lookupFunction<WinCoTaskMemAllocNative, WinCoTaskMemAlloc>(
ole32lib.lookupFunction<WinCoTaskMemAllocNative, WinCoTaskMemAlloc>(
'CoTaskMemAlloc');

typedef WinCoTaskMemFreeNative = Void Function(Pointer);
typedef WinCoTaskMemFree = void Function(Pointer);
final Pointer<NativeFunction<WinCoTaskMemFreeNative>> winCoTaskMemFreePointer =
stdlib.lookup('CoTaskMemFree');
ole32lib.lookup('CoTaskMemFree');
final WinCoTaskMemFree winCoTaskMemFree = winCoTaskMemFreePointer.asFunction();

/// Manages memory on the native heap.
Expand Down
4 changes: 2 additions & 2 deletions pkgs/ffi/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ffi
version: 2.1.3-wip
version: 2.1.3
description: Utilities for working with Foreign Function Interface (FFI) code.
repository: https://github.com/dart-lang/native/tree/main/pkgs/ffi

Expand All @@ -9,7 +9,7 @@ topics:
- codegen

environment:
sdk: '>=3.3.0-279.1.beta <4.0.0'
sdk: '>=3.3.0 <4.0.0'

dev_dependencies:
dart_flutter_team_lints: ^2.0.0
Expand Down

0 comments on commit 96b990a

Please sign in to comment.