bullet3/examples/SharedMemory/PosixSharedMemory.cpp
Erwin Coumans 03d991c92b add command-line argument for example browser and shared memory app, --shared_memory_key=<int>
fix some shared memory issues, client uses attach/detach, server uses create/remove shared memory
implement CMD_RESET_SIMULATION
2015-08-07 00:13:26 -07:00

110 lines
2.3 KiB
C++

#include "PosixSharedMemory.h"
#include "Bullet3Common/b3Logging.h"
#include "LinearMath/btScalar.h" //for btAssert
//haven't implemented shared memory on Windows yet, just Linux and Mac
#ifndef _WIN32
#define TEST_SHARED_MEMORY
#endif//_WIN32
#include <stddef.h>
#ifdef TEST_SHARED_MEMORY
#include <sys/shm.h>
#include <sys/ipc.h>
#endif
struct PosixSharedMemoryInteralData
{
bool m_createdSharedMemory;
void* m_sharedMemoryPtr;
PosixSharedMemoryInteralData()
:m_createdSharedMemory(false),
m_sharedMemoryPtr(0)
{
}
};
PosixSharedMemory::PosixSharedMemory()
{
m_internalData = new PosixSharedMemoryInteralData;
}
PosixSharedMemory::~PosixSharedMemory()
{
delete m_internalData;
}
struct btPointerCaster
{
union
{
void* ptr;
ptrdiff_t integer;
};
};
void* PosixSharedMemory::allocateSharedMemory(int key, int size, bool allowCreation)
{
#ifdef TEST_SHARED_MEMORY
int flags = (allowCreation ? IPC_CREAT : 0) | 0666;
int id = shmget((key_t) key, (size_t) size,flags);
if (id < 0)
{
b3Error("shmget error");
} else
{
btPointerCaster result;
result.ptr = shmat(id,0,0);
if (result.integer == -1)
{
b3Error("shmat returned -1");
} else
{
m_internalData->m_createdSharedMemory = allowCreation;
m_internalData->m_sharedMemoryPtr = result.ptr;
return result.ptr;
}
}
#else
//not implemented yet
btAssert(0);
#endif
return 0;
}
void PosixSharedMemory::releaseSharedMemory(int key, int size)
{
#ifdef TEST_SHARED_MEMORY
int flags = 0666;
int id = shmget((key_t) key, (size_t) size,flags);
if (id < 0)
{
b3Error("PosixSharedMemory::releaseSharedMemory: shmget error");
} else
{
if (m_internalData->m_createdSharedMemory)
{
int result = shmctl(id,IPC_RMID,0);
if (result == -1)
{
b3Error("PosixSharedMemory::releaseSharedMemory: shmat returned -1");
} else
{
b3Printf("PosixSharedMemory::releaseSharedMemory removed shared memory");
}
m_internalData->m_createdSharedMemory = false;
m_internalData->m_sharedMemoryPtr = 0;
} else
{
shmdt(m_internalData->m_sharedMemoryPtr);
m_internalData->m_sharedMemoryPtr = 0;
b3Printf("PosixSharedMemory::releaseSharedMemory detached shared memory\n");
}
}
#endif
}