Fawkes API  Fawkes Development Version
shm_registry.h
00001 
00002 /***************************************************************************
00003  *  shm_registry.h - shared memory registry
00004  *
00005  *  Created: Sun Mar 06 12:07:50 2011
00006  *  Copyright  2011  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __UTILS_IPC_SHM_REGISTRY_H_
00025 #define __UTILS_IPC_SHM_REGISTRY_H_
00026 
00027 #include <semaphore.h>
00028 #include <list>
00029 
00030 #define MAGIC_TOKEN_SIZE 16
00031 #define MAXNUM_SHM_SEGMS 64
00032 #define DEFAULT_SHM_NAME "/fawkes-shmem-registry"
00033 
00034 namespace fawkes {
00035 #if 0 /* just to make Emacs auto-indent happy */
00036 }
00037 #endif
00038 
00039 class SharedMemoryRegistry
00040 {
00041  public:
00042   /** Shared memory identifier. */
00043   typedef struct {
00044     int  shmid;                         /**< SysV IPC shared memory ID */
00045     char magic_token[MAGIC_TOKEN_SIZE]; /**< Magic token */
00046   } SharedMemID;
00047 
00048  public:
00049   SharedMemoryRegistry(bool master = false,
00050                        const char *name = 0);
00051   ~SharedMemoryRegistry();
00052 
00053   std::list<SharedMemoryRegistry::SharedMemID> get_snapshot() const;
00054 
00055   std::list<SharedMemoryRegistry::SharedMemID>
00056     find_segments(const char *magic_token) const;
00057 
00058   void add_segment(int shmid, const char *magic_token);
00059   void remove_segment(int shmid);
00060 
00061   static void cleanup(const char *name = 0);
00062 
00063  private:
00064   /// @cond INTERNALS
00065   typedef struct {
00066     SharedMemID segments[MAXNUM_SHM_SEGMS];
00067   } MemInfo;
00068   /// @endcond
00069 
00070   bool  __master;
00071   int   __shmfd;
00072   char *__shm_name;
00073 
00074   sem_t   *__sem;
00075   MemInfo *__meminfo;
00076 };
00077 
00078 
00079 } // end namespace fawkes
00080 
00081 #endif