File: SharedMemoryAllocator.hpp

package info (click to toggle)
pbseqlib 5.3.5%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,020 kB
  • sloc: cpp: 77,250; python: 331; sh: 103; makefile: 41
file content (34 lines) | stat: -rw-r--r-- 1,089 bytes parent folder | download | duplicates (4)
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_