Ara3D.Buffers 1.4.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ara3D.Buffers --version 1.4.4                
NuGet\Install-Package Ara3D.Buffers -Version 1.4.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ara3D.Buffers" Version="1.4.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ara3D.Buffers --version 1.4.4                
#r "nuget: Ara3D.Buffers, 1.4.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Ara3D.Buffers as a Cake Addin
#addin nuget:?package=Ara3D.Buffers&version=1.4.4

// Install Ara3D.Buffers as a Cake Tool
#tool nuget:?package=Ara3D.Buffers&version=1.4.4                

Ara3D.Buffers

NuGet Version

Utilities for working with large arrays of unmanaged types.

IBuffer

The IBuffer interface is presents a simple interface for an array of unmanaged types, where the type is not known at compile-time.

This allows us to work more easily with collections of buffers where the underlying type can vary at run-time.

   public interface IBuffer
   {
       Array Data { get; }
       void WithPointer(Action<IntPtr> action);
       int ElementSize { get; }
   }

The total number of bytes is determined by multiply the data array by the element size. This is provided in an extension function

    public static long GetNumBytes(this IBuffer buffer)
        => (long)buffer.NumElements() * buffer.ElementSize;

Accessing the raw bytes can be done using a pattern similar to:

    public static unsafe void Write(this Stream stream, IBuffer buffer)
        => buffer.WithPointer(ptr => {
            stream.WriteBytesBuffered((byte*)ptr.ToPointer(), buffer.GetNumBytes());
        });

INamedBuffer

Frequently a buffer is associated with a name or identifier. The INamedBuffer introduces a string name property, for convenience.

    public interface INamedBuffer : IBuffer
    {
        string Name { get; }
    }

Generic Versions

There exist generic versions of the interfaces as well, which provide access to the underlying data array.

    public interface IBuffer<out T> : IBuffer
        where T: unmanaged
    {
        T[] GetTypedData();
    }

    public interface INamedBuffer<out T> 
        : INamedBuffer, IBuffer<T> where T: unmanaged
    {
    }

Use Case

This structure is particularly useful when serializing and manipulating raw memory such as attribute buffers associated with 3D geometry, ZIP files (or other binary archive formats), or color data associated with bitmaps.

Documentation

This is a very small library, and we recommend reading the source code and looking at some projects that use it:

  • Ara3D.Graphics
  • Ara3D.Serialization.BFAST
  • Ara3D.Serialization.G3D
  • Ara3D.Serialization.VIM

Size Limitations

Due to limitations in C# the array can have at most 2^31 elements (approximately 2 billion).

About ByteSpan

ByteSpan is designed specifically for working with pointers to unmanaged (e.g. pinned) memory.

A ByteSpan consists of a literal pointer to a byte, and the number of bytes. It doesn’t have the ref struct limitations of Span<byte> data type and is faster for memory accessing (just grab the pointer and address it).

The only caveat is you have to pin memory before using it, and be careful about avoiding accessing memory access overrun.

Pinning memory prevents the garbage collector from moving it or freeing it, putting the responsibility of lifetime management of the memory on the developer.

Pinning memory involves a call to GCHandle.Alloc(<your array>, GCHandlerType.Pinned) which returns a handle, and provides raw accesss to memory via AddrOfPinnedObject().

While this is unusual for a C# developer, this is common practice for C/C++ developers.

It just means that as software developers, we need to consider how and when we deallocate memory in a reasonable way. It is no different than the requirements of any disposable resource (e.g., file handles, or window contexts).

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Ara3D.Buffers:

Package Downloads
Ara3D.Serialization.BFAST

A library for reading and writing collections of arrays of binary data (archives).

Ara3D.Serialization.VIM

A library for reading and writing architectural BIM models in the VIM format.

Ara3D.Graphics

A library for working with and rendering 2D and 3D graphics.

Speckle.WebIfc.Importer

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.5 251 10/9/2024
1.4.4 279 8/25/2024
1.4.3 234 3/19/2024
1.4.1 222 3/12/2024
1.3.2 105 3/5/2024
1.3.0 112 3/3/2024