Component for handling Memory Mapped Files will store matrices, vectors or the whole array of files in the RAM and share this data between several programs. Now mapping files into memory is as easy as in Linux, Unix or Mac – just one row of code is enough. For reference, in Windows one should write many rows of code to use memory-mapped files.
Just call a win32 API CreateFileMapping or MapViewOfFile from any application to get access to the memory. Many programming languages can access memory-mapped files:
No more win32 api related crashes! We made sure that all is functioning well. No RW requests to restricted or non-existing memory areas.
Data type | Vector | Matrix |
---|---|---|
Byte | TByteVector | TByteMatrix |
Word | TWordVector | TWordMatrix |
Cardinal | TCardinalVector | TCardinalMatrix |
Integer | TIntegerVector | TIntegerMatrix |
Int64 | TInt64Vector | TInt64Matrix |
Single | TSingleVector | TSingleMatrix |
Double | TDoubleVector | TDoubleMatrix |
- Merge vectors, Merge matrices
- Fill with random values/zeros, create from file, save to file
- Add rows/columns, insert rows/columns, get row/column, delete rows/columns
Use cases
Unload heavy-load calculations to external program
You have a Delphi application which collects data such as images, video streams, matrices. Your task is to manipulate this data and return the result back to the user. But you are not familiar with processing this data in Delphi. And to be fair, Delphi does not offer a convenient way to process this data in efficient manner.
With PBMemoryMappedFiles all you have to do is just map the data between 2 applications and pick a library you are most familiar with:
- Python numpy
- C BLAS (Basic Linear Algebra Subprograms)
- you name it…
Microservices and failure-resistant systems
You are developing software for a critical use case, which should run at all costs, despite possible hardware and software failures:
- Spacecraft software (like Apollo Guidance System)
- Medical software
- Military software
- Software for work with Hazardous materials: nuclear, chemical, etc.
With PBMemoryMappedFiles you can divide your 1 program into N separate applications (also called microprocesses, jobs, actors, cores). Just map the data between all microservices involved and you are good to go.
Prerequirements
OS compatibility
7 SP2 x64 |
8 x64 |
8.1 x64 |
10 x64 |
11 x64 |
RAD Studio compatibility
Delphi |
---|
10 Seattle |
10.1 Berlin |
10.2 Tokyo |
10.3 Rio |
10.4 Sydney |
11 Alexandria |
1 user
3 users
5 users
10 users
Site license
Free components in a bundle
Buy a bundle now and enjoy all new component releases for free during the update period. |
Keep track of multiple versions
You will get access to all older versions and a detailed changelog. |
Free new features
Get all new component features for free during the free update period. |
Install once use forever
Our products are not time limited. |
Source code promise
In case our component will become obsolete and we no longer support it, we will provide you its source code. So you don’t have to worry about a replacement. |
References:
> QSG code example provided with the installer
After installation, a group PyBridge will appear in the Tool Palette of Delphi, where you can find the new component TPBMemoryMappedFiles.
New component TPBMemoryMappedFiles can be found in the Tool Palette under PyBridge group
Now PBMemoryMappedFiles is ready to be used. To demonstrate how it works, let’s create two applications – a SERVER and a CLIENT for distributed work with images.
Layout of SERVER and CLIENT applications
SERVER application. Add to the form:
- “Load Picture” button, a standard OpenPictureDialog component and a TImage object – for loading an image from a file
- “Reverse” button – for triggering a synchronous image change
- TPBMemoryMappedFiles1 – for sharing main data between multiple applications and for sharing a counter status, which serves as a trigger for updating an image upon clicking “Reverse” button
CLIENT application. Add to the form:
- “Open MMF” button – for connecting to a file displayed in memory
- “Reverse” button – for triggering a synchronous image change
- TPBMemoryMappedFiles1 – for sharing main data between multiple applications and sharing a counter status, which serves as a trigger for updating an image upon clicking “Reverse” button
Add a unit PBMMFTypes to uses.
Typically, the address space that is allocated in memory by the system can be represented as an address vector with a step depending on the type of data. In the PBMemoryMappedFiles component, work with the address space of Memory Mapped Files is carried out using objects of special classes in the form of vectors or two-dimensional arrays, which are described in PBMMFTypes.
Data type | Vector | Matrix |
---|---|---|
Byte | TByteVector | TByteMatrix |
Word | TWordVector | TWordMatrix |
Cardinal | TCardinalVector | TCardinalMatrix |
Integer | TIntegerVector | TIntegerMatrix |
Int64 | TInt64Vector | TInt64Matrix |
Single | TSingleVector | TSingleMatrix |
Double | TDoubleVector | TDoubleMatrix |
Such an object is not a data storage but provides a convenient way to access the data of Memory Mapped Files. To work correctly with MMP, it is necessary to specify the data type and type of address space mapping: vector (in the general case) or matrix (for more convenient and quick work with data that can be conveniently represented as two-dimensional matrices, for example, for working with flat images).
Declare variables for the Memory Mapped Files in both SERVER and CLIENT applications. We will work with color images. In Delphi, the TСolor type is used to store information about the color of a pixel, whose objects consist of 4 bytes. Therefore, to work with such objects, the Cardinal type is sufficient.
Adding PBMMFTypes to uses and declaring variables
In the SERVER application, create a procedure for loading an image from a file by clicking the “Load Image” button. In this procedure, after loading the image, create the matrix “imgMatrix” (object of PBMappedMemoryFiles), specify its dimensions and give the name “Smile” to this Memory Mapped File. Then fill the created array with the color values of the pixels of the loaded image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
procedure TForm1.LOADbuttonClick(Sender: TObject); var rcnt, ccnt, i, j: Integer; begin if OpenPictureDialog1.Execute then Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName); rcnt := Image1.Picture.Height; ccnt := Image1.Picture.Width; PBMemoryMappedFiles1.CreateMatrix(imgMatrix, ccnt, rcnt, 'Smile'); // Creation of a PBMemoryMapFiles object for the counter (MMF matrix name, matrix width, matrix height, name of MMF). for i := 0 to Pred(rcnt) do // Uploading the image data to the PBMemoryMapFiles object for j := 0 to Pred(ccnt) do begin imgMatrix[j, i] := Image1.Canvas.Pixels[j, i]; Form1.Canvas.Pixels[j, i] := imgMatrix[j, i]; end; ReverseButton.Enabled := True; if FEventThread = nil then begin FEventThread := TEventThread.Create; FEventThread.TerminateFlg := True; FEventThread.Resume; end; end; |
In the CLIENT application, create a procedure for connecting to the Memory Mapped File named “Smile”. Here you also need to create the matrix object (this time without additional parameters) and then use the imgMatrix.Open command to associate this object with the Memory Mapped File “Smile”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
procedure TForm1.OPENbuttonClick(Sender: TObject); begin PBMemoryMappedFiles1.CreateMatrix(imgMatrix); // Creation of a PBMemoryMapFiles object for the counter (MMF matrix name). imgMatrix.Open('Smile'); // Linking PBMemoryMapFiles object with MemoryMappedFile "Smile" if imgMatrix.MMFName <> 'Smile' then begin imgMatrix.Close; exit; end; ImageUpdate(); ReverseButton.Enabled := True; if FEventThread = nil then begin FEventThread := TEventThread.Create; FEventThread.TerminateFlg := True; FEventThread.Resume; end; end; |
For BOTH applications create a set of identical functions.
In the ImageUpdate procedure pull the current state of an image, stored in the imgMatrix MMF, and perform a pixel-by-pixel update of the Form.Canvas.
1 2 3 4 5 6 7 8 9 |
// Updating an image from PBMemoryMapFiles object procedure TForm1.ImageUpdate(); var i, j: Integer; begin for i := 0 to Pred(imgMatrix.RowCount) do for j := 0 to Pred(imgMatrix.ColCount) do Form1.Canvas.Pixels[j, i] := imgMatrix[j, i]; end; |
In the handler procedure of the “Reverse” button make some changes to the imgMatrix MMF. Afterwards notify all applications, including self, of the changes by incrementing the counter stored in the ‘msg’ MMF. This will trigger all applications to perform a data pull from the imgMatrix MMF and to redraw their respective Form.Canvas.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Modification of the PBMemoryMapFiles object procedure TForm1.REVERSEbuttonClick(Sender: TObject); var i, j: Integer; buff: Cardinal; begin for i := 19 to 45 do for j := 0 to 3 do begin buff := imgMatrix[i, 43-j]; imgMatrix[i, 43-j] := imgMatrix[i, 35+j]; imgMatrix[i, 35+j] := buff; end; FEventThread.IncCount; end; |
But how the application will know there is a change of the counter? With the help of the listener thread of cause. Create a thread in which both SERVER and CLIENT monitor the counter state. Use another PBMemoryMeppedFiles1 object – flagVector: TCardinalVector to synchronise the counter between applications. As soon as counter has changed update the graphical display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
constructor TEventThread.Create; begin inherited Create; Form1.PBMemoryMappedFiles1.CreateVector(flagVector, 1, 'msg'); // Creation of a PBMemoryMapFiles object for the counter (MMF vector name, size of the vector, name of MMF). flagVector[0] := 0; FCount := flagVector[0]; end; destructor TEventThread.Destroy; begin flagVector.Close; inherited Destroy; end;; // Incrementation of the counter - modificiation of PBMemoryMapFiles object procedure TEventThread.IncCount; begin flagVector[0]:=flagVector[0] + 1; end; procedure TEventThread.OneCount; begin Form1.ChangedCount(FCount); end; procedure TForm1.ChangedCount(Count: Cardinal); begin ImageUpdate; end; |
Launch the SERVER application and two or more copies of the CLIENT application
Load the image by clicking «Load Picture» button in the SERVER application
Connect both CLIENT applications to the Memory Mapped File by clicking “Open MMF” button
Clicking the “Reverse” button in any of the applications leads to a synchronous change of images in all applications
This example demonstrates how you can easily and quickly perform distributed work with images with a help of PBMemoryMappedFiles component. Now you can also enjoy the full power of PBMemoryMappedFiles in your applications!