2015-05-28 23:05:24 +00:00
|
|
|
#include "PosixSharedMemory.h"
|
|
|
|
#include "Bullet3Common/b3Logging.h"
|
|
|
|
#include "LinearMath/btScalar.h" //for btAssert
|
2017-01-06 17:49:03 +00:00
|
|
|
#include "LinearMath/btAlignedObjectArray.h"
|
2015-05-29 00:49:55 +00:00
|
|
|
|
2016-09-24 04:59:38 +00:00
|
|
|
//Windows implementation is in Win32SharedMemory.cpp
|
2015-05-31 03:32:29 +00:00
|
|
|
#ifndef _WIN32
|
2015-05-29 00:49:55 +00:00
|
|
|
#define TEST_SHARED_MEMORY
|
2015-05-31 03:32:29 +00:00
|
|
|
#endif//_WIN32
|
2015-05-28 23:05:24 +00:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#ifdef TEST_SHARED_MEMORY
|
|
|
|
|
|
|
|
#include <sys/shm.h>
|
|
|
|
#include <sys/ipc.h>
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-01-06 17:49:03 +00:00
|
|
|
struct btSharedMemorySegment
|
|
|
|
{
|
|
|
|
int m_key;
|
|
|
|
int m_sharedMemoryId;
|
|
|
|
void* m_sharedMemoryPtr;
|
|
|
|
bool m_createdSharedMemory;
|
|
|
|
|
|
|
|
btSharedMemorySegment()
|
|
|
|
: m_sharedMemoryId(-1),
|
|
|
|
m_sharedMemoryPtr(0),
|
|
|
|
m_createdSharedMemory(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-05-28 23:05:24 +00:00
|
|
|
struct PosixSharedMemoryInteralData
|
|
|
|
{
|
2017-01-06 17:49:03 +00:00
|
|
|
btAlignedObjectArray<btSharedMemorySegment> m_segments;
|
|
|
|
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
PosixSharedMemoryInteralData()
|
|
|
|
{
|
|
|
|
}
|
2015-05-28 23:05:24 +00:00
|
|
|
};
|
2015-08-07 07:13:26 +00:00
|
|
|
|
2015-05-28 23:05:24 +00:00
|
|
|
PosixSharedMemory::PosixSharedMemory()
|
|
|
|
{
|
|
|
|
|
|
|
|
m_internalData = new PosixSharedMemoryInteralData;
|
|
|
|
}
|
|
|
|
|
|
|
|
PosixSharedMemory::~PosixSharedMemory()
|
|
|
|
{
|
|
|
|
delete m_internalData;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct btPointerCaster
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
void* ptr;
|
|
|
|
ptrdiff_t integer;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-08-06 18:59:31 +00:00
|
|
|
void* PosixSharedMemory::allocateSharedMemory(int key, int size, bool allowCreation)
|
2015-05-28 23:05:24 +00:00
|
|
|
{
|
|
|
|
#ifdef TEST_SHARED_MEMORY
|
2017-01-06 17:55:22 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
btSharedMemorySegment* 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_sharedMemoryPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:59:31 +00:00
|
|
|
int flags = (allowCreation ? IPC_CREAT : 0) | 0666;
|
2015-05-28 23:05:24 +00:00
|
|
|
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
|
|
|
|
{
|
2017-01-06 17:49:03 +00:00
|
|
|
btSharedMemorySegment seg;
|
|
|
|
seg.m_key = key;
|
|
|
|
seg.m_createdSharedMemory = allowCreation;
|
|
|
|
seg.m_sharedMemoryId = id;
|
|
|
|
seg.m_sharedMemoryPtr = result.ptr;
|
|
|
|
m_internalData->m_segments.push_back(seg);
|
2015-05-28 23:05:24 +00:00
|
|
|
return result.ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
//not implemented yet
|
|
|
|
btAssert(0);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void PosixSharedMemory::releaseSharedMemory(int key, int size)
|
|
|
|
{
|
|
|
|
#ifdef TEST_SHARED_MEMORY
|
2017-01-06 17:49:03 +00:00
|
|
|
|
|
|
|
btSharedMemorySegment* 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 (0==seg)
|
|
|
|
{
|
|
|
|
b3Error("PosixSharedMemory::releaseSharedMemory: shared memory key not found");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seg->m_sharedMemoryId < 0)
|
2015-05-28 23:05:24 +00:00
|
|
|
{
|
2015-09-07 09:59:59 +00:00
|
|
|
b3Error("PosixSharedMemory::releaseSharedMemory: shared memory id is not set");
|
2015-05-28 23:05:24 +00:00
|
|
|
} else
|
|
|
|
{
|
2017-01-06 17:49:03 +00:00
|
|
|
if (seg->m_createdSharedMemory)
|
2015-08-07 07:13:26 +00:00
|
|
|
{
|
2017-01-06 17:49:03 +00:00
|
|
|
int result = shmctl(seg->m_sharedMemoryId,IPC_RMID,0);
|
2015-08-07 07:13:26 +00:00
|
|
|
if (result == -1)
|
|
|
|
{
|
|
|
|
b3Error("PosixSharedMemory::releaseSharedMemory: shmat returned -1");
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
b3Printf("PosixSharedMemory::releaseSharedMemory removed shared memory");
|
|
|
|
}
|
2017-01-06 17:49:03 +00:00
|
|
|
seg->m_createdSharedMemory = false;
|
|
|
|
seg->m_sharedMemoryId = -1;
|
2015-09-07 09:59:59 +00:00
|
|
|
}
|
2017-01-06 17:49:03 +00:00
|
|
|
if (seg->m_sharedMemoryPtr)
|
2015-08-07 07:13:26 +00:00
|
|
|
{
|
2017-01-06 17:49:03 +00:00
|
|
|
shmdt(seg->m_sharedMemoryPtr);
|
|
|
|
seg->m_sharedMemoryPtr = 0;
|
2015-08-07 07:13:26 +00:00
|
|
|
b3Printf("PosixSharedMemory::releaseSharedMemory detached shared memory\n");
|
|
|
|
}
|
2015-05-28 23:05:24 +00:00
|
|
|
}
|
2017-01-06 17:49:03 +00:00
|
|
|
|
|
|
|
m_internalData->m_segments.removeAtIndex(i);
|
|
|
|
|
2015-05-28 23:05:24 +00:00
|
|
|
#endif
|
2015-05-31 03:32:29 +00:00
|
|
|
}
|