mirror of
https://github.com/bulletphysics/bullet3
synced 2024-12-13 21:30:09 +00:00
add Windows version of shared memory, very basic implementation (only allows one single allocation)
This commit is contained in:
parent
f74e3782aa
commit
a94ac6300a
@ -18,6 +18,7 @@ SET(App_ExampleBrowser_SRCS
|
||||
../SharedMemory/PhysicsServer.cpp
|
||||
../SharedMemory/PhysicsClient.cpp
|
||||
../SharedMemory/PosixSharedMemory.cpp
|
||||
../SharedMemory/Win32SharedMemory.cpp
|
||||
../BasicDemo/BasicExample.cpp
|
||||
../BasicDemo/BasicExample.h
|
||||
../ForkLift/ForkLiftDemo.cpp
|
||||
|
@ -53,6 +53,7 @@
|
||||
"../SharedMemory/PhysicsServer.cpp",
|
||||
"../SharedMemory/PhysicsClient.cpp",
|
||||
"../SharedMemory/PosixSharedMemory.cpp",
|
||||
"../SharedMemory/Win32SharedMemory.cpp",
|
||||
"../BasicDemo/BasicExample.*",
|
||||
"../Benchmarks/*",
|
||||
"../CommonInterfaces/*",
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "../CommonInterfaces/CommonMultiBodyBase.h"
|
||||
#include "PosixSharedMemory.h"
|
||||
#include "Win32SharedMemory.h"
|
||||
#include "SharedMemoryCommon.h"
|
||||
|
||||
class PhysicsClient : public SharedMemoryCommon
|
||||
@ -43,7 +44,11 @@ m_counter(0),
|
||||
m_wantsTermination(false)
|
||||
{
|
||||
b3Printf("Started PhysicsClient");
|
||||
#ifdef _WIN32
|
||||
m_sharedMemory = new Win32SharedMemoryClient();
|
||||
#else
|
||||
m_sharedMemory = new PosixSharedMemory();
|
||||
#endif
|
||||
}
|
||||
|
||||
PhysicsClient::~PhysicsClient()
|
||||
|
@ -2,6 +2,8 @@
|
||||
#include "PhysicsServer.h"
|
||||
|
||||
#include "PosixSharedMemory.h"
|
||||
#include "Win32SharedMemory.h"
|
||||
|
||||
#include "../Importers/ImportURDFDemo/MyURDFImporter.h"
|
||||
#include "../Importers/ImportURDFDemo/MyMultiBodyCreator.h"
|
||||
#include "../Importers/ImportURDFDemo/URDF2Bullet.h"
|
||||
@ -48,7 +50,12 @@ m_testBlock1(0),
|
||||
m_wantsShutdown(false)
|
||||
{
|
||||
b3Printf("Started PhysicsServer\n");
|
||||
bool useServer = true;
|
||||
#ifdef _WIN32
|
||||
m_sharedMemory = new Win32SharedMemoryServer();
|
||||
#else
|
||||
m_sharedMemory = new PosixSharedMemory();
|
||||
#endif
|
||||
}
|
||||
|
||||
void PhysicsServer::releaseSharedMemory()
|
||||
|
106
examples/SharedMemory/Win32SharedMemory.cpp
Normal file
106
examples/SharedMemory/Win32SharedMemory.cpp
Normal file
@ -0,0 +1,106 @@
|
||||
#ifdef _WIN32
|
||||
#include "Win32SharedMemory.h"
|
||||
#include "Bullet3Common/b3Logging.h"
|
||||
#include "Bullet3Common/b3Scalar.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
//see also https://msdn.microsoft.com/en-us/library/windows/desktop/aa366551%28v=vs.85%29.aspx
|
||||
|
||||
//TCHAR szName[]=TEXT("Global\\MyFileMappingObject2");
|
||||
TCHAR szName[]=TEXT("MyFileMappingObject2");
|
||||
|
||||
struct Win32SharedMemoryInteralData
|
||||
{
|
||||
HANDLE m_hMapFile;
|
||||
|
||||
void* m_buf;
|
||||
|
||||
Win32SharedMemoryInteralData()
|
||||
:m_hMapFile(0),
|
||||
m_buf(0)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
Win32SharedMemory::Win32SharedMemory()
|
||||
{
|
||||
m_internalData = new Win32SharedMemoryInteralData;
|
||||
}
|
||||
Win32SharedMemory::~Win32SharedMemory()
|
||||
{
|
||||
delete m_internalData;
|
||||
}
|
||||
|
||||
void* Win32SharedMemory::allocateSharedMemory(int key, int size)
|
||||
{
|
||||
b3Assert(m_internalData->m_buf==0);
|
||||
|
||||
|
||||
if (this->isServer())
|
||||
{
|
||||
m_internalData->m_hMapFile = CreateFileMapping(
|
||||
INVALID_HANDLE_VALUE, // use paging file
|
||||
NULL, // default security
|
||||
PAGE_READWRITE, // read/write access
|
||||
0, // maximum object size (high-order DWORD)
|
||||
size, // maximum object size (low-order DWORD)
|
||||
szName); // name of mapping object
|
||||
} else
|
||||
{
|
||||
m_internalData->m_hMapFile = OpenFileMapping(
|
||||
FILE_MAP_ALL_ACCESS, // read/write access
|
||||
FALSE, // do not inherit the name
|
||||
szName); // name of mapping object
|
||||
|
||||
}
|
||||
|
||||
if (m_internalData->m_hMapFile == NULL)
|
||||
{
|
||||
b3Error("Could not create file mapping object (%d).\n",GetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
m_internalData->m_buf = MapViewOfFile(m_internalData->m_hMapFile, // handle to map object
|
||||
FILE_MAP_ALL_ACCESS, // read/write permission
|
||||
0,
|
||||
0,
|
||||
size);
|
||||
|
||||
if (m_internalData->m_buf == NULL)
|
||||
{
|
||||
b3Error("Could not map view of file (%d).\n",GetLastError());
|
||||
CloseHandle(m_internalData->m_hMapFile);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return m_internalData->m_buf;
|
||||
}
|
||||
void Win32SharedMemory::releaseSharedMemory(int key, int size)
|
||||
{
|
||||
if (m_internalData->m_buf)
|
||||
{
|
||||
UnmapViewOfFile(m_internalData->m_buf);
|
||||
m_internalData->m_buf=0;
|
||||
}
|
||||
if (m_internalData->m_hMapFile)
|
||||
{
|
||||
CloseHandle(m_internalData->m_hMapFile);
|
||||
}
|
||||
}
|
||||
|
||||
Win32SharedMemoryServer::Win32SharedMemoryServer()
|
||||
{
|
||||
}
|
||||
Win32SharedMemoryServer::~Win32SharedMemoryServer()
|
||||
{
|
||||
}
|
||||
|
||||
Win32SharedMemoryClient::Win32SharedMemoryClient()
|
||||
{
|
||||
}
|
||||
Win32SharedMemoryClient:: ~Win32SharedMemoryClient()
|
||||
{
|
||||
}
|
||||
|
||||
#endif //_WIN32
|
45
examples/SharedMemory/Win32SharedMemory.h
Normal file
45
examples/SharedMemory/Win32SharedMemory.h
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef WIN32_SHARED_MEMORY_H
|
||||
#define WIN32_SHARED_MEMORY_H
|
||||
|
||||
#include "SharedMemoryInterface.h"
|
||||
|
||||
|
||||
|
||||
class Win32SharedMemory : public SharedMemoryInterface
|
||||
{
|
||||
|
||||
struct Win32SharedMemoryInteralData* m_internalData;
|
||||
|
||||
public:
|
||||
Win32SharedMemory();
|
||||
virtual ~Win32SharedMemory();
|
||||
|
||||
virtual void* allocateSharedMemory(int key, int size);
|
||||
virtual void releaseSharedMemory(int key, int size);
|
||||
virtual bool isServer() const = 0;
|
||||
};
|
||||
|
||||
class Win32SharedMemoryServer : public Win32SharedMemory
|
||||
{
|
||||
public:
|
||||
Win32SharedMemoryServer();
|
||||
virtual ~Win32SharedMemoryServer();
|
||||
virtual bool isServer() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class Win32SharedMemoryClient : public Win32SharedMemory
|
||||
{
|
||||
public:
|
||||
Win32SharedMemoryClient();
|
||||
virtual ~Win32SharedMemoryClient();
|
||||
virtual bool isServer() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //WIN32_SHARED_MEMORY_H
|
Loading…
Reference in New Issue
Block a user