some code changes
This commit is contained in:
+145
@@ -1,6 +1,14 @@
|
|||||||
#include "SharedMemory.h"
|
#include "SharedMemory.h"
|
||||||
#include "Config.h"
|
#include "Config.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: createOrOpenMapping
|
||||||
|
Description: Creates or opens a file mapping and maps it into the process address space.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping information and handles
|
||||||
|
Returns:
|
||||||
|
- bool: True if the mapping was successfully created/opened, otherwise false
|
||||||
|
*/
|
||||||
bool SharedMemory::createOrOpenMapping(MappingInfo& mapping)
|
bool SharedMemory::createOrOpenMapping(MappingInfo& mapping)
|
||||||
{
|
{
|
||||||
if (mapping.recordSize == 0)
|
if (mapping.recordSize == 0)
|
||||||
@@ -84,3 +92,140 @@ bool SharedMemory::createOrOpenMapping(MappingInfo& mapping)
|
|||||||
mapping.mappedCapacity = header->capacity;
|
mapping.mappedCapacity = header->capacity;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: closeMapping
|
||||||
|
Description: Unmaps and closes all resources associated with a file mapping.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping to close
|
||||||
|
Returns:
|
||||||
|
- None
|
||||||
|
*/
|
||||||
|
void SharedMemory::closeMapping(MappingInfo& mapping)
|
||||||
|
{
|
||||||
|
if (mapping.mappedView != nullptr)
|
||||||
|
{
|
||||||
|
UnmapViewOfFile(mapping.mappedView);
|
||||||
|
mapping.mappedView = nullptr;
|
||||||
|
}
|
||||||
|
if (mapping.mappingHandle != NULL)
|
||||||
|
{
|
||||||
|
CloseHandle(mapping.mappingHandle);
|
||||||
|
mapping.mappingHandle = NULL;
|
||||||
|
}
|
||||||
|
if (mapping.fileHandle != INVALID_HANDLE_VALUE)
|
||||||
|
{
|
||||||
|
CloseHandle(mapping.fileHandle);
|
||||||
|
mapping.fileHandle = INVALID_HANDLE_VALUE;
|
||||||
|
}
|
||||||
|
mapping.mappedCapacity = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: getHeader
|
||||||
|
Description: Returns the file header stored at the beginning of the mapped memory region.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping information and handles
|
||||||
|
Returns:
|
||||||
|
- FileHeader*: Pointer to the file header, or nullptr if the mapping is not valid
|
||||||
|
*/
|
||||||
|
FileHeader* SharedMemory::getHeader(MappingInfo& mapping)
|
||||||
|
{
|
||||||
|
if (mapping.mappedView == nullptr)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return reinterpret_cast<FileHeader*>(mapping.mappedView);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: getRecordAddress
|
||||||
|
Description: Returns the address of a record at the specified index within the mapped memory region.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping information and handles
|
||||||
|
- index: size_t, record index
|
||||||
|
Returns:
|
||||||
|
- void*: Pointer to the record, or nullptr if the mapping is not valid
|
||||||
|
*/
|
||||||
|
void* SharedMemory::getRecordAddress(MappingInfo& mapping, size_t index)
|
||||||
|
{
|
||||||
|
if (mapping.mappedView == nullptr)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return reinterpret_cast<char*>(mapping.mappedView) + sizeof(FileHeader) + index * mapping.recordSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: getRecordAddress
|
||||||
|
Description: Returns the address of a record at the specified index within the mapped memory region.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping information and handles
|
||||||
|
- index: size_t, record index
|
||||||
|
Returns:
|
||||||
|
- void*: Pointer to the record, or nullptr if the mapping is not valid
|
||||||
|
*/
|
||||||
|
void* SharedMemory::getRecordAddress(MappingInfo& mapping, size_t index)
|
||||||
|
{
|
||||||
|
if (mapping.mappedView == nullptr)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return reinterpret_cast<char*>(mapping.mappedView) + sizeof(FileHeader) + index * mapping.recordSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: getRecordCount
|
||||||
|
Description: Returns the number of records currently stored in the mapping.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping information and handles
|
||||||
|
Returns:
|
||||||
|
- size_t: Number of records in the mapping
|
||||||
|
*/
|
||||||
|
size_t SharedMemory::getRecordCount(MappingInfo& mapping)
|
||||||
|
{
|
||||||
|
FileHeader* header = getHeader(mapping);
|
||||||
|
if (header == nullptr)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return header->recordCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: setRecordCount
|
||||||
|
Description: Updates the number of records stored in the mapping.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping information and handles
|
||||||
|
- count: size_t, new record count
|
||||||
|
Returns:
|
||||||
|
- None
|
||||||
|
*/
|
||||||
|
void SharedMemory::setRecordCount(MappingInfo& mapping, size_t count)
|
||||||
|
{
|
||||||
|
FileHeader* header = getHeader(mapping);
|
||||||
|
if (header == nullptr)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
header->recordCount = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Function: getCapacity
|
||||||
|
Description: Returns the current capacity of the mapping.
|
||||||
|
Parameters:
|
||||||
|
- mapping: MappingInfo&, mapping information and handles
|
||||||
|
Returns:
|
||||||
|
- size_t: Maximum number of records that can be stored in the mapping
|
||||||
|
*/
|
||||||
|
size_t SharedMemory::getCapacity(MappingInfo& mapping)
|
||||||
|
{
|
||||||
|
FileHeader* header = getHeader(mapping);
|
||||||
|
if (header == nullptr)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return header->capacity;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user