3

I am converting some C code for a Raspberry Pi 3B to C++. This portion of the C code,

   // Open /dev/mem
   if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
      printf("Failed to open /dev/mem, try checking permissions.\n");
      return -1;
   }

   p->map = mmap(
      NULL,
      BLOCK_SIZE,
      PROT_READ|PROT_WRITE,
      MAP_SHARED,
      p->mem_fd,      // File descriptor to physical memory virtual file '/dev/mem'
      p->addr_p       // Address in physical map that we want this memory block to expose
   );

presents a challenge. The immediate issue is with the file descriptor returned by the C open function. C++ uses fstream, which will work for opening the file but when I get to the C function mmap I do not have a file descriptor.

mmap maps files into memory.

This is a learning experiment and I would like to stick to C++.

It does bring to mind a question. The RPi OS, Raspbian, has built-in all these C functions. Does it also have built-in the C++ equivalents or is it expected that one use the C functions and perhaps an extern "C" statement?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
user34299
  • 377
  • 2
  • 11
  • C++ is almost a strict superset of C (see [here](http://stackoverflow.com/questions/1201593/c-subset-of-c-where-not-examples)). While you could convert this to C++, it can also stay as C. – Eli Sadoff Dec 26 '16 at 22:47
  • @EliSadoff: This is wrong! C++ is not even "almost" a strict superset of C. There are a lot of commonly used ideoms which have different semantics in both languages. This might have been different before standard C and modern C99, but it is plain wrong since 17 years now. – too honest for this site Dec 26 '16 at 22:50
  • 1
    @Olaf I explicitly linked to a (non-exhaustive) list of times where that is not true. "Almost" is a subjective definition, and for the purposes of this question, this code is valid in both C and C++. – Eli Sadoff Dec 26 '16 at 22:51
  • @EliSadoff: This will be read by beginners who commonly interpret "almost" that identical syntax has identical semantics. This is not true for C and C++. I'm fine if you say "the code can be used as-is in C++ (as long as the other problems are solved, e.g. file-descriptor). But that does not justify such a missleading and too general comment. – too honest for this site Dec 26 '16 at 22:55
  • @Olaf I don't think that I implied anywhere that identical syntax has identical semantics. In retrospect I could have further clarified by putting up a more stark disclaimer, but I think that on the whole my comment was not particularly irresponsible or misleading. – Eli Sadoff Dec 26 '16 at 22:57
  • 2
    `open()` is not a C-specific function, it's a POSIX function that works the same from both C and C++. The same with `mmap()`. There's no need to recode this when switching from C to C++. – Barmar Dec 26 '16 at 22:58
  • @EliSadoff: Read my previous comment carefully again. As you stated, "almost" is a matter of interpretation. I wrote how it most times is interpreted by beginners from long tme experience. In general one should avoid such imprecision; especially if there is not need. – too honest for this site Dec 26 '16 at 23:01
  • @Barmar: It uses C calling conventions. But I agree the headers should handle this transparently. – too honest for this site Dec 26 '16 at 23:01
  • Using the mmap api does not reclassify your code as C nor C++. Both can invoke the api. – 2785528 Dec 27 '16 at 03:58
  • I think you might be asking the wrong question. DId you mean to ask how to convert a "stream" to / from a "file descriptor"? (On vxWorks, this was trivial and convenient.) Perhaps his would be a worthwhile research effort. – 2785528 Dec 27 '16 at 04:00

1 Answers1

0

You can achive what you want with boost::interptocess

http://www.boost.org/doc/libs/1_57_0/doc/html/interprocess/sharedmemorybetweenprocesses.html#interprocess.sharedmemorybetweenprocesses.mapped_file

I curious what's your problem with mmap in c++ code?

paweldac
  • 1,144
  • 6
  • 11
  • paweldac, The `mmap` function works well. However, I was under the perhaps mistaken impression that it is a C construct and inappropriate to use in a C++ application. I am not trying to be a purest, but rather trying to be consistent with C++ as I learn the same on the RPi. My understanding is that UNIX / Linux is written in C. My confusion comes re: mixing C and C++. The note from Barmar about `open()` and `mmap()` not being C-specific but POSIX functions is interesting in that the `mmap()` function worked in the C++ code **without** having to use an `extern "C"` qualifier. – user34299 Dec 27 '16 at 00:21
  • @user34299 mmap is not C specific, it's *nix specific. In C++ calls like mmap should be hidden from user in nice named object or function if class is not needed. Proposed library can map memory into memory in more 'C++ style', but somewhere in implementation it's also using mmap or equivalent on non-poxis sysytem(ex. windows) – paweldac Dec 27 '16 at 08:21