forked from Nexus/nexus
tweak memory management
This commit is contained in:
parent
e899bb3929
commit
ea29889e5c
2 changed files with 26 additions and 32 deletions
|
|
@ -79,34 +79,26 @@ class ClientController(final String? accountId) extends AsyncNotifier<int> {
|
||||||
final environment = await _environment(account);
|
final environment = await _environment(account);
|
||||||
|
|
||||||
debugPrint("Setting Gomuks env for $account...");
|
debugPrint("Setting Gomuks env for $account...");
|
||||||
|
using((arena) {
|
||||||
for (final MapEntry(:key, :value) in environment.entries) {
|
for (final MapEntry(:key, :value) in environment.entries) {
|
||||||
final keyPtr = key.toNativeUtf8().cast<Char>();
|
GomuksSetEnv(
|
||||||
final valuePtr = value.toNativeUtf8().cast<Char>();
|
key.toNativeUtf8(allocator: arena).cast(),
|
||||||
try {
|
value.toNativeUtf8(allocator: arena).cast(),
|
||||||
GomuksSetEnv(keyPtr, valuePtr);
|
);
|
||||||
} finally {
|
|
||||||
calloc
|
|
||||||
..free(keyPtr)
|
|
||||||
..free(valuePtr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}, malloc);
|
||||||
|
|
||||||
debugPrint("Initializing Gomuks...");
|
debugPrint("Initializing Gomuks...");
|
||||||
final bufferPointer = GomuksConfig(
|
final config = GomuksConfig(
|
||||||
matrix: .new(
|
matrix: .new(
|
||||||
initialDeviceDisplayName:
|
initialDeviceDisplayName:
|
||||||
"Nexus on ${toBeginningOfSentenceCase(Platform.operatingSystem)}",
|
"Nexus on ${toBeginningOfSentenceCase(Platform.operatingSystem)}",
|
||||||
),
|
),
|
||||||
).toJson().toGomuksBufferPtr();
|
).toJson();
|
||||||
|
final handle = using(
|
||||||
final int handle;
|
(arena) => GomuksInit(config.toGomuksBufferPtr(arena).ref),
|
||||||
try {
|
malloc,
|
||||||
handle = GomuksInit(bufferPointer.ref);
|
);
|
||||||
} finally {
|
|
||||||
calloc
|
|
||||||
..free(bufferPointer.ref.base)
|
|
||||||
..free(bufferPointer);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ref.mounted) {
|
if (!ref.mounted) {
|
||||||
GomuksDestroy(handle);
|
GomuksDestroy(handle);
|
||||||
|
|
@ -233,13 +225,16 @@ class ClientController(final String? accountId) extends AsyncNotifier<int> {
|
||||||
String command, [
|
String command, [
|
||||||
Map<String, dynamic> data = const {},
|
Map<String, dynamic> data = const {},
|
||||||
]) => callGomuksMethod(data, (handle, data, callback) {
|
]) => callGomuksMethod(data, (handle, data, callback) {
|
||||||
final commandPointer = command.toNativeUtf8().cast<Char>();
|
|
||||||
try {
|
|
||||||
GomuksSubmitCommandAsync(handle, commandPointer, data, callback);
|
|
||||||
} finally {
|
|
||||||
// gomuks copies the command name before returning
|
// gomuks copies the command name before returning
|
||||||
calloc.free(commandPointer);
|
using(
|
||||||
}
|
(arena) => GomuksSubmitCommandAsync(
|
||||||
|
handle,
|
||||||
|
command.toNativeUtf8(allocator: arena).cast(),
|
||||||
|
data,
|
||||||
|
callback,
|
||||||
|
),
|
||||||
|
malloc,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
Future<void> redactEvent(RedactEventRequest report) =>
|
Future<void> redactEvent(RedactEventRequest report) =>
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ import "dart:convert";
|
||||||
import "dart:ffi";
|
import "dart:ffi";
|
||||||
import "dart:typed_data";
|
import "dart:typed_data";
|
||||||
|
|
||||||
import "package:ffi/ffi.dart";
|
|
||||||
import "package:nexus/src/third_party/gomuks.g.dart";
|
import "package:nexus/src/third_party/gomuks.g.dart";
|
||||||
|
|
||||||
extension GomuksOwnedBufferToX on GomuksOwnedBuffer {
|
extension GomuksOwnedBufferToX on GomuksOwnedBuffer {
|
||||||
|
|
@ -29,13 +28,13 @@ extension JsonToGomuksBuffer on Map<String, dynamic> {
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
Pointer<GomuksBorrowedBuffer> toGomuksBufferPtr() {
|
Pointer<GomuksBorrowedBuffer> toGomuksBufferPtr(Allocator allocator) {
|
||||||
final bytes = utf8.encode(json.encode(this));
|
final bytes = utf8.encode(json.encode(this));
|
||||||
|
|
||||||
final dataPtr = calloc<Uint8>(bytes.length);
|
final dataPtr = allocator<Uint8>(bytes.length);
|
||||||
dataPtr.asTypedList(bytes.length).setAll(0, bytes);
|
dataPtr.asTypedList(bytes.length).setAll(0, bytes);
|
||||||
|
|
||||||
return calloc<GomuksBorrowedBuffer>()
|
return allocator<GomuksBorrowedBuffer>()
|
||||||
..ref.base = dataPtr
|
..ref.base = dataPtr
|
||||||
..ref.length = bytes.length;
|
..ref.length = bytes.length;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue