Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
All
Classes
Namespaces
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Groups
Pages
shm.h
1
2
/***************************************************************************
3
* shm.h - shared memory segment
4
*
5
* Created: Thu Jan 12 13:12:24 2006
6
* Copyright 2005-2011 Tim Niemueller [www.niemueller.de]
7
*
8
****************************************************************************/
9
10
/* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version. A runtime exception applies to
14
* this software (see LICENSE.GPL_WRE file mentioned below for details).
15
*
16
* This program is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU Library General Public License for more details.
20
*
21
* Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22
*/
23
24
#ifndef __UTILS_IPC_SHM_H_
25
#define __UTILS_IPC_SHM_H_
26
27
// for size_t
28
#include <sys/types.h>
29
#include <utils/ipc/shm_registry.h>
30
31
namespace
fawkes {
32
33
class
SharedMemoryHeader
{
34
public
:
35
virtual
~SharedMemoryHeader
() {}
36
virtual
bool
matches
(
void
*memptr) = 0;
37
virtual
size_t
size
() = 0;
38
virtual
void
initialize
(
void
*memptr) = 0;
39
virtual
void
set
(
void
*memptr) = 0;
40
virtual
void
reset
() = 0;
41
virtual
size_t
data_size
() = 0;
42
virtual
SharedMemoryHeader
*
clone
()
const
= 0;
43
virtual
bool
operator==
(
const
SharedMemoryHeader
&s)
const
= 0;
44
};
45
46
class
SharedMemoryLister;
47
class
SemaphoreSet;
48
49
class
SharedMemory
50
{
51
52
public
:
53
54
static
const
unsigned
int
MagicTokenSize
;
55
static
const
short
MaxNumConcurrentReaders
;
56
57
SharedMemory
(
const
char
*magic_token,
58
SharedMemoryHeader
*header,
59
bool
is_read_only
,
bool
create,
60
bool
destroy_on_delete,
61
const
char
*registry_name = 0);
62
63
SharedMemory
(
const
SharedMemory
&s);
64
65
virtual
~SharedMemory
();
66
67
bool
is_read_only
()
const
;
68
bool
is_destroyed
()
const
;
69
bool
is_swapable
()
const
;
70
bool
is_valid
()
const
;
71
bool
is_creator
()
const
;
72
bool
is_protected
()
const
;
73
void
*
memptr
()
const
;
74
size_t
data_size
()
const
;
75
int
shmem_id
()
const
;
76
unsigned
int
num_attached
()
const
;
77
78
void
set
(
void
*
memptr
);
79
void
set_destroy_on_delete
(
bool
destroy);
80
void
add_semaphore
();
81
void
set_swapable
(
bool
swapable);
82
83
void
lock_for_read
();
84
bool
try_lock_for_read
();
85
void
lock_for_write
();
86
bool
try_lock_for_write
();
87
void
unlock
();
88
89
void
*
ptr
(
void
*
addr
)
const
;
90
void
*
addr
(
void
*
ptr
)
const
;
91
92
static
void
list
(
const
char
*magic_token,
93
SharedMemoryHeader
*header,
SharedMemoryLister
*lister,
94
const
char
*registry_name = 0);
95
96
static
void
erase
(
const
char
*magic_token,
97
SharedMemoryHeader
*header,
98
SharedMemoryLister
*lister = 0,
99
const
char
*registry_name = 0);
100
101
static
void
erase_orphaned
(
const
char
*magic_token,
102
SharedMemoryHeader
*header,
103
SharedMemoryLister
*lister = 0,
104
const
char
*registry_name = 0);
105
106
static
bool
exists
(
const
char
*magic_token,
107
SharedMemoryHeader
*header,
108
const
char
*registry_name = 0);
109
110
static
bool
is_destroyed
(
int
shm_id);
111
static
bool
is_swapable
(
int
shm_id);
112
static
unsigned
int
num_attached
(
int
shm_id);
113
114
class
SharedMemoryIterator
115
{
116
public
:
117
SharedMemoryIterator
();
118
SharedMemoryIterator
(
const
SharedMemoryIterator
&shmit);
119
SharedMemoryIterator
(std::list<SharedMemoryRegistry::SharedMemID> ids,
120
SharedMemoryHeader
*header);
121
~SharedMemoryIterator
();
122
123
SharedMemoryIterator
&
operator++
();
// prefix
124
SharedMemoryIterator
operator++
(
int
inc);
// postfix
125
SharedMemoryIterator
&
operator+
(
unsigned
int
i);
126
SharedMemoryIterator
&
operator+=
(
unsigned
int
i);
127
bool
operator==
(
const
SharedMemoryIterator
& s)
const
;
128
bool
operator!=
(
const
SharedMemoryIterator
& s)
const
;
129
const
SharedMemoryHeader
*
operator*
()
const
;
130
SharedMemoryIterator
&
operator=
(
const
SharedMemoryIterator
& shmit);
131
132
const
char
*
magic_token
()
const
;
133
int
shmid
()
const
;
134
int
semaphore
()
const
;
135
size_t
segmsize
()
const
;
136
size_t
segmnattch
()
const
;
137
void
*
databuf
()
const
;
138
139
private
:
140
void
attach();
141
void
reset();
142
143
bool
__initialized;
144
char
*__magic_token;
145
std::list<SharedMemoryRegistry::SharedMemID> __ids;
146
std::list<SharedMemoryRegistry::SharedMemID>::iterator __id_it;
147
int
__cur_shmid;
148
SharedMemoryHeader
*__header;
149
void
*__shm_buf;
150
void
*__data_buf;
151
int
__semaphore;
152
size_t
__segmsize;
153
size_t
__segmnattch;
154
};
155
156
static
SharedMemoryIterator
find
(
const
char
*magic_token,
157
SharedMemoryHeader
*header,
158
const
char
*registry_name = 0);
159
static
SharedMemoryIterator
end
();
160
161
protected
:
162
163
/** General header.
164
* This header is stored right after the magic token.
165
*/
166
typedef
struct
{
167
void
*
shm_addr
;
/**< Desired shared memory address */
168
int
semaphore
;
/**< Semaphore set ID */
169
}
SharedMemory_header_t
;
170
171
SharedMemory
(
const
char
*magic_token,
172
bool
is_read_only
,
bool
create,
bool
destroy_on_delete,
173
const
char
*registry_name = 0);
174
175
void
attach
();
176
void
free
();
177
178
void
*
_memptr
;
179
size_t
_mem_size
;
180
size_t
_data_size
;
181
SharedMemoryHeader
*
_header
;
182
bool
_is_read_only
;
183
bool
_destroy_on_delete
;
184
bool
_should_create
;
185
char
*
_magic_token
;
186
char
*
_shm_magic_token
;
187
SharedMemory_header_t
*
_shm_header
;
188
void
*
_shm_upper_bound
;
189
long
unsigned
int
_shm_offset
;
190
191
192
private
:
193
SharedMemoryRegistry
*__shm_registry;
194
char
* __registry_name;
195
196
void
*__shared_mem;
197
int
__shared_mem_id;
198
void
*__shared_mem_upper_bound;
199
200
bool
__created;
201
SemaphoreSet
*__semset;
202
203
bool
__lock_aquired;
204
bool
__write_lock_aquired;
205
206
};
207
208
209
}
// end namespace fawkes
210
211
#endif
src
libs
utils
ipc
shm.h
Generated by
1.8.3.1