Fawkes API  Fawkes Development Version
shm_registry.h
1 
2 /***************************************************************************
3  * shm_registry.h - shared memory registry
4  *
5  * Created: Sun Mar 06 12:07:50 2011
6  * Copyright 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_REGISTRY_H_
25 #define __UTILS_IPC_SHM_REGISTRY_H_
26 
27 #include <semaphore.h>
28 #include <list>
29 
30 #define MAGIC_TOKEN_SIZE 16
31 #define MAXNUM_SHM_SEGMS 64
32 #define DEFAULT_SHM_NAME "/fawkes-shmem-registry"
33 #define USER_SHM_NAME "/fawkes-shmem-registry-%s"
34 
35 namespace fawkes {
36 #if 0 /* just to make Emacs auto-indent happy */
37 }
38 #endif
39 
41 {
42  public:
43  /** Shared memory identifier. */
44  typedef struct {
45  int shmid; /**< SysV IPC shared memory ID */
46  char magic_token[MAGIC_TOKEN_SIZE]; /**< Magic token */
47  } SharedMemID;
48 
49  public:
50  SharedMemoryRegistry(const char *name = 0);
52 
53  std::list<SharedMemoryRegistry::SharedMemID> get_snapshot() const;
54 
55  std::list<SharedMemoryRegistry::SharedMemID>
56  find_segments(const char *magic_token) const;
57 
58  void add_segment(int shmid, const char *magic_token);
59  void remove_segment(int shmid);
60 
61  static void cleanup(const char *name = 0);
62 
63  private:
64  /// @cond INTERNALS
65  typedef struct {
66  SharedMemID segments[MAXNUM_SHM_SEGMS];
67  } MemInfo;
68  /// @endcond
69 
70  bool __master;
71  int __shmfd;
72  char *__shm_name;
73 
74  sem_t *__sem;
75  MemInfo *__meminfo;
76 };
77 
78 
79 } // end namespace fawkes
80 
81 #endif
void add_segment(int shmid, const char *magic_token)
Register a segment.
std::list< SharedMemoryRegistry::SharedMemID > get_snapshot() const
Get a snapshot of currently registered segments.
Fawkes library namespace.
Shared memory registry.
Definition: shm_registry.h:40
SharedMemoryRegistry(const char *name=0)
Constructor.
std::list< SharedMemoryRegistry::SharedMemID > find_segments(const char *magic_token) const
Find segments with particular magic token.
void remove_segment(int shmid)
Remove segment.
int shmid
SysV IPC shared memory ID.
Definition: shm_registry.h:45
static void cleanup(const char *name=0)
Cleanup existing shared memory segments.