SodiumScope class

A lexical scope that owns libsodium allocations and disposes every one it still owns, in reverse (LIFO) order, when the body returns or throws.

This is not a package:ffi Arena: every allocation goes through SodiumPointer/SecureKeyFFI and therefore uses libsodium's guarded, secure memory (sodium_malloc, mlock, mprotect, zero-on-free) — never malloc/calloc.

Ownership: the copy*/alloc* methods register the allocation for disposal at scope exit. The take* methods transfer ownership out of the scope (to a returned SodiumPointer-backed list, String, SecureKeyFFI or SodiumPointer) and stop tracking it. On the error path the body never reaches its take* calls, so the scope frees those buffers too.

Every take* method must be given the very instance that copy*/alloc* returned. Passing a SodiumPointer.viewAt view of it, or a resource from outside the scope, is a programming error and asserted against.

Scope exit disposes every remaining entry, even if one of the disposals fails. Such a failure is reported as an unhandled error to the current Zone instead of being thrown, so that it can neither mask the error that broke the body nor turn a successful call into a failing one.

Instances are created by and only valid for the duration of a sodiumScope call.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

alloc<T extends NativeType>(int count, {bool zeroMemory = false, MemoryProtection memoryProtection = .readWrite}) SodiumPointer<T>
Allocates a fresh, tracked output buffer of count elements.
copyList<T extends NativeType>(List<num> data, {MemoryProtection memoryProtection = .readOnly}) SodiumPointer<T>
Copies a typed data list into a fresh, tracked SodiumPointer.
copyString(String str, {int? memoryWidth, bool zeroTerminated = false, MemoryProtection memoryProtection = .readOnly, Encoding encoding = utf8}) SodiumPointer<Char>
Copies str into a fresh, tracked SodiumPointer of Char.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
takeBytes<TList extends List<num>>(SodiumPointer<NativeType> pointer) → TList
Hands pointer's memory to the returned list and stops tracking it.
takePointer<T extends NativeType>(SodiumPointer<T> pointer) SodiumPointer<T>
Returns pointer to the caller live and stops tracking it, so scope exit does not free it.
takeString(SodiumPointer<Char> pointer, {bool zeroTerminated = true, Encoding encoding = utf8}) String
Copies pointer out as a Dart String, then frees the native buffer immediately and stops tracking it.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited