Skip to content

TVirtualBuffer is a generic, thread-safe virtual memory buffer class for Delphi. It provides stream-like read/write access to memory with full position tracking, locking, and bounds safety. Designed for high-performance applications, it’s ideal for buffering, serialization, and multithreaded memory operations.

License

Notifications You must be signed in to change notification settings

tinyBigGAMES/VirtualBuffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VirtualBuffer
Chat on Discord Follow on Bluesky

VirtualBuffer is a generic, high-performance memory buffer class for Delphi, implemented using memory-mapped files with built-in thread safety. It provides fast, in-memory, stream-like access to typed data with locking, serialization, and disk persistence — ideal for use cases like custom file formats, virtual storage, runtime serialization, and memory-bound computation.

🔧 Features

  • Generic type support (TVirtualBuffer<T>)
  • Backed by memory-mapped files (CreateFileMapping)
  • Thread-safe read/write access with TCriticalSection
  • Stream-style I/O: position tracking, Read, Write, Eos
  • String read/write with length-prefix encoding
  • Save/load entire buffer contents to/from disk
  • Bounds-checked index access via Item[]
  • Clean API with robust error handling

🚀 Use Cases

  • Fast, memory-resident data processing
  • In-memory storage of structured records
  • Virtual file or asset systems
  • Custom serialization pipelines
  • Preloading large binary datasets
  • Inter-thread or inter-module shared buffers

🛠️ Installation

Just add VirtualBuffer to your Delphi project uses section. No external dependencies required.

Tested on Delphi 12.3 and Windows 11, 64-bit.

📦 Usage Example

type
  TMyRecord = packed record
    ID: Integer;
    Value: Double;
  end;

var
  Buffer: TVirtualBuffer<TMyRecord>;
  Rec: TMyRecord;
begin
  Buffer := TVirtualBuffer<TMyRecord>.Create(1000); // 1000 records
  try
    Rec.ID := 42;
    Rec.Value := 3.14159;
    Buffer[0] := Rec; // Index-based assignment

    Buffer.Position := 0;
    Buffer.WriteString('Hello Virtual World');

    Buffer.Position := 0;
    Writeln(Buffer.ReadString); // Outputs: Hello Virtual World

    Buffer.SaveToFile('buffer.bin');
  finally
    Buffer.Free;
  end;
end;

🧠 API Overview

Constructor

constructor Create(aSize: UInt64);

Allocates memory for ASize elements of type T.

Properties

Property Description
Item[Index] Typed access to item at index
Capacity Total number of items
Size Total size in bytes
Position Current read/write position
Memory Raw pointer to buffer
Name OS memory map name (auto-generated GUID)

Methods

Basic I/O

function Write(const ABuffer; const ACount: UInt64): UInt64;
function Read(var ABuffer; const ACount: UInt64): UInt64;

Byte array support

function Write(const ABuffer: TBytes; const AOffset, ACount: UInt64): UInt64;
function Read(var ABuffer: TBytes; const AOffset, ACount: UInt64): UInt64;

Strings

procedure WriteString(const AValue: string);
function ReadString: string;

File Persistence

procedure SaveToFile(const AFilename: string);
class function LoadFromFile(const AFilename: string): TVirtualBuffer<T>;

Other

function Eob(): Boolean; // End of buffer

🔒 Thread Safety

All read/write operations and position changes are protected with a TCriticalSection, making the buffer safe for concurrent access across threads.

📂 File Format

Buffers saved using SaveToFile() are raw binary dumps of memory content. They can be reloaded with LoadFromFile() as long as the file size is divisible by SizeOf(T).

🚧️ This repository is currently under construction.

VirtualBuffer is actively being developed. Features, APIs, and internal structure are subject to change.

Contributions, feedback, and issue reports are welcome as the project evolves.

🛠️ Support and Resources

🤝 Contributing

Contributions to ✨ VirtualBuffer are highly encouraged! 🌟

  • 🐛 Report Issues: Submit issues if you encounter bugs or need help.
  • 💡 Suggest Features: Share your ideas to make VirtualBuffer even better.
  • 🔧 Create Pull Requests: Help expand the capabilities and robustness of the library.

Your contributions make a difference! 🙌✨

Contributors 👥🤝


📜 Licensing

VirtualBuffer is distributed under the 🆓 BSD-3-Clause License, allowing for redistribution and use in both source and binary forms, with or without modification, under specific conditions.
See the 📜 LICENSE file for more details.


🔒🧠 Thread-Safe, Generic Virtual Memory Buffer for Delphi — ⚡ Fast. 🧵 Safe. 🧰 Powerful.

Delphi

Made with ❤️ in Delphi

About

TVirtualBuffer is a generic, thread-safe virtual memory buffer class for Delphi. It provides stream-like read/write access to memory with full position tracking, locking, and bounds safety. Designed for high-performance applications, it’s ideal for buffering, serialization, and multithreaded memory operations.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages