Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Mem_Map.h

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

Generated on Mon Jun 16 11:20:18 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002