3 //==========================================================================
7 * $Id: Mem_Map.h 80826 2008-03-04 14:51:23Z wotte $
9 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
11 //==========================================================================
16 #include /**/ "ace/pre.h"
18 #include /**/ "ace/ACE_export.h"
20 #if !defined (ACE_LACKS_PRAGMA_ONCE)
22 #endif /* ACE_LACKS_PRAGMA_ONCE */
24 #include "ace/Global_Macros.h"
25 #include "ace/os_include/sys/os_mman.h"
26 #include "ace/os_include/os_limits.h"
27 #include "ace/os_include/os_fcntl.h"
28 #include "ace/Default_Constants.h"
30 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
35 * @brief C++ interface OS memory mapping system call.
37 * This class works with both the mmap(2) UNIX system and the
38 * Win32 family of memory mapping system calls.
40 class ACE_Export ACE_Mem_Map
43 // = Initialization and termination methods.
45 /// Default constructor.
48 /// Map a file from an open file descriptor @a handle. This function
49 /// will lookup the length of the file if it is not given.
50 ACE_Mem_Map (ACE_HANDLE handle
,
51 size_t length
= static_cast<size_t> (-1),
53 int share
= ACE_MAP_PRIVATE
,
56 LPSECURITY_ATTRIBUTES sa
= 0);
58 /// Map a file specified by @a file_name.
59 ACE_Mem_Map (const ACE_TCHAR
*filename
,
60 size_t length
= static_cast<size_t> (-1),
61 int flags
= O_RDWR
| O_CREAT
,
62 mode_t mode
= ACE_DEFAULT_FILE_PERMS
,
64 int share
= ACE_MAP_PRIVATE
,
67 LPSECURITY_ATTRIBUTES sa
= 0);
69 /// Map a file from an open file descriptor @a handle. This function
70 /// will lookup the length of the file if it is not given.
71 int map (ACE_HANDLE handle
,
72 size_t length
= static_cast<size_t> (-1),
74 int share
= ACE_MAP_PRIVATE
,
77 LPSECURITY_ATTRIBUTES sa
= 0);
79 /// Remap the file associated with <handle_>.
80 int map (size_t length
= static_cast<size_t> (-1),
82 int share
= ACE_MAP_PRIVATE
,
85 LPSECURITY_ATTRIBUTES sa
= 0);
87 /// Map a file specified by @a filename.
88 int map (const ACE_TCHAR
*filename
,
89 size_t length
= static_cast<size_t> (-1),
90 int flags
= O_RDWR
| O_CREAT
,
91 mode_t mode
= ACE_DEFAULT_FILE_PERMS
,
93 int share
= ACE_MAP_PRIVATE
,
96 LPSECURITY_ATTRIBUTES sa
= 0);
101 /// Open the file without mapping it.
102 int open (const ACE_TCHAR
*filename
,
103 int flags
= O_RDWR
| O_CREAT
,
104 mode_t perms
= ACE_DEFAULT_FILE_PERMS
,
105 LPSECURITY_ATTRIBUTES sa
= 0);
107 /// Close down the <handle_> if necessary and unmap the mapping.
110 /// Close down the <handle_> if necessary.
111 int close_handle (void);
114 * Close down the internal <file_mapping_> if necessary. This is
115 * mostly necessary on Win32, which has a different handle for
116 * file-mapping kernel object.
118 int close_filemapping_handle (void);
120 /// This operator passes back the starting address of the mapped
122 int operator () (void *&addr
);
124 /// Return the base address.
125 void *addr (void) const;
127 /// This function returns the number of bytes currently mapped in the
129 size_t size (void) const;
131 /// Unmap the region starting at <base_addr_>.
132 int unmap (ssize_t len
= -1);
134 /// Unmap the region starting at <addr_>.
135 int unmap (void *addr
, ssize_t len
);
138 * Sync @a len bytes of the memory region to the backing store
139 * starting at <base_addr_>. If @a len == -1 then sync the whole
142 int sync (size_t len
, int flags
= MS_SYNC
);
145 * Sync the whole memory region to the backing store
146 * starting at <base_addr_>.
148 int sync (int flags
= MS_SYNC
);
150 /// Sync @a len bytes of the memory region to the backing store
151 /// starting at <addr_>.
152 int sync (void *addr
, size_t len
, int flags
= MS_SYNC
);
155 * Change the protection of the pages of the mapped region to @a prot
156 * starting at <base_addr_> up to @a len bytes.
158 int protect (size_t len
, int prot
= PROT_RDWR
);
161 * Change the protection of all the pages of the mapped region to @a prot
162 * starting at <base_addr_>.
164 int protect (int prot
= PROT_RDWR
);
166 /// Change the protection of the pages of the mapped region to @a prot
167 /// starting at @a addr up to @a len bytes.
168 int protect (void *addr
, size_t len
, int prot
= PROT_RDWR
);
170 /// Close and remove the file from the file system.
173 /// Hook into the underlying VM system.
174 int advise (int behavior
, int len
= -1);
176 /// Return the underlying <handle_>.
177 ACE_HANDLE
handle (void) const;
179 /// Return the name of file that is mapped (if any).
180 const ACE_TCHAR
*filename (void) const;
182 /// Dump the state of an object.
183 void dump (void) const;
185 /// Declare the dynamic allocation hooks.
186 ACE_ALLOC_HOOK_DECLARE
;
190 /// This method does the dirty work of actually calling ::mmap to map
191 /// the file into memory.
192 int map_it (ACE_HANDLE handle
,
193 size_t len
= static_cast<size_t> (-1),
194 int prot
= PROT_RDWR
,
195 int share
= MAP_SHARED
,
197 ACE_OFF_T offset
= 0,
198 LPSECURITY_ATTRIBUTES sa
= 0);
200 // = Disallow copying and assignment.
201 ACE_Mem_Map (const ACE_Mem_Map
&);
202 void operator = (const ACE_Mem_Map
&);
206 /// Base address of the memory-mapped file.
209 /// Name of the file that is mapped.
210 ACE_TCHAR filename_
[MAXPATHLEN
+ 1];
212 /// Length of the mapping.
215 /// HANDLE for the open file.
218 /// HANDLE for the open mapping.
219 ACE_HANDLE file_mapping_
;
221 /// Keeps track of whether we need to close the handle. This is set
222 /// if we opened the file.
227 ACE_END_VERSIONED_NAMESPACE_DECL
229 #if defined (__ACE_INLINE__)
230 #include "ace/Mem_Map.inl"
231 #endif /* __ACE_INLINE__ */
233 #include /**/ "ace/post.h"
235 #endif /* ACE_MEM_MAP_H */