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  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 
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 
115  {
116  public:
119  SharedMemoryIterator(std::list<SharedMemoryRegistry::SharedMemID> ids,
120  SharedMemoryHeader *header);
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;
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 */
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;
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
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:1163
IPC semaphore set.
Definition: semset.h:32
virtual void set(void *memptr)=0
Set information from memptr.
virtual size_t data_size()=0
Return the size of the data.
bool is_creator() const
Determine if the shared memory segment has been created by this instance.
Definition: shm.cpp:670
void lock_for_write()
Lock shared memory segment for writing.
Definition: shm.cpp:918
size_t data_size() const
Get the size of the data-segment.
Definition: shm.cpp:694
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:352
Fawkes library namespace.
size_t _mem_size
Total size of the segment, including headers.
Definition: shm.h:179
bool _should_create
Create shared memory segment.
Definition: shm.h:184
Shared Memory iterator.
Definition: shm.h:114
bool try_lock_for_write()
Try to aquire lock on shared memory segment for writing.
Definition: shm.cpp:953
size_t _data_size
Size of the data segment only.
Definition: shm.h:180
SharedMemory_header_t * _shm_header
general header as stored in the shared memory segment
Definition: shm.h:187
virtual ~SharedMemoryHeader()
Virtual destructor.
Definition: shm.h:35
Shared memory registry.
Definition: shm_registry.h:39
virtual bool matches(void *memptr)=0
Method to check if the given memptr matches this header.
void * _shm_upper_bound
Upper bound of memory.
Definition: shm.h:188
size_t segmsize() const
Get segment size.
Definition: shm.cpp:1596
bool is_protected() const
Check if memory segment is protected.
Definition: shm.cpp:783
void lock_for_read()
Lock shared memory segment for reading.
Definition: shm.cpp:867
bool try_lock_for_read()
Try to aquire lock on shared memory segment for reading.
Definition: shm.cpp:897
int semaphore
Semaphore set ID.
Definition: shm.h:168
char * _magic_token
Magic token.
Definition: shm.h:185
char * _shm_magic_token
Magic token as stored in the shared memory segment.
Definition: shm.h:186
virtual bool operator==(const SharedMemoryHeader &s) const =0
Check for equality of headers.
static SharedMemoryIterator end()
Get invalid iterator.
Definition: shm.cpp:1254
bool _destroy_on_delete
destroy on delete.
Definition: shm.h:183
void * addr(void *ptr) const
Get an address from a real pointer.
Definition: shm.cpp:637
void set_swapable(bool swapable)
Set shared memory swapable.
Definition: shm.cpp:847
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:1115
void * ptr(void *addr) const
Get the real pointer to the data based on an address.
Definition: shm.cpp:606
SharedMemoryIterator & operator++()
Prefix increment.
Definition: shm.cpp:1404
bool is_read_only() const
Check for read-only mode.
Definition: shm.cpp:653
bool is_destroyed() const
Check if segment has been destroyed This can be used if the segment has been destroyed.
Definition: shm.cpp:741
void free()
Detach from and maybe destroy the shared memory segment.
Definition: shm.cpp:423
const SharedMemoryHeader * operator*() const
Get SharedMemoryHeader.
Definition: shm.cpp:1519
void attach()
Attach to the shared memory segment.
Definition: shm.cpp:450
const char * magic_token() const
Get magic token.
Definition: shm.cpp:1562
virtual void initialize(void *memptr)=0
Initialize the header.
bool operator==(const SharedMemoryIterator &s) const
Check iterators for equality.
Definition: shm.cpp:1498
void * shm_addr
Desired shared memory address.
Definition: shm.h:167
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:1221
virtual SharedMemoryHeader * clone() const =0
Clone this shared memory header.
void * _memptr
Pointer to the data segment.
Definition: shm.h:178
void set_destroy_on_delete(bool destroy)
Set deletion behaviour.
Definition: shm.cpp:796
virtual ~SharedMemory()
Destructor.
Definition: shm.cpp:400
Format list output for shared memory segments.
Definition: shm_lister.h:38
void * databuf() const
Get pointer to data buffer.
Definition: shm.cpp:1616
SharedMemoryIterator & operator=(const SharedMemoryIterator &shmit)
Make this instance point to the same segment as shmit.
Definition: shm.cpp:1530
void add_semaphore()
Add semaphore to shared memory segment.
Definition: shm.cpp:810
Shared memory segment.
Definition: shm.h:49
int shmid() const
Get shared memory ID.
Definition: shm.cpp:1576
unsigned int num_attached() const
Get number of attached processes.
Definition: shm.cpp:714
int semaphore() const
Get semaphore.
Definition: shm.cpp:1586
static SharedMemoryIterator find(const char *magic_token, SharedMemoryHeader *header, const char *registry_name=0)
Find SharedMemory segments.
Definition: shm.cpp:1237
static const short MaxNumConcurrentReaders
Maximum number of concurrent readers.
Definition: shm.h:55
bool _is_read_only
Read-only.
Definition: shm.h:182
virtual size_t size()=0
Size of the header.
virtual void reset()=0
Reset information previously set with set().
bool is_swapable() const
Check if memory can be swapped out.
Definition: shm.cpp:752
SharedMemoryHeader * _header
Data-specific header.
Definition: shm.h:181
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:682
bool operator!=(const SharedMemoryIterator &s) const
Check iterators for inequality.
Definition: shm.cpp:1509
static const unsigned int MagicTokenSize
The magic token size.
Definition: shm.h:54
size_t segmnattch() const
Get number of attached parties.
Definition: shm.cpp:1606
void set(void *memptr)
Copies data from the memptr to shared memory.
Definition: shm.cpp:726
int shmem_id() const
Get shared memory ID.
Definition: shm.cpp:704
SharedMemoryIterator & operator+(unsigned int i)
Advance by i steps.
Definition: shm.cpp:1470
long unsigned int _shm_offset
Offset to the master's base addr.
Definition: shm.h:189
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:1079
SharedMemoryIterator & operator+=(unsigned int i)
Advance by i steps.
Definition: shm.cpp:1484
Interface for shared memory header.
Definition: shm.h:33
bool is_valid() const
Check validity of shared memory segment.
Definition: shm.cpp:766
void unlock()
Unlock memory.
Definition: shm.cpp:985