Loading [MathJax]/extensions/tex2jax.js
mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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
37namespace mx
38{
39namespace 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
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
75
76 public:
77 /// Initialize the class
78 void initialize();
79
80 /// Set the key
81 /** If path == 0, then the key is set directly to id, without using ftok.
82 *
83 * \param path is the full path to use in a call to ftok
84 * \param id is the id number to use in a call to ftok
85 *
86 * \returns the key value, which is also set in the msgq
87 */
88 key_t setKey( const char *path, const int id );
89
90 /// Create and attach to the segment
91 /** A segment of size = sz + sizeof(uintptr_t) is actually created.
92 *
93 * \note key must be set before calling this function.
94 *
95 * \param sz the size of the segment to create
96 *
97 */
98 int create( size_t sz );
99
100 /// Attach to a segment without creating it.
101 /** If donot_set_addr == false, then the address is mapped to match that stored in the first uintptr_t of the
102 * segment.
103 *
104 * \param donot_set_addr if > 0 (true) then the address is not mapped to match that stored in the segment
105 */
106 int attach( bool donot_set_addr = false );
107
108 /// Detach from the segment
109 /**
110 *
111 */
112 int detach();
113};
114
115/// @}
116
117} // namespace ipc
118} // namespace mx
119
120#endif // ipc_sharedMemSegment_hpp
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:106