mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
sharedMemSegment.hpp
Go to the documentation of this file.
1 /** \file sharedMemSegment.hpp
2  * \author Jared R. Males (jaredmales@gmail.com)
3  * \brief Declarations for the mxlib shared memory facility
4  * \ingroup IPC_sharedmem
5  * \ingroup IPC
6  *
7 */
8 
9 //***********************************************************************//
10 // Copyright 2015, 2016, 2017, 2018, 2021 Jared R. Males (jaredmales@gmail.com)
11 //
12 // This file is part of mxlib.
13 //
14 // mxlib is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 // mxlib is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with mxlib. If not, see <http://www.gnu.org/licenses/>.
26 //***********************************************************************//
27 
28 #ifndef ipc_sharedMemSegment_hpp
29 #define ipc_sharedMemSegment_hpp
30 
31 #include <sys/shm.h>
32 #include <stdint.h>
33 #include <stdio.h>
34 
35 #include "ipc.hpp"
36 
37 namespace mx
38 {
39 namespace ipc
40 {
41 
42 /** \addtogroup IPC_sharedmem
43  * @{
44  */
45 
46 /// A c++ class to manage a System V shared memory segment with memory mapping
47 /** The associated functions create and/or attach to a shared memory segment. If a segment is created, a block of
48  * size sizeof(uintptr_t) is reserved where the address is stored. This address is used when subsequently
49  * attaching to the segment so that pointers stored in the block are valid, etc.
50  *
51  */
53 {
54 public:
55  ///The path to use for key creation
56  char key_path[MX_IPC_KEYLEN];
57 
58  ///The id to use for key creation
59  int key_id;
60 
61  ///The shared memory key
62  key_t key;
63 
64  ///The shared memory id associated with the key
65  int shmemid;
66 
67  ///The base address of the segment
68  void * addr;
69 
70  ///The size of the segment
71  size_t size;
72 
73  ///Flag indicating whether or not the segment is attached
74  int attached;
75 
76 public:
77 
78  ///Initialize the class
79  void initialize();
80 
81  ///Set the key
82  /** If path == 0, then the key is set directly to id, without using ftok.
83  *
84  * \param path is the full path to use in a call to ftok
85  * \param id is the id number to use in a call to ftok
86  *
87  * \returns the key value, which is also set in the msgq
88  */
89  key_t setKey( const char * path,
90  const int id
91  );
92 
93  ///Create and attach to the segment
94  /** A segment of size = sz + sizeof(uintptr_t) is actually created.
95  *
96  * \note key must be set before calling this function.
97  *
98  * \param sz the size of the segment to create
99  *
100  */
101  int create(size_t sz);
102 
103  ///Attach to a segment without creating it.
104  /** If donot_set_addr == false, then the address is mapped to match that stored in the first uintptr_t of the segment.
105  *
106  * \param donot_set_addr if > 0 (true) then the address is not mapped to match that stored in the segment
107  */
108  int attach( bool donot_set_addr = false);
109 
110  ///Detach from the segment
111  /**
112  *
113  */
114  int detach();
115 
116 };
117 
118 /// @}
119 
120 }//namespace ipc
121 }//namespace mx
122 
123 #endif //ipc_sharedMemSegment_hpp
124 
A c++ class to manage a System V shared memory segment with memory mapping.
void initialize()
Initialize the class.
int shmemid
The shared memory id associated with the key.
int create(size_t sz)
Create and attach to the segment.
int detach()
Detach from the segment.
int key_id
The id to use for key creation.
size_t size
The size of the segment.
int attach(bool donot_set_addr=false)
Attach to a segment without creating it.
void * addr
The base address of the segment.
key_t key
The shared memory key.
int attached
Flag indicating whether or not the segment is attached.
char key_path[MX_IPC_KEYLEN]
The path to use for key creation.
key_t setKey(const char *path, const int id)
Set the key.
Declarations for the mxlib interprocess communication (IPC) tools.
The mxlib c++ namespace.
Definition: mxError.hpp:107