fix _WIN32 lacking #include <inttypes.h>

allow creation of multiple shared memory segments on win32.
fix pybullet compilation on Visual Studio 2010 (old compiler)
This commit is contained in:
Erwin Coumans
2017-01-06 10:19:19 -08:00
parent 44126584fb
commit 4fc697f646
3 changed files with 96 additions and 27 deletions

View File

@@ -162,7 +162,12 @@ FILE* gTimingFile = 0;
#ifndef __STDC_FORMAT_MACROS #ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS #define __STDC_FORMAT_MACROS
#endif //__STDC_FORMAT_MACROS #endif //__STDC_FORMAT_MACROS
//see http://stackoverflow.com/questions/18107426/printf-format-for-unsigned-int64-on-windows
#ifndef _WIN32
#include <inttypes.h> #include <inttypes.h>
#endif
#define BT_TIMING_CAPACITY 16*65536 #define BT_TIMING_CAPACITY 16*65536
static bool m_firstTiming = true; static bool m_firstTiming = true;
@@ -246,12 +251,21 @@ struct btTimings
char newname[1024]; char newname[1024];
static int counter2=0; static int counter2=0;
sprintf(newname,"%s%d",name,counter2++); sprintf(newname,"%s%d",name,counter2++);
#ifdef _WIN32
fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%I64d.%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n",
threadId, startTimeDiv1000,startTimeRem1000Str, newname);
fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%I64d.%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}",
threadId, endTimeDiv1000,endTimeRem1000Str,newname);
#else
fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n", fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".%s ,\"ph\":\"B\",\"name\":\"%s\",\"args\":{}},\n",
threadId, startTimeDiv1000,startTimeRem1000Str, newname); threadId, startTimeDiv1000,startTimeRem1000Str, newname);
fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}", fprintf(gTimingFile,"{\"cat\":\"timing\",\"pid\":1,\"tid\":%d,\"ts\":%" PRIu64 ".%s ,\"ph\":\"E\",\"name\":\"%s\",\"args\":{}}",
threadId, endTimeDiv1000,endTimeRem1000Str,newname); threadId, endTimeDiv1000,endTimeRem1000Str,newname);
#endif
#endif #endif
} }

View File

@@ -2,22 +2,36 @@
#include "Win32SharedMemory.h" #include "Win32SharedMemory.h"
#include "Bullet3Common/b3Logging.h" #include "Bullet3Common/b3Logging.h"
#include "Bullet3Common/b3Scalar.h" #include "Bullet3Common/b3Scalar.h"
#include "LinearMath/btAlignedObjectArray.h"
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
//see also https://msdn.microsoft.com/en-us/library/windows/desktop/aa366551%28v=vs.85%29.aspx //see also https://msdn.microsoft.com/en-us/library/windows/desktop/aa366551%28v=vs.85%29.aspx
struct Win32SharedMemoryInteralData struct Win32SharedMemorySegment
{ {
int m_key;
HANDLE m_hMapFile; HANDLE m_hMapFile;
void* m_buf; void* m_buf;
TCHAR m_szName[1024]; TCHAR m_szName[1024];
Win32SharedMemorySegment()
:m_hMapFile(0),
m_buf(0),
m_key(-1)
{
m_szName[0] = 0;
}
};
struct Win32SharedMemoryInteralData
{
btAlignedObjectArray<Win32SharedMemorySegment> m_segments;
Win32SharedMemoryInteralData() Win32SharedMemoryInteralData()
:m_hMapFile(0),
m_buf(0)
{ {
} }
}; };
@@ -33,32 +47,53 @@ Win32SharedMemory::~Win32SharedMemory()
void* Win32SharedMemory::allocateSharedMemory(int key, int size, bool allowCreation) void* Win32SharedMemory::allocateSharedMemory(int key, int size, bool allowCreation)
{ {
b3Assert(m_internalData->m_buf==0); {
Win32SharedMemorySegment* seg = 0;
int i=0;
for (i=0;i<m_internalData->m_segments.size();i++)
{
if (m_internalData->m_segments[i].m_key == key)
{
seg = &m_internalData->m_segments[i];
break;
}
}
if (seg)
{
b3Error("already created shared memory segment using same key");
return seg->m_buf;
}
}
Win32SharedMemorySegment seg;
seg.m_key = key;
#ifdef UNICODE #ifdef UNICODE
swprintf_s (m_internalData->m_szName,TEXT("MyFileMappingObject%d"),key); swprintf_s (seg.m_szName,TEXT("MyFileMappingObject%d"),key);
#else #else
sprintf(m_internalData->m_szName,"MyFileMappingObject%d",key); sprintf(seg.m_szName,"MyFileMappingObject%d",key);
#endif #endif
m_internalData->m_hMapFile = OpenFileMapping( seg.m_hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name FALSE, // do not inherit the name
m_internalData->m_szName); // name of mapping object seg.m_szName); // name of mapping object
if (m_internalData->m_hMapFile==NULL) if (seg.m_hMapFile==NULL)
{ {
if (allowCreation) if (allowCreation)
{ {
m_internalData->m_hMapFile = CreateFileMapping( seg.m_hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file INVALID_HANDLE_VALUE, // use paging file
NULL, // default security NULL, // default security
PAGE_READWRITE, // read/write access PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD) 0, // maximum object size (high-order DWORD)
size, // maximum object size (low-order DWORD) size, // maximum object size (low-order DWORD)
m_internalData->m_szName); // name of mapping object seg.m_szName); // name of mapping object
} else } else
{ {
b3Warning("Could not create file mapping object (%d).\n",GetLastError()); b3Warning("Could not create file mapping object (%d).\n",GetLastError());
@@ -67,37 +102,56 @@ void* Win32SharedMemory::allocateSharedMemory(int key, int size, bool allowCre
} }
m_internalData->m_buf = MapViewOfFile(m_internalData->m_hMapFile, // handle to map object seg.m_buf = MapViewOfFile(seg.m_hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission FILE_MAP_ALL_ACCESS, // read/write permission
0, 0,
0, 0,
size); size);
if (m_internalData->m_buf == NULL) if (seg.m_buf == NULL)
{ {
b3Warning("Could not map view of file (%d).\n",GetLastError()); b3Warning("Could not map view of file (%d).\n",GetLastError());
CloseHandle(m_internalData->m_hMapFile); CloseHandle(seg.m_hMapFile);
return 0; return 0;
} }
return m_internalData->m_buf; m_internalData->m_segments.push_back(seg);
return seg.m_buf;
} }
void Win32SharedMemory::releaseSharedMemory(int key, int size) void Win32SharedMemory::releaseSharedMemory(int key, int size)
{ {
if (m_internalData->m_buf) Win32SharedMemorySegment* seg = 0;
{ int i=0;
for (i=0;i<m_internalData->m_segments.size();i++)
{
if (m_internalData->m_segments[i].m_key == key)
{
seg = &m_internalData->m_segments[i];
break;
}
}
UnmapViewOfFile(m_internalData->m_buf); if (seg==0)
m_internalData->m_buf=0; {
b3Error("Couldn't find shared memory segment");
return;
} }
if (m_internalData->m_hMapFile) if (seg->m_buf)
{ {
CloseHandle(m_internalData->m_hMapFile); UnmapViewOfFile(seg->m_buf);
m_internalData->m_hMapFile = 0; seg->m_buf=0;
} }
if (seg->m_hMapFile)
{
CloseHandle(seg->m_hMapFile);
seg->m_hMapFile = 0;
}
m_internalData->m_segments.removeAtIndex(i);
} }
Win32SharedMemoryServer::Win32SharedMemoryServer() Win32SharedMemoryServer::Win32SharedMemoryServer()

View File

@@ -1174,6 +1174,8 @@ static PyObject* pybullet_setGravity(PyObject* self, PyObject* args, PyObject* k
double gravZ = -10.0; double gravZ = -10.0;
int ret; int ret;
b3PhysicsClientHandle sm = 0; b3PhysicsClientHandle sm = 0;
b3SharedMemoryCommandHandle command;
b3SharedMemoryStatusHandle statusHandle;
int physicsClientId = 0; int physicsClientId = 0;
static char *kwlist[] = { "gravX", "gravY", "gravZ", "physicsClientId", NULL }; static char *kwlist[] = { "gravX", "gravY", "gravZ", "physicsClientId", NULL };
@@ -1189,9 +1191,8 @@ static PyObject* pybullet_setGravity(PyObject* self, PyObject* args, PyObject* k
} }
b3SharedMemoryCommandHandle command = b3InitPhysicsParamCommand(sm); command = b3InitPhysicsParamCommand(sm);
b3SharedMemoryStatusHandle statusHandle;
ret = b3PhysicsParamSetGravity(command, gravX, gravY, gravZ); ret = b3PhysicsParamSetGravity(command, gravX, gravY, gravZ);
// ret = b3PhysicsParamSetTimeStep(command, timeStep); // ret = b3PhysicsParamSetTimeStep(command, timeStep);
statusHandle = b3SubmitClientCommandAndWaitStatus(sm, command); statusHandle = b3SubmitClientCommandAndWaitStatus(sm, command);