Mem_Map.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //==========================================================================
00004 /**
00005  *  @file    Mem_Map.h
00006  *
00007  *  Mem_Map.h,v 4.38 2006/06/09 07:59:44 jwillemsen Exp
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //==========================================================================
00012 
00013 #ifndef ACE_MEM_MAP_H
00014 #define ACE_MEM_MAP_H
00015 
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "ace/ACE_export.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/Global_Macros.h"
00025 #include "ace/os_include/sys/os_mman.h"
00026 #include "ace/os_include/os_limits.h"
00027 #include "ace/os_include/os_fcntl.h"
00028 #include "ace/Default_Constants.h"
00029 
00030 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00031 
00032 /**
00033  * @class ACE_Mem_Map
00034  *
00035  * @brief C++ interface OS memory mapping system call.
00036  *
00037  * This class works with both the mmap(2) UNIX system and the
00038  * Win32 family of memory mapping system calls.
00039  */
00040 class ACE_Export ACE_Mem_Map
00041 {
00042 public:
00043   // = Initialization and termination methods.
00044 
00045   /// Default constructor.
00046   ACE_Mem_Map (void);
00047 
00048   /// Map a file from an open file descriptor @a handle.  This function
00049   /// will lookup the length of the file if it is not given.
00050   ACE_Mem_Map (ACE_HANDLE handle,
00051                ssize_t length = -1,
00052                int prot = PROT_RDWR,
00053                int share = ACE_MAP_PRIVATE,
00054                void *addr = 0,
00055                off_t offset = 0,
00056                LPSECURITY_ATTRIBUTES sa = 0);
00057 
00058   /// Map a file specified by @a file_name.
00059   ACE_Mem_Map (const ACE_TCHAR *filename,
00060                ssize_t length = -1,
00061                int flags = O_RDWR | O_CREAT,
00062                int mode = ACE_DEFAULT_FILE_PERMS,
00063                int prot = PROT_RDWR,
00064                int share = ACE_MAP_PRIVATE,
00065                void *addr = 0,
00066                off_t offset = 0,
00067                LPSECURITY_ATTRIBUTES sa = 0);
00068 
00069   /// Map a file from an open file descriptor @a handle.  This function
00070   /// will lookup the length of the file if it is not given.
00071   int map (ACE_HANDLE handle,
00072            ssize_t length = -1,
00073            int prot = PROT_RDWR,
00074            int share = ACE_MAP_PRIVATE,
00075            void *addr = 0,
00076            off_t offset = 0,
00077            LPSECURITY_ATTRIBUTES sa = 0);
00078 
00079   /// Remap the file associated with <handle_>.
00080   int map (ssize_t length = -1,
00081            int prot = PROT_RDWR,
00082            int share = ACE_MAP_PRIVATE,
00083            void *addr = 0,
00084            off_t offset = 0,
00085            LPSECURITY_ATTRIBUTES sa = 0);
00086 
00087   /// Map a file specified by <filename>.
00088   int map (const ACE_TCHAR *filename,
00089            ssize_t length = -1,
00090            int flags = O_RDWR | O_CREAT,
00091            int mode = ACE_DEFAULT_FILE_PERMS,
00092            int prot = PROT_RDWR,
00093            int share = ACE_MAP_PRIVATE,
00094            void *addr = 0,
00095            off_t offset = 0,
00096            LPSECURITY_ATTRIBUTES sa = 0);
00097 
00098   /// Destructor.
00099   ~ACE_Mem_Map (void);
00100 
00101   /// Open the file without mapping it.
00102   int open (const ACE_TCHAR *filename,
00103             int flags = O_RDWR | O_CREAT,
00104             int mode = ACE_DEFAULT_FILE_PERMS,
00105             LPSECURITY_ATTRIBUTES sa = 0);
00106 
00107   /// Close down the <handle_> if necessary and unmap the mapping.
00108   int close (void);
00109 
00110   /// Close down the <handle_> if necessary.
00111   int close_handle (void);
00112 
00113   /**
00114    * Close down the internal <file_mapping_> if necessary.  This is
00115    * mostly necessary on Win32, which has a different handle for
00116    * file-mapping kernel object.
00117    */
00118   int close_filemapping_handle (void);
00119 
00120   /// This operator passes back the starting address of the mapped
00121   /// file.
00122   int operator () (void *&addr);
00123 
00124   /// Return the base address.
00125   void *addr (void) const;
00126 
00127   /// This function returns the number of bytes currently mapped in the
00128   /// file.
00129   size_t size (void) const;
00130 
00131   /// Unmap the region starting at <base_addr_>.
00132   int unmap (ssize_t len = -1);
00133 
00134   /// Unmap the region starting at <addr_>.
00135   int unmap (void *addr, ssize_t len);
00136 
00137   /**
00138    * Sync <len> bytes of the memory region to the backing store
00139    * starting at <base_addr_>.  If <len> == -1 then sync the whole
00140    * region.
00141    */
00142   int sync (ssize_t len = -1, int flags = MS_SYNC);
00143 
00144   /// Sync <len> bytes of the memory region to the backing store
00145   /// starting at <addr_>.
00146   int sync (void *addr, size_t len, int flags = MS_SYNC);
00147 
00148   /**
00149    * Change the protection of the pages of the mapped region to <prot>
00150    * starting at <base_addr_> up to <len> bytes.  If <len> == -1 then
00151    * change protection of all pages in the mapped region.
00152    */
00153   int protect (ssize_t len = -1, int prot = PROT_RDWR);
00154 
00155   /// Change the protection of the pages of the mapped region to <prot>
00156   /// starting at <addr> up to <len> bytes.
00157   int protect (void *addr, size_t len, int prot = PROT_RDWR);
00158 
00159   /// Close and remove the file from the file system.
00160   int remove (void);
00161 
00162   /// Hook into the underlying VM system.
00163   int advise (int behavior, int len = -1);
00164 
00165   /// Return the underlying <handle_>.
00166   ACE_HANDLE handle (void) const;
00167 
00168   /// Return the name of file that is mapped (if any).
00169   const ACE_TCHAR *filename (void) const;
00170 
00171   /// Dump the state of an object.
00172   void dump (void) const;
00173 
00174   /// Declare the dynamic allocation hooks.
00175   ACE_ALLOC_HOOK_DECLARE;
00176 
00177 private:
00178 
00179   /// This method does the dirty work of actually calling ::mmap to map
00180   /// the file into memory.
00181   int map_it (ACE_HANDLE handle,
00182               ssize_t len = -1,
00183               int prot = PROT_RDWR,
00184               int share = MAP_SHARED,
00185               void *addr = 0,
00186               off_t offset = 0,
00187               LPSECURITY_ATTRIBUTES sa = 0);
00188 
00189   // = Disallow copying and assignment.
00190   ACE_Mem_Map (const ACE_Mem_Map &);
00191   void operator = (const ACE_Mem_Map &);
00192 
00193 private:
00194 
00195   /// Base address of the memory-mapped file.
00196   void *base_addr_;
00197 
00198   /// Name of the file that is mapped.
00199   ACE_TCHAR filename_[MAXPATHLEN + 1];
00200 
00201   /// Length of the mapping.
00202   size_t length_;
00203 
00204   /// HANDLE for the open file.
00205   ACE_HANDLE handle_;
00206 
00207   /// HANDLE for the open mapping.
00208   ACE_HANDLE file_mapping_;
00209 
00210 #if defined (ACE_HAS_LYNXOS_BROKEN_MMAP)
00211   /// Flag to indicate that PROT_WRITE has been enabled.
00212   int write_enabled_;
00213 #endif /* ACE_HAS_LYNXOS_BROKEN_MMAP */
00214 
00215   /// Keeps track of whether we need to close the handle.  This is set
00216   /// if we opened the file.
00217   bool close_handle_;
00218 
00219 };
00220 
00221 ACE_END_VERSIONED_NAMESPACE_DECL
00222 
00223 #if defined (__ACE_INLINE__)
00224 #include "ace/Mem_Map.inl"
00225 #endif /* __ACE_INLINE__ */
00226 
00227 #include /**/ "ace/post.h"
00228 
00229 #endif /* ACE_MEM_MAP_H */

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