MMAP_Memory_Pool.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file     MMAP_Memory_Pool.h
00006  *
00007  *  MMAP_Memory_Pool.h,v 4.10 2006/06/09 07:51:09 jwillemsen 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_MMAP_MEMORY_POOL_H
00015 #define ACE_MMAP_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 #include "ace/ACE.h"
00026 #include "ace/Event_Handler.h"
00027 #include "ace/Signal.h"
00028 #include "ace/Mem_Map.h"
00029 
00030 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00031 
00032 /**
00033  * @class ACE_MMAP_Memory_Pool_Options
00034  *
00035  * @brief Helper class for MMAP Memory Pool constructor options.
00036  *
00037  * This should be a nested class, but that breaks too many
00038  * compilers.
00039  */
00040 class ACE_Export ACE_MMAP_Memory_Pool_Options
00041 {
00042 public:
00043   enum
00044   {
00045     /**
00046      * The base address from the first call to mmap will be used for subsequent
00047      * calls to mmap.
00048      */
00049     FIRSTCALL_FIXED = 0,
00050 
00051     /**
00052      * The base address specified in base_addr will be used in all calls to
00053      * mmap.
00054      */
00055     ALWAYS_FIXED = 1,
00056 
00057     /**
00058      * The base address will be selected by the OS for each call to mmap.
00059      * Caution should be used with this mode since a call that requires the
00060      * backing store to grow may change pointers that are cached by the
00061      * application.
00062      */
00063     NEVER_FIXED = 2
00064   };
00065 
00066   // = Initialization method.
00067   ACE_MMAP_Memory_Pool_Options (const void *base_addr = ACE_DEFAULT_BASE_ADDR,
00068                                 int use_fixed_addr = ALWAYS_FIXED,
00069                                 int write_each_page = 1,
00070                                 ACE_LOFF_T minimum_bytes = 0,
00071                                 u_int flags = 0,
00072                                 int guess_on_fault = 1,
00073                                 LPSECURITY_ATTRIBUTES sa = 0,
00074                                 mode_t file_mode = ACE_DEFAULT_FILE_PERMS,
00075                                 bool unique_ = false);
00076 
00077   /// Base address of the memory-mapped backing store.
00078   const void *base_addr_;
00079 
00080   /**
00081    * Determines whether we set @c base_addr_ or if mmap(2) selects it
00082    * FIRSTCALL_FIXED The base address from the first call to mmap
00083    *                 will be used for subsequent calls to mmap
00084    * ALWAYS_FIXED    The base address specified in base_addr will be
00085    *                 used in all calls to mmap.
00086    * NEVER_FIXED     The base address will be selected by the OS for
00087    *                 each call to mmap. Caution should be used with
00088    *                 this mode since a call that requires the backing
00089    *                 store to grow may change pointers that are
00090    *                 cached by the application.
00091    */
00092   int use_fixed_addr_;
00093 
00094   /// Should each page be written eagerly to avoid surprises later
00095   /// on?
00096   int write_each_page_;
00097 
00098   /// What the minimim bytes of the initial segment should be.
00099   ACE_LOFF_T minimum_bytes_;
00100 
00101   /// Any special flags that need to be used for @c mmap.
00102   u_int flags_;
00103 
00104   /**
00105    * Try to remap without knowing the faulting address.  This
00106    * parameter is ignored on platforms that know the faulting address
00107    * (UNIX with SI_ADDR and Win32).
00108    */
00109   bool guess_on_fault_;
00110 
00111   /// Pointer to a security attributes object.  Only used on NT.
00112   LPSECURITY_ATTRIBUTES sa_;
00113 
00114   /// File mode for mmaped file, if it is created.
00115   mode_t file_mode_;
00116 
00117   /// Do we want an unique backing store name?
00118   bool unique_;
00119 
00120 private:
00121   // Prevent copying
00122   ACE_MMAP_Memory_Pool_Options (const ACE_MMAP_Memory_Pool_Options &);
00123   ACE_MMAP_Memory_Pool_Options &operator= (const ACE_MMAP_Memory_Pool_Options &);
00124 };
00125 
00126 /**
00127  * @class ACE_MMAP_Memory_Pool
00128  *
00129  * @brief Make a memory pool that is based on @c mmap(2).  This
00130  * implementation allows memory to be shared between processes.
00131  */
00132 class ACE_Export ACE_MMAP_Memory_Pool : public ACE_Event_Handler
00133 {
00134 public:
00135   typedef ACE_MMAP_Memory_Pool_Options OPTIONS;
00136 
00137   // = Initialization and termination methods.
00138 
00139   /// Initialize the pool.
00140   ACE_MMAP_Memory_Pool (const ACE_TCHAR *backing_store_name = 0,
00141                         const OPTIONS *options = 0);
00142 
00143   /// Destructor.
00144   virtual ~ACE_MMAP_Memory_Pool (void);
00145 
00146   /// Ask system for initial chunk of shared memory.
00147   virtual void *init_acquire (size_t nbytes,
00148                               size_t &rounded_bytes,
00149                               int &first_time);
00150 
00151   /**
00152    * Acquire at least @a nbytes from the memory pool. @a rounded_bytes
00153    * is the actual number of bytes allocated.  Also acquires an
00154    * internal semaphore that ensures proper serialization of
00155    * ACE_MMAP_Memory_Pool initialization across processes.
00156    */
00157   virtual void *acquire (size_t nbytes,
00158                          size_t &rounded_bytes);
00159 
00160   /// Instruct the memory pool to release all of its resources.
00161   virtual int release (int destroy = 1);
00162 
00163   /// Sync the memory region to the backing store starting at
00164   /// @c this->base_addr_.
00165   virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
00166 
00167   /// Sync the memory region to the backing store starting at @a addr.
00168   virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
00169 
00170   /**
00171    * Change the protection of the pages of the mapped region to <prot>
00172    * starting at <this->base_addr_> up to <len> bytes.  If <len> == -1
00173    * then change protection of all pages in the mapped region.
00174    */
00175   virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
00176 
00177   /// Change the protection of the pages of the mapped region to @a prot
00178   /// starting at @a addr up to @a len bytes.
00179   virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
00180 
00181 #if defined (ACE_WIN32)
00182   /**
00183    * Win32 Structural exception selector.  The return value decides
00184    * how to handle memory pool related structural exceptions.  Returns
00185    * 1, 0, or , -1.
00186    */
00187   virtual int seh_selector (void *);
00188 #endif /* ACE_WIN32 */
00189 
00190   /**
00191    * Try to extend the virtual address space so that @a addr is now
00192    * covered by the address mapping.  The method succeeds and returns
00193    * 0 if the backing store has adequate memory to cover this address.
00194    * Otherwise, it returns -1.  This method is typically called by a
00195    * UNIX signal handler for SIGSEGV or a Win32 structured exception
00196    * when another process has grown the backing store (and its
00197    * mapping) and our process now incurs a fault because our mapping
00198    * isn't in range (yet).
00199    */
00200   virtual int remap (void *addr);
00201 
00202   /// Return the base address of this memory pool.
00203   virtual void *base_addr (void) const;
00204 
00205   /// Dump the state of an object.
00206   virtual void dump (void) const;
00207 
00208   /// Get reference to underlying ACE_Mem_Map object.
00209   ACE_Mem_Map const & mmap (void) const;
00210 
00211   /// Get reference to underlying ACE_Mem_Map object.
00212   ACE_Mem_Map & mmap (void);
00213 
00214   /// Declare the dynamic allocation hooks.
00215   ACE_ALLOC_HOOK_DECLARE;
00216 
00217 protected:
00218   /// Implement the algorithm for rounding up the request to an
00219   /// appropriate chunksize.
00220   virtual size_t round_up (size_t nbytes);
00221 
00222   /// Compute the new @a map_size of the backing store and commit the
00223   /// memory.
00224   virtual int commit_backing_store_name (size_t rounded_bytes,
00225                                          ACE_LOFF_T &map_size);
00226 
00227   /// Memory map the file up to @a map_size bytes.
00228   virtual int map_file (ACE_LOFF_T map_size);
00229 
00230   /// Handle SIGSEGV and SIGBUS signals to remap shared memory
00231   /// properly.
00232   virtual int handle_signal (int signum, siginfo_t *, ucontext_t *);
00233 
00234   /// Handles SIGSEGV.
00235   ACE_Sig_Handler signal_handler_;
00236 
00237   /// Memory-mapping object.
00238   ACE_Mem_Map mmap_;
00239 
00240   /**
00241    * Base of mapped region.  If this has the value of 0 then the OS is
00242    * free to select any address to map the file, otherwise this value
00243    * is what the OS must try to use to mmap the file.
00244    */
00245   void *base_addr_;
00246 
00247   /// Must we use the @c base_addr_ or can we let mmap(2) select it?
00248   int use_fixed_addr_;
00249 
00250   /// Flags passed into <ACE_OS::mmap>.
00251   int flags_;
00252 
00253   /// Should we write a byte to each page to forceably allocate memory
00254   /// for this backing store?
00255   int write_each_page_;
00256 
00257   /// What the minimum bytes of the initial segment should be.
00258   ACE_LOFF_T minimum_bytes_;
00259 
00260   /// Name of the backing store where the shared memory pool is kept.
00261   ACE_TCHAR backing_store_name_[MAXPATHLEN + 1];
00262 
00263   /**
00264    * Try to remap without knowing the faulting address.  This
00265    * parameter is ignored on platforms that know the faulting address
00266    * (UNIX with SI_ADDR and Win32).
00267    */
00268   bool guess_on_fault_;
00269 
00270   /// Security attributes object, only used on NT.
00271   LPSECURITY_ATTRIBUTES sa_;
00272 
00273   /// Protection mode for mmaped file.
00274   mode_t file_mode_;
00275 };
00276 
00277 /**
00278  * @class ACE_Lite_MMAP_Memory_Pool
00279  *
00280  * @brief Make a ``lighter-weight'' memory pool based ACE_Mem_Map.
00281  *
00282  * This implementation allows memory to be shared between
00283  * processes.  However, unlike the ACE_MMAP_Memory_Pool
00284  * the <sync> methods are no-ops, which means that we don't pay
00285  * for the price of flushing the memory to the backing store on
00286  * every update.  Naturally, this trades off increased
00287  * performance for less reliability if the machine crashes.
00288  */
00289 class ACE_Export ACE_Lite_MMAP_Memory_Pool : public ACE_MMAP_Memory_Pool
00290 {
00291 public:
00292   /// Initialize the pool.
00293   ACE_Lite_MMAP_Memory_Pool (const ACE_TCHAR *backing_store_name = 0,
00294                              const OPTIONS *options = 0);
00295 
00296   /// Destructor.
00297   virtual ~ACE_Lite_MMAP_Memory_Pool (void);
00298 
00299   /// Overwrite the default sync behavior with no-op
00300   virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
00301 
00302   /// Overwrite the default sync behavior with no-op
00303   virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
00304 };
00305 
00306 ACE_END_VERSIONED_NAMESPACE_DECL
00307 
00308 #if defined (__ACE_INLINE__)
00309 #include "ace/MMAP_Memory_Pool.inl"
00310 #endif /* __ACE_INLINE__ */
00311 
00312 #include /**/ "ace/post.h"
00313 #endif /* ACE_MMAP_MEMORY_POOL_H */

Generated on Thu Nov 9 09:41:56 2006 for ACE by doxygen 1.3.6