Shared_Memory_Pool.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file     Shared_Memory_Pool.h
00006  *
00007  *  $Id: Shared_Memory_Pool.h 80826 2008-03-04 14:51:23Z wotte $
00008  *
00009  *  @author Dougls C. Schmidt <schmidt@cs.wustl.edu>
00010  *  @author Prashant Jain <pjain@cs.wustl.edu>
00011  */
00012 //=============================================================================
00013 
00014 #ifndef ACE_SHARED_MEMORY_POOL_H
00015 #define ACE_SHARED_MEMORY_POOL_H
00016 
00017 #include /**/ "ace/pre.h"
00018 
00019 #include /**/ "ace/ACE_export.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif /* ACE_LACKS_PRAGMA_ONCE */
00024 
00025 #if !defined (ACE_LACKS_SYSV_SHMEM)
00026 
00027 #include "ace/ACE.h"
00028 #include "ace/Event_Handler.h"
00029 #include "ace/Sig_Handler.h"
00030 #include "ace/os_include/sys/os_mman.h"
00031 
00032 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00033 
00034 /**
00035  * @class ACE_Shared_Memory_Pool_Options
00036  *
00037  * @brief Helper class for Shared Memory Pool constructor options.
00038  *
00039  * This should be a nested class, but that breaks too many
00040  * compilers.
00041  */
00042 class ACE_Export ACE_Shared_Memory_Pool_Options
00043 {
00044 public:
00045   /// Initialization method.
00046   ACE_Shared_Memory_Pool_Options (
00047     const char *base_addr = ACE_DEFAULT_BASE_ADDR,
00048     size_t max_segments = ACE_DEFAULT_MAX_SEGMENTS,
00049     size_t file_perms = ACE_DEFAULT_FILE_PERMS,
00050     ACE_OFF_T minimum_bytes = 0,
00051     size_t segment_size = ACE_DEFAULT_SEGMENT_SIZE);
00052 
00053   /// Base address of the memory-mapped backing store.
00054   const char *base_addr_;
00055 
00056   /// Number of shared memory segments to allocate.
00057   size_t max_segments_;
00058 
00059   /// What the minimum bytes of the initial segment should be.
00060   ACE_OFF_T minimum_bytes_;
00061 
00062   /// File permissions to use when creating/opening a segment.
00063   size_t file_perms_;
00064 
00065   /// Shared memory segment size.
00066   size_t segment_size_;
00067 };
00068 
00069 /**
00070  * @class ACE_Shared_Memory_Pool
00071  *
00072  * @brief Make a memory pool that is based on System V shared memory
00073  * (shmget(2) etc.).  This implementation allows memory to be
00074  * shared between processes.  If your platform doesn't support
00075  * System V shared memory (e.g., Win32 and many RTOS platforms
00076  * do not) then you should use ACE_MMAP_Memory_Pool instead of this
00077  * class.  In fact, you should probably use ACE_MMAP_Memory_Pool on
00078  * platforms that *do* support System V shared memory since it
00079  * provides more powerful features, such as persistent backing store
00080  * and greatly scalability.
00081  */
00082 class ACE_Export ACE_Shared_Memory_Pool : public ACE_Event_Handler
00083 {
00084 public:
00085   typedef ACE_Shared_Memory_Pool_Options OPTIONS;
00086 
00087   /// Initialize the pool.
00088   ACE_Shared_Memory_Pool (const ACE_TCHAR *backing_store_name = 0,
00089                           const OPTIONS *options = 0);
00090 
00091   virtual ~ACE_Shared_Memory_Pool (void);
00092 
00093   /// Ask system for initial chunk of local memory.
00094   virtual void *init_acquire (size_t nbytes,
00095                               size_t &rounded_bytes,
00096                               int &first_time);
00097 
00098   /**
00099    * Acquire at least @a nbytes from the memory pool. @a rounded_byes is
00100    * the actual number of bytes allocated.  Also acquires an internal
00101    * semaphore that ensures proper serialization of Memory_Pool
00102    * initialization across processes.
00103    */
00104   virtual void *acquire (size_t nbytes,
00105                          size_t &rounded_bytes);
00106 
00107   /// Instruct the memory pool to release all of its resources.
00108   virtual int release (int destroy = 1);
00109 
00110   /// Sync the memory region to the backing store starting at
00111   /// @c this->base_addr_.
00112   virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
00113 
00114   /// Sync the memory region to the backing store starting at @a addr.
00115   virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
00116 
00117   /**
00118    * Change the protection of the pages of the mapped region to @a prot
00119    * starting at @c this->base_addr_ up to @a len bytes.  If @a len == -1
00120    * then change protection of all pages in the mapped region.
00121    */
00122   virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
00123 
00124   /// Change the protection of the pages of the mapped region to @a prot
00125   /// starting at @a addr up to @a len bytes.
00126   virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
00127 
00128   /// Return the base address of this memory pool, 0 if base_addr
00129   /// never changes.
00130   virtual void *base_addr (void) const;
00131 
00132   /// Dump the state of an object.
00133   virtual void dump (void) const;
00134 
00135   /// Declare the dynamic allocation hooks.
00136   ACE_ALLOC_HOOK_DECLARE;
00137 
00138 protected:
00139   /// Implement the algorithm for rounding up the request to an
00140   /// appropriate chunksize.
00141   virtual size_t round_up (size_t nbytes);
00142 
00143   /**
00144    * Commits a new shared memory segment if necessary after an
00145    * <acquire> or a signal.  @a offset is set to the new offset into
00146    * the backing store.
00147    */
00148   virtual int commit_backing_store_name (size_t rounded_bytes,
00149                                          ACE_OFF_T &offset);
00150 
00151   /// Keeps track of all the segments being used.
00152   struct SHM_TABLE
00153   {
00154     /// Shared memory segment key.
00155     key_t key_;
00156 
00157     /// Shared memory segment internal id.
00158     int shmid_;
00159 
00160     /// Is the segment currently used.;
00161     int used_;
00162   };
00163 
00164   /**
00165    * Base address of the shared memory segment.  If this has the value
00166    * of 0 then the OS is free to select any address, otherwise this
00167    * value is what the OS must try to use to map the shared memory
00168    * segment.
00169    */
00170   void *base_addr_;
00171 
00172   /// File permissions to use when creating/opening a segment.
00173   size_t file_perms_;
00174 
00175   /// Number of shared memory segments in the <SHM_TABLE> table.
00176   size_t max_segments_;
00177 
00178   /// What the minimim bytes of the initial segment should be.
00179   ACE_OFF_T minimum_bytes_;
00180 
00181   /// Shared memory segment size.
00182   size_t segment_size_;
00183 
00184   /// Base shared memory key for the segment.
00185   key_t base_shm_key_;
00186 
00187   /// Find the segment that contains the @a searchPtr
00188   virtual int find_seg (const void *const searchPtr,
00189                         ACE_OFF_T &offset,
00190                         size_t &counter);
00191 
00192   /// Determine how much memory is currently in use.
00193   virtual int in_use (ACE_OFF_T &offset,
00194                       size_t &counter);
00195 
00196   /// Handles SIGSEGV.
00197   ACE_Sig_Handler signal_handler_;
00198 
00199   /// Handle SIGSEGV and SIGBUS signals to remap shared memory
00200   /// properly.
00201   virtual int handle_signal (int signum, siginfo_t *, ucontext_t *);
00202 };
00203 
00204 ACE_END_VERSIONED_NAMESPACE_DECL
00205 
00206 #endif /* !ACE_LACKS_SYSV_SHMEM */
00207 
00208 #include /**/ "ace/post.h"
00209 
00210 #endif /* ACE_SHARED_MEMORY_POOL_H */

Generated on Tue Feb 2 17:18:42 2010 for ACE by  doxygen 1.4.7