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
#include "MemObject.hpp"
#include <iostream>
#include <fcntl.h>

MemObject
get_memobject()
{
   std::string filename;

   std::cout << "Input file: ";
   getline(std::cin, filename);

   int fd = open(filename.c_str(), O_RDWR);
   if (fd < 0) {
       exit(1);
   }

   off_t off;
   std::cout << "Offset: "; std::cin >> off;

   size_t size;
   std::cout << "Size: "; std::cin >> size;

   MemObject words{fd, off, size, PROT_READ|PROT_WRITE, MAP_SHARED};
   return words;
}

char hallo[] = "Hallo";

int
main()
{
    try {
        MemObject words = get_memobject();
        std::cout << "Access position: ";
        size_t pos;
        std::cin >> pos;
        char*& cptr{words.access<char*>(pos)};
        if (cptr) {
            std::cout << (long int) cptr << std::endl;
            std::cout << cptr << std::endl;
        }
        cptr = hallo;
    }
    catch (const MemObject::Exception& exc) {
        std::cout << "got " << exc.what() << std::endl;
    }
}