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
|
#ifndef _BLASR_SHARED_MEMORY_ALLOCATOR_HPP_
#define _BLASR_SHARED_MEMORY_ALLOCATOR_HPP_
#include <cerrno>
#include <iostream>
#include <string>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
template <typename T_Data>
int AllocateMappedShare(std::string &handle, int dataLength, T_Data *&dataPtr, int &shmId)
{
std::cout << "opening shm" << std::endl;
shmId = shm_open(handle.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
if (ftruncate(shmId, sizeof(T_Data) * dataLength) == -1) {
std::cout << " ftruncate error: " << errno << std::endl;
}
std::cout << "done truncating." << std::endl;
dataPtr = (T_Data *)mmap(NULL, sizeof(T_Data) * dataLength, PROT_READ | PROT_WRITE, MAP_SHARED,
shmId, 0);
if (dataPtr == MAP_FAILED) {
//
// Handle this better later on.
//
std::cout << "ERROR, MEMORY MAP FAILED." << std::endl;
std::exit(EXIT_FAILURE);
}
std::cout << "done mapping." << std::endl;
return dataLength;
}
#endif // _BLASR_SHARED_MEMORY_ALLOCATOR_HPP_
|