diff --git a/Parallel/MemoryMap/MapObject.vb b/Parallel/MemoryMap/MapObject.vb index ead3cd0..024ef5e 100644 --- a/Parallel/MemoryMap/MapObject.vb +++ b/Parallel/MemoryMap/MapObject.vb @@ -62,6 +62,11 @@ Public Class MapObject : Implements IDisposable Private Sub New() End Sub + ''' + ''' load object from the memory region by a specific type schema + ''' + ''' + ''' Public Function GetObject(type As Type) As Object Dim obj As Object Dim memory As MemoryMappedFile = MemoryMappedFile.OpenExisting(hMem) @@ -79,11 +84,16 @@ Public Class MapObject : Implements IDisposable } End Function - Public Shared Function FromObject(obj As Object) As MapObject + ''' + ''' serialize object in BSON to a memory region + ''' + ''' + ''' + Public Shared Function FromObject(obj As Object, Optional hMemP As String = Nothing) As MapObject Dim type As Type = obj.GetType Dim element = type.GetJsonElement(obj, New JSONSerializerOptions) Dim bufferSize As Integer - Dim hMem As String = App.GetNextUniqueName($"mem_{type.Name}_") + Dim hMem As String = If(hMemP.StringEmpty, App.GetNextUniqueName($"mem_{type.Name}_"), hMemP) Static mapFiles As New Dictionary(Of String, MemoryMappedFile) @@ -96,7 +106,10 @@ Public Class MapObject : Implements IDisposable Dim hMemFile As MemoryMappedFile = MemoryMappedFile.CreateNew(hMem, bufferSize) Dim view As MemoryMappedViewStream = hMemFile.CreateViewStream - Call mapFiles.Add(hMem, hMemFile) + SyncLock mapFiles + Call mapFiles.Add(hMem, hMemFile) + End SyncLock + Call view.Write(buffer, Scan0, bufferSize) Erase buffer diff --git a/Parallel/MemoryMap/UnmanageMemoryRegion.vb b/Parallel/MemoryMap/UnmanageMemoryRegion.vb index 6cf87ef..d56db27 100644 --- a/Parallel/MemoryMap/UnmanageMemoryRegion.vb +++ b/Parallel/MemoryMap/UnmanageMemoryRegion.vb @@ -44,4 +44,12 @@ Public Class UnmanageMemoryRegion Public Property memoryFile As String Public Property size As Integer + Public Function GetMemoryMap() As MapObject + Return MapObject.FromPointer(Me) + End Function + + Public Overrides Function ToString() As String + Return $"{memoryFile} ({StringFormats.Lanudry(size)})" + End Function + End Class