Shared_Memory_Pool.h

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

Generated on Thu Nov 9 09:42:03 2006 for ACE by doxygen 1.3.6