10 #if defined(AJA_LINUX) || defined(AJA_MAC)
17 #include <sys/types.h>
20 #elif defined(MSWindows)
22 #elif defined(AJA_BAREMETAL)
34 #if defined(AJA_WINDOWS)
46 #if defined(AJA_WINDOWS)
82 #if defined(AJA_WINDOWS)
83 pMemory = malloc(memorySize);
85 pMemory = malloc(memorySize);
106 #if defined(AJA_WINDOWS)
123 void* pMemory =
NULL;
126 #if defined(AJA_WINDOWS)
127 pMemory = _aligned_malloc(size, alignment);
128 #elif defined(AJA_BAREMETAL)
129 pMemory = memalign(alignment, size);
131 if (posix_memalign(&pMemory, alignment, size))
159 #if defined(AJA_WINDOWS)
160 _aligned_free(pMemory);
174 if (pMemorySize ==
NULL)
180 if (*pMemorySize == 0)
186 if (pShareName ==
NULL)
192 if (*pShareName ==
'\0')
199 size_t sizeInBytes = (*pMemorySize + AJA_PAGE_SIZE - 1) / AJA_PAGE_SIZE * AJA_PAGE_SIZE;
202 #if defined(AJA_WINDOWS)
206 #elif defined(AJA_LINUX)
211 #elif defined(AJA_BAREMETAL)
219 std::list<SharedData>::iterator shareIter;
223 if (name == shareIter->shareName)
226 shareIter->refCount++;
229 *pMemorySize = shareIter->memorySize;
230 return shareIter->pMemory;
237 #if defined(AJA_WINDOWS)
241 SECURITY_DESCRIPTOR sd;
242 BOOL rv = InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
245 rv = SetSecurityDescriptorDacl(&sd,
251 SECURITY_ATTRIBUTES sa;
252 sa.nLength =
sizeof (SECURITY_ATTRIBUTES);
253 sa.lpSecurityDescriptor = &sd;
254 sa.bInheritHandle = FALSE;
256 &sa, PAGE_READWRITE, 0, (DWORD)sizeInBytes, name.c_str());
260 DWORD err = GetLastError();
262 "AJAMemory::AllocateShared SetSecurityDescriptorDacl failed (err=%d)", err);
267 DWORD err = GetLastError();
269 "AJAMemory::AllocateShared InitializeSecurityDescriptor failed (err=%d)", err);
276 NULL, PAGE_READWRITE, 0, (DWORD)sizeInBytes, name.c_str());
279 if (newData.fileMapHandle ==
NULL)
281 DWORD err = GetLastError();
285 newData.
pMemory = MapViewOfFile(newData.fileMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, sizeInBytes);
294 #elif defined(AJA_BAREMETAL)
300 newData.
fileDescriptor = shm_open (name.c_str(), O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
303 syslog(LOG_ERR,
"AJAMemory::AllocateShared -- shm_open failed");
307 bool needsTruncate =
false;
308 #if defined(AJA_LINUX)
309 needsTruncate =
true;
311 fchmod (newData.
fileDescriptor, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
318 if (mapstat.st_size == 0)
319 needsTruncate =
true;
320 else if (
size_t(mapstat.st_size) < sizeInBytes)
324 sizeInBytes = mapstat.st_size;
334 syslog(LOG_ERR,
"AJAMemory::AllocateShared -- ftruncate failed\n");
338 if (newData.
pMemory == MAP_FAILED)
340 std::ostringstream oss; oss <<
"AJAMemory::AllocateShared: 'mmap' failed, '" << name <<
"' fd=" << newData.
fileDescriptor
341 <<
" size=" << sizeInBytes <<
" trunc=" << (needsTruncate?
"Y":
"N") <<
" errno=" << errno <<
" -- " << strerror(errno);
342 syslog(LOG_ERR,
"%s\n", oss.str().c_str());
349 #endif // AJA_LINUX || AJA_MAC
360 *pMemorySize = sizeInBytes;
371 std::list<SharedData>::iterator shareIter;
374 if (pMemory == shareIter->pMemory)
376 shareIter->refCount--;
377 if (shareIter->refCount <= 0)
379 #if defined(AJA_WINDOWS)
380 UnmapViewOfFile(shareIter->pMemory);
381 CloseHandle(shareIter->fileMapHandle);
382 #elif defined(AJA_BAREMETAL)
385 munmap(shareIter->pMemory, shareIter->memorySize);
386 close(shareIter->fileDescriptor);