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 <list>
28 #include <semaphore.h>
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 
38 {
39 public:
40  /** Shared memory identifier. */
41  typedef struct
42  {
43  int shmid; /**< SysV IPC shared memory ID */
44  char magic_token[MAGIC_TOKEN_SIZE]; /**< Magic token */
45  } SharedMemID;
46 
47 public:
48  SharedMemoryRegistry(const char *name = 0);
50 
51  std::list<SharedMemoryRegistry::SharedMemID> get_snapshot() const;
52 
53  std::list<SharedMemoryRegistry::SharedMemID> find_segments(const char *magic_token) const;
54 
55  void add_segment(int shmid, const char *magic_token);
56  void remove_segment(int shmid);
57 
58  static void cleanup(const char *name = 0);
59 
60 private:
61  /// @cond INTERNALS
62  typedef struct
63  {
64  SharedMemID segments[MAXNUM_SHM_SEGMS];
65  } MemInfo;
66  /// @endcond
67 
68  bool master_;
69  int shmfd_;
70  char *shm_name_;
71 
72  sem_t * sem_;
73  MemInfo *meminfo_;
74 };
75 
76 } // end namespace fawkes
77 
78 #endif
Shared memory registry.
Definition: shm_registry.h:38
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.
void remove_segment(int shmid)
Remove segment.
SharedMemoryRegistry(const char *name=0)
Constructor.
static void cleanup(const char *name=0)
Cleanup existing shared memory segments.
std::list< SharedMemoryRegistry::SharedMemID > find_segments(const char *magic_token) const
Find segments with particular magic token.
Fawkes library namespace.
int shmid
SysV IPC shared memory ID.
Definition: shm_registry.h:43