Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 
34 namespace fawkes {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
40 {
41  public:
42  /** Shared memory identifier. */
43  typedef struct {
44  int shmid; /**< SysV IPC shared memory ID */
45  char magic_token[MAGIC_TOKEN_SIZE]; /**< Magic token */
46  } SharedMemID;
47 
48  public:
49  SharedMemoryRegistry(bool master = false,
50  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