SharedArray<T extends NativeType> class

Instead of allocating many arrays, share one array which gets deleted when all references to it got deleted. This can lead to better performance and reduced memory usage.

Gets lazily initialized when the first reference gets created. It is safe to use even after deletion; it just reallocates new memory for new references. Not deleting all references causes memory leaks.

final shared = SharedArray<Uint8>(10);
// All instances of [WritesToArray] share the same [SharedArray] so they don't have to all
// allocate their own array.
class WritesToArray {
  final _buffer = shared.newRef();

  Uint8List someNativeWrapper(){
    writeToBuffer(_buffer.ptr);
    return Uint8List.fromList(_buffer.view);
  }
  void delete() => _buffer.delete();
}

Constructors

SharedArray(int length, [Pointer<T> _allocate({int count}) = ffi.allocate])
_allocateArray allocates an Array of length elements

Properties

hashCode int
The hash code for this object. [...]
read-only, inherited
length int
final
refCount int
read-only
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
sharedBuffer Array<T>
@protected, read / write

Methods

newRef() ArrayRef<T>
Create new reference to this SharedArray.
@mustCallSuper
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() String
Returns a string representation of this object.
inherited

Operators

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