Pagefile_Memory_Pool.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file     Pagefile_Memory_Pool.h
00006  *
00007  *  Pagefile_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_PAGEFILE_MEMORY_POOL_H
00015 #define ACE_PAGEFILE_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_WIN32)
00026 
00027 #include "ace/ACE.h"
00028 #include "ace/os_include/sys/os_mman.h"
00029 
00030 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00031 
00032 /**
00033  * @class ACE_Pagefile_Memory_Pool_Options
00034  *
00035  * @brief Helper class for Pagefile 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_Pagefile_Memory_Pool_Options
00041 {
00042 public:
00043   /// Initialization method.
00044   ACE_Pagefile_Memory_Pool_Options (
00045     void *base_addr = ACE_DEFAULT_PAGEFILE_POOL_BASE,
00046     size_t max_size = ACE_DEFAULT_PAGEFILE_POOL_SIZE);
00047 
00048   /// Base address of the memory-mapped backing store.
00049   void *base_addr_;
00050 
00051   /// Maximum size the pool may grow.
00052   size_t max_size_;
00053 };
00054 
00055 /**
00056  * @class ACE_Pagefile_Memory_Pool
00057  *
00058  * @brief Make a memory pool that is based on "anonymous" memory
00059  * regions allocated from the Win32 page file.
00060  */
00061 class ACE_Export ACE_Pagefile_Memory_Pool
00062 {
00063 public:
00064   typedef ACE_Pagefile_Memory_Pool_Options OPTIONS;
00065 
00066   /// Initialize the pool.
00067   ACE_Pagefile_Memory_Pool (const ACE_TCHAR *backing_store_name = 0,
00068                             const OPTIONS *options = 0);
00069 
00070   /// Ask system for initial chunk of shared memory.
00071   void *init_acquire (size_t nbytes,
00072                       size_t &rounded_bytes,
00073                       int &first_time);
00074 
00075   /// Acquire at least <nbytes> from the memory pool.  <rounded_bytes>
00076   /// is the actual number of bytes allocated.
00077   void *acquire (size_t nbytes,
00078                  size_t &rounded_bytes);
00079 
00080   /// Instruct the memory pool to release all of its resources.
00081   int release (int destroy = 1);
00082 
00083   /**
00084    * Win32 Structural exception selector.  The return value decides
00085    * how to handle memory pool related structural exceptions.  Returns
00086    * 1, 0, or , -1.
00087    */
00088   virtual int seh_selector (void *);
00089 
00090   /**
00091    * Try to extend the virtual address space so that <addr> is now
00092    * covered by the address mapping.  The method succeeds and returns
00093    * 0 if the backing store has adequate memory to cover this address.
00094    * Otherwise, it returns -1.  This method is typically called by an
00095    * exception handler for a Win32 structured exception when another
00096    * process has grown the backing store (and its mapping) and our
00097    * process now incurs a fault because our mapping isn't in range
00098    * (yet).
00099    */
00100   int remap (void *addr);
00101 
00102   /// Round up to system page size.
00103   size_t round_to_page_size (size_t nbytes);
00104 
00105   /// Round up to the chunk size required by the operation system
00106   size_t round_to_chunk_size (size_t nbytes);
00107 
00108   // = Don't need this methods here ...
00109   int sync (ssize_t = -1, int = MS_SYNC);
00110   int sync (void *, size_t, int = MS_SYNC);
00111   int protect (ssize_t = -1, int = PROT_RDWR);
00112   int protect (void *, size_t, int = PROT_RDWR);
00113 
00114   /// Return the base address of this memory pool, 0 if base_addr
00115   /// never changes.
00116   virtual void *base_addr (void) const;
00117 
00118   void dump (void) const {}
00119 
00120 protected:
00121 
00122   /**
00123    * Map portions or the entire pool into the local virtual address
00124    * space.  To do this, we compute the new @c file_offset of the
00125    * backing store and commit the memory.
00126    */
00127   int map (int &firstTime, size_t appendBytes = 0);
00128 
00129   /// Release the mapping.
00130   int unmap (void);
00131 
00132 private:
00133 
00134   /**
00135    * @class Control_Block
00136    *
00137    * @brief Attributes that are meaningful in local storage only.
00138    */
00139   class Control_Block
00140   {
00141   public:
00142     /// Required base address
00143     void *req_base_;
00144 
00145     /// Base address returned from system call
00146     void *mapped_base_;
00147 
00148     /**
00149      * @class Shared_Control_Block
00150      *
00151      * @brief Pool statistics
00152      */
00153     class Shared_Control_Block
00154     {
00155     public:
00156       /// Maximum size the pool may grow
00157       size_t max_size_;
00158 
00159       /// Size of mapped shared memory segment
00160       size_t mapped_size_;
00161 
00162       /// Offset to mapped but not yet acquired address space
00163       ptrdiff_t free_offset_;
00164 
00165       /// Size of mapped but not yet acquired address space
00166       size_t free_size_;
00167     };
00168 
00169     Shared_Control_Block sh_;
00170   };
00171 
00172   // Base of mapped region.  If this has the value of 0 then the OS is
00173   // free to select any address to map the file, otherwise this value
00174   // is what the OS must try to use to mmap the file.
00175 
00176   /// Description of what our process mapped.
00177   Control_Block local_cb_;
00178 
00179   /// Shared memory pool statistics.
00180   Control_Block *shared_cb_;
00181 
00182   /// File mapping handle.
00183   ACE_HANDLE object_handle_;
00184 
00185   /// System page size.
00186   size_t page_size_;
00187 
00188   /// Name of the backing store where the shared memory pool is kept.
00189   ACE_TCHAR backing_store_name_[MAXPATHLEN];
00190 };
00191 
00192 ACE_END_VERSIONED_NAMESPACE_DECL
00193 
00194 #endif /* ACE_WIN32 */
00195 
00196 #if defined (__ACE_INLINE__)
00197 #include "ace/Pagefile_Memory_Pool.inl"
00198 #endif /* __ACE_INLINE__ */
00199 
00200 #include /**/ "ace/post.h"
00201 #endif /* ACE_MEMORY_POOL_H */

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