File: Win32SharedMemory.cpp

package info (click to toggle)
bullet 2.83.7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,772 kB
  • sloc: cpp: 355,312; lisp: 12,087; ansic: 11,969; python: 644; makefile: 116; xml: 27
file content (110 lines) | stat: -rw-r--r-- 2,653 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#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, bool allowCreation)
{
	b3Assert(m_internalData->m_buf==0);

	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)
	{
		 if (allowCreation)
		 {
			  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
		 {
			   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);
		m_internalData->m_hMapFile = 0;
	}

}

Win32SharedMemoryServer::Win32SharedMemoryServer()
{
}
Win32SharedMemoryServer::~Win32SharedMemoryServer()
{
}

Win32SharedMemoryClient::Win32SharedMemoryClient()
{
}
Win32SharedMemoryClient:: ~Win32SharedMemoryClient()
{
}

#endif //_WIN32