Show / Hide Table of Contents

Struct FastBuffer<T>

Memory buffer on stack with ability to expand and use heap memory. Can be used for calling Windows API or building arrays. Must be used with [SkipLocalsInit] attribute; add it to the caller function or class.

public ref struct FastBuffer<T> where T : unmanaged
Type Parameters
Name Description
T
Examples
print.it(api.GetEnvironmentVariable("temp"));

unsafe class api : NativeApi {
	[DllImport("kernel32.dll", EntryPoint = "GetEnvironmentVariableW", SetLastError = true)]
	static extern int _GetEnvironmentVariable(string lpName, char* lpBuffer, int nSize);

	[SkipLocalsInit]
	internal static string GetEnvironmentVariable(string name) {
		using FastBuffer<char> b = new();
		for (; ; ) if (b.GetString(_GetEnvironmentVariable(name, b.p, b.n), out var s)) return s;
	}
}

Namespace: Au.More
Assembly: Au.dll

Constructors

Name Description
FastBuffer()

Allocates first buffer of default size. It is on stack (in this variable), and its length is StackSize/sizeof(T) elements of type T (2048 bytes or 1024 chars or 512 ints...).

FastBuffer(int)

Allocates first buffer of specified size.

Fields

Name Description
StackSize

A FastBuffer variable contains a field of this size. It is a memory buffer on stack. It is a byte count and does not depend on T. To get count of T elements on stack: StackSize/sizeof(T).

Properties

Name Description
this[int]

Gets reference to p[i]. Does not check bounds.

n

Memory buffer length as number of elements of type T.

p

Memory buffer pointer.

Methods

Name Description
Dispose()

Frees allocated memory if need.

FindByteStringLength()

Finds length of '\0'-terminated byte string in buffer. Returns FastBuffer<T>.n if there is no '\0' character.

FindStringLength()

Finds length of '\0'-terminated char string in buffer. Returns FastBuffer<T>.n if there is no '\0' character.

GetString(int, out string, BSFlags, string)

Gets API result as string, or allocates bigger buffer if old buffer was too small. This function can be used when T is char.

GetStringFindLength()

Finds length of '\0'-terminated UTF-16 string in buffer and converts to C# string. This function can be used when T is char. Use when length is unknown.

More(bool)

Allocates new bigger buffer of at least n*2 length. Frees old buffer if need.

More(int, bool)

Allocates new bigger buffer of specified length. Frees old buffer if need.

Operators

Name Description
implicit operator T*(in FastBuffer<T>)

Returns memory buffer pointer (FastBuffer<T>.p).