Fawkes API  Fawkes Development Version
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 
34 {
35 public:
37  {
38  }
39  virtual bool matches(void *memptr) = 0;
40  virtual size_t size() = 0;
41  virtual void initialize(void *memptr) = 0;
42  virtual void set(void *memptr) = 0;
43  virtual void reset() = 0;
44  virtual size_t data_size() = 0;
45  virtual SharedMemoryHeader *clone() const = 0;
46  virtual bool operator==(const SharedMemoryHeader &s) const = 0;
47 };
48 
49 class SharedMemoryLister;
50 class SemaphoreSet;
51 
53 {
54 public:
55  static const unsigned int MagicTokenSize;
56  static const short MaxNumConcurrentReaders;
57 
58  SharedMemory(const char * magic_token,
59  SharedMemoryHeader *header,
60  bool is_read_only,
61  bool create,
62  bool destroy_on_delete,
63  const char * registry_name = 0);
64 
65  SharedMemory(const SharedMemory &s);
66 
67  virtual ~SharedMemory();
68 
70 
71  bool is_read_only() const;
72  bool is_destroyed() const;
73  bool is_swapable() const;
74  bool is_valid() const;
75  bool is_creator() const;
76  bool is_protected() const;
77  void * memptr() const;
78  size_t data_size() const;
79  int shmem_id() const;
80  unsigned int num_attached() const;
81 
82  void set(void *memptr);
83  void set_destroy_on_delete(bool destroy);
84  void add_semaphore();
85  void set_swapable(bool swapable);
86 
87  void lock_for_read();
88  bool try_lock_for_read();
89  void lock_for_write();
90  bool try_lock_for_write();
91  void unlock();
92 
93  void *ptr(void *addr) const;
94  void *addr(void *ptr) const;
95 
96  static void list(const char * magic_token,
97  SharedMemoryHeader *header,
98  SharedMemoryLister *lister,
99  const char * registry_name = 0);
100 
101  static void erase(const char * magic_token,
102  SharedMemoryHeader *header,
103  SharedMemoryLister *lister = 0,
104  const char * registry_name = 0);
105 
106  static void erase_orphaned(const char * magic_token,
107  SharedMemoryHeader *header,
108  SharedMemoryLister *lister = 0,
109  const char * registry_name = 0);
110 
111  static bool
112  exists(const char *magic_token, SharedMemoryHeader *header, const char *registry_name = 0);
113 
114  static bool is_destroyed(int shm_id);
115  static bool is_swapable(int shm_id);
116  static unsigned int num_attached(int shm_id);
117 
119  {
120  public:
123  SharedMemoryIterator(std::list<SharedMemoryRegistry::SharedMemID> ids,
124  SharedMemoryHeader * header);
126 
127  SharedMemoryIterator & operator++(); // prefix
128  SharedMemoryIterator operator++(int inc); // postfix
129  SharedMemoryIterator & operator+(unsigned int i);
130  SharedMemoryIterator & operator+=(unsigned int i);
131  bool operator==(const SharedMemoryIterator &s) const;
132  bool operator!=(const SharedMemoryIterator &s) const;
133  const SharedMemoryHeader *operator*() const;
135 
136  const char *magic_token() const;
137  int shmid() const;
138  int semaphore() const;
139  size_t segmsize() const;
140  size_t segmnattch() const;
141  void * databuf() const;
142 
143  private:
144  void attach();
145  void reset();
146 
147  bool initialized_;
148  std::list<SharedMemoryRegistry::SharedMemID> ids_;
149  std::list<SharedMemoryRegistry::SharedMemID>::iterator id_it_;
150  int cur_shmid_;
151  SharedMemoryHeader * header_;
152  void * shm_buf_;
153  void * data_buf_;
154  int semaphore_;
155  size_t segmsize_;
156  size_t segmnattch_;
157  };
158 
159  static SharedMemoryIterator
160  find(const char *magic_token, SharedMemoryHeader *header, const char *registry_name = 0);
161  static SharedMemoryIterator end();
162 
163 protected:
164  /** General header.
165  * This header is stored right after the magic token.
166  */
167  typedef struct
168  {
169  void *shm_addr; /**< Desired shared memory address */
170  int semaphore; /**< Semaphore set ID */
172 
173  SharedMemory(const char *magic_token,
174  bool is_read_only,
175  bool create,
176  bool destroy_on_delete,
177  const char *registry_name = 0);
178 
179  void attach();
180  void free();
181 
182  void * _memptr;
183  size_t _mem_size;
184  size_t _data_size;
189  char * _magic_token;
193  long unsigned int _shm_offset;
194 
195 private:
196  SharedMemoryRegistry *shm_registry_;
197  char * registry_name_;
198 
199  void *shared_mem_;
200  int shared_mem_id_;
201  void *shared_mem_upper_bound_;
202 
203  bool created_;
204  SemaphoreSet *semset_;
205 
206  bool lock_aquired_;
207  bool write_lock_aquired_;
208 };
209 
210 } // end namespace fawkes
211 
212 #endif
static void erase_orphaned(const char *magic_token, SharedMemoryHeader *header, SharedMemoryLister *lister=0, const char *registry_name=0)
Erase orphaned (attach count = 0) shared memory segments of a given type.
Definition: shm.cpp:1199
IPC semaphore set.
Definition: semset.h:31
virtual void set(void *memptr)=0
Set information from memptr.
virtual size_t data_size()=0
Return the size of the data.
bool is_protected() const
Check if memory segment is protected.
Definition: shm.cpp:827
void lock_for_write()
Lock shared memory segment for writing.
Definition: shm.cpp:959
SharedMemory(const char *magic_token, SharedMemoryHeader *header, bool is_read_only, bool create, bool destroy_on_delete, const char *registry_name=0)
Create a new shared memory segment.
Definition: shm.cpp:348
Fawkes library namespace.
size_t _mem_size
Total size of the segment, including headers.
Definition: shm.h:183
bool _should_create
Create shared memory segment.
Definition: shm.h:188
Shared Memory iterator.
Definition: shm.h:118
bool try_lock_for_write()
Try to aquire lock on shared memory segment for writing.
Definition: shm.cpp:993
size_t _data_size
Size of the data segment only.
Definition: shm.h:184
SharedMemory_header_t * _shm_header
general header as stored in the shared memory segment
Definition: shm.h:191
virtual ~SharedMemoryHeader()
Virtual destructor.
Definition: shm.h:36
Shared memory registry.
Definition: shm_registry.h:37
void * databuf() const
Get pointer to data buffer.
Definition: shm.cpp:1626
virtual bool matches(void *memptr)=0
Method to check if the given memptr matches this header.
bool is_swapable() const
Check if memory can be swapped out.
Definition: shm.cpp:798
void * _shm_upper_bound
Upper bound of memory.
Definition: shm.h:192
bool is_creator() const
Determine if the shared memory segment has been created by this instance.
Definition: shm.cpp:722
void lock_for_read()
Lock shared memory segment for reading.
Definition: shm.cpp:909
bool try_lock_for_read()
Try to aquire lock on shared memory segment for reading.
Definition: shm.cpp:938
void * addr(void *ptr) const
Get an address from a real pointer.
Definition: shm.cpp:690
size_t segmsize() const
Get segment size.
Definition: shm.cpp:1608
int semaphore
Semaphore set ID.
Definition: shm.h:170
bool is_valid() const
Check validity of shared memory segment.
Definition: shm.cpp:811
char * _magic_token
Magic token.
Definition: shm.h:189
char * _shm_magic_token
Magic token as stored in the shared memory segment.
Definition: shm.h:190
virtual bool operator==(const SharedMemoryHeader &s) const =0
Check for equality of headers.
static SharedMemoryIterator end()
Get invalid iterator.
Definition: shm.cpp:1283
bool _destroy_on_delete
destroy on delete.
Definition: shm.h:187
void set_swapable(bool swapable)
Set shared memory swapable.
Definition: shm.cpp:890
static void erase(const char *magic_token, SharedMemoryHeader *header, SharedMemoryLister *lister=0, const char *registry_name=0)
Erase shared memory segments of a given type.
Definition: shm.cpp:1151
SharedMemory & operator=(const SharedMemory &s)
Assignment operator.
Definition: shm.cpp:421
size_t segmnattch() const
Get number of attached parties.
Definition: shm.cpp:1617
bool is_destroyed() const
Check if segment has been destroyed This can be used if the segment has been destroyed.
Definition: shm.cpp:788
bool is_read_only() const
Check for read-only mode.
Definition: shm.cpp:706
SharedMemoryIterator & operator++()
Prefix increment.
Definition: shm.cpp:1428
void free()
Detach from and maybe destroy the shared memory segment.
Definition: shm.cpp:486
void attach()
Attach to the shared memory segment.
Definition: shm.cpp:512
virtual void initialize(void *memptr)=0
Initialize the header.
void * shm_addr
Desired shared memory address.
Definition: shm.h:169
void * ptr(void *addr) const
Get the real pointer to the data based on an address.
Definition: shm.cpp:659
static bool exists(const char *magic_token, SharedMemoryHeader *header, const char *registry_name=0)
Check if a specific shared memory segment exists.
Definition: shm.cpp:1255
void * memptr() const
Get a pointer to the shared memory This method returns a pointer to the data-segment of the shared me...
Definition: shm.cpp:734
unsigned int num_attached() const
Get number of attached processes.
Definition: shm.cpp:763
virtual SharedMemoryHeader * clone() const =0
Clone this shared memory header.
void * _memptr
Pointer to the data segment.
Definition: shm.h:182
void set_destroy_on_delete(bool destroy)
Set deletion behaviour.
Definition: shm.cpp:839
virtual ~SharedMemory()
Destructor.
Definition: shm.cpp:397
Format list output for shared memory segments.
Definition: shm_lister.h:37
const SharedMemoryHeader * operator*() const
Get SharedMemoryHeader.
Definition: shm.cpp:1535
int semaphore() const
Get semaphore.
Definition: shm.cpp:1599
SharedMemoryIterator & operator=(const SharedMemoryIterator &shmit)
Make this instance point to the same segment as shmit.
Definition: shm.cpp:1545
void add_semaphore()
Add semaphore to shared memory segment.
Definition: shm.cpp:852
Shared memory segment.
Definition: shm.h:52
static SharedMemoryIterator find(const char *magic_token, SharedMemoryHeader *header, const char *registry_name=0)
Find SharedMemory segments.
Definition: shm.cpp:1268
static const short MaxNumConcurrentReaders
Maximum number of concurrent readers.
Definition: shm.h:56
bool operator==(const SharedMemoryIterator &s) const
Check iterators for equality.
Definition: shm.cpp:1517
bool _is_read_only
Read-only.
Definition: shm.h:186
virtual size_t size()=0
Size of the header.
virtual void reset()=0
Reset information previously set with set().
bool operator!=(const SharedMemoryIterator &s) const
Check iterators for inequality.
Definition: shm.cpp:1527
size_t data_size() const
Get the size of the data-segment.
Definition: shm.cpp:745
SharedMemoryHeader * _header
Data-specific header.
Definition: shm.h:185
static const unsigned int MagicTokenSize
The magic token size.
Definition: shm.h:55
int shmid() const
Get shared memory ID.
Definition: shm.cpp:1590
int shmem_id() const
Get shared memory ID.
Definition: shm.cpp:754
void set(void *memptr)
Copies data from the memptr to shared memory.
Definition: shm.cpp:774
SharedMemoryIterator & operator+(unsigned int i)
Advance by i steps.
Definition: shm.cpp:1491
long unsigned int _shm_offset
Offset to the master's base addr.
Definition: shm.h:193
static void list(const char *magic_token, SharedMemoryHeader *header, SharedMemoryLister *lister, const char *registry_name=0)
List shared memory segments of a given type.
Definition: shm.cpp:1116
SharedMemoryIterator & operator+=(unsigned int i)
Advance by i steps.
Definition: shm.cpp:1504
Interface for shared memory header.
Definition: shm.h:33
void unlock()
Unlock memory.
Definition: shm.cpp:1025
const char * magic_token() const
Get magic token.
Definition: shm.cpp:1577