3 //==========================================================================
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //==========================================================================
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/Global_Macros.h"
23 #include "ace/Copy_Disabled.h"
24 #include "ace/os_include/sys/os_mman.h"
25 #include "ace/os_include/os_limits.h"
26 #include "ace/os_include/os_fcntl.h"
27 #include "ace/Default_Constants.h"
29 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
34 * @brief C++ interface OS memory mapping system call.
36 * This class works with both the mmap(2) UNIX system and the
37 * Win32 family of memory mapping system calls.
39 class ACE_Export ACE_Mem_Map
: private ACE_Copy_Disabled
42 /// Default constructor.
45 /// Map a file from an open file descriptor @a handle. This function
46 /// will lookup the length of the file if it is not given.
47 ACE_Mem_Map (ACE_HANDLE handle
,
48 size_t length
= static_cast<size_t> (-1),
50 int share
= ACE_MAP_PRIVATE
,
53 LPSECURITY_ATTRIBUTES sa
= 0);
55 /// Map a file specified by @a file_name.
56 ACE_Mem_Map (const ACE_TCHAR
*filename
,
57 size_t length
= static_cast<size_t> (-1),
58 int flags
= O_RDWR
| O_CREAT
,
59 mode_t mode
= ACE_DEFAULT_FILE_PERMS
,
61 int share
= ACE_MAP_PRIVATE
,
64 LPSECURITY_ATTRIBUTES sa
= 0);
66 /// Map a file from an open file descriptor @a handle. This function
67 /// will lookup the length of the file if it is not given.
68 int map (ACE_HANDLE handle
,
69 size_t length
= static_cast<size_t> (-1),
71 int share
= ACE_MAP_PRIVATE
,
74 LPSECURITY_ATTRIBUTES sa
= 0);
76 /// Remap the file associated with <handle_>.
77 int map (size_t length
= static_cast<size_t> (-1),
79 int share
= ACE_MAP_PRIVATE
,
82 LPSECURITY_ATTRIBUTES sa
= 0);
84 /// Map a file specified by @a filename.
85 int map (const ACE_TCHAR
*filename
,
86 size_t length
= static_cast<size_t> (-1),
87 int flags
= O_RDWR
| O_CREAT
,
88 mode_t mode
= ACE_DEFAULT_FILE_PERMS
,
90 int share
= ACE_MAP_PRIVATE
,
93 LPSECURITY_ATTRIBUTES sa
= 0);
98 /// Open the file without mapping it.
99 int open (const ACE_TCHAR
*filename
,
100 int flags
= O_RDWR
| O_CREAT
,
101 mode_t perms
= ACE_DEFAULT_FILE_PERMS
,
102 LPSECURITY_ATTRIBUTES sa
= 0);
104 /// Close down the <handle_> if necessary and unmap the mapping.
107 /// Close down the <handle_> if necessary.
111 * Close down the internal <file_mapping_> if necessary. This is
112 * mostly necessary on Win32, which has a different handle for
113 * file-mapping kernel object.
115 int close_filemapping_handle ();
117 /// This operator passes back the starting address of the mapped
119 int operator () (void *&addr
);
121 /// Return the base address.
124 /// This function returns the number of bytes currently mapped in the
126 size_t size () const;
128 /// Unmap the region starting at base_addr_.
129 int unmap (ssize_t len
= -1);
131 /// Unmap the region starting at addr_.
132 int unmap (void *addr
, ssize_t len
);
135 * Sync @a len bytes of the memory region to the backing store
136 * starting at base_addr_. If @a len == -1 then sync the whole
139 int sync (size_t len
, int flags
= MS_SYNC
);
142 * Sync the whole memory region to the backing store
143 * starting at base_addr_.
145 int sync (int flags
= MS_SYNC
);
147 /// Sync @a len bytes of the memory region to the backing store
148 /// starting at addr_.
149 int sync (void *addr
, size_t len
, int flags
= MS_SYNC
);
152 * Change the protection of the pages of the mapped region to @a prot
153 * starting at base_addr_ up to @a len bytes.
155 int protect (size_t len
, int prot
= PROT_RDWR
);
158 * Change the protection of all the pages of the mapped region to @a prot
159 * starting at base_addr_.
161 int protect (int prot
= PROT_RDWR
);
163 /// Change the protection of the pages of the mapped region to @a prot
164 /// starting at @a addr up to @a len bytes.
165 int protect (void *addr
, size_t len
, int prot
= PROT_RDWR
);
167 /// Close and remove the file from the file system.
170 /// Hook into the underlying VM system.
171 int advise (int behavior
, int len
= -1);
173 /// Return the underlying <handle_>.
174 ACE_HANDLE
handle () const;
176 /// Return the name of file that is mapped (if any).
177 const ACE_TCHAR
*filename () const;
179 /// Dump the state of an object.
182 /// Declare the dynamic allocation hooks.
183 ACE_ALLOC_HOOK_DECLARE
;
186 /// This method does the dirty work of actually calling ::mmap to map
187 /// the file into memory.
188 int map_it (ACE_HANDLE handle
,
189 size_t len
= static_cast<size_t> (-1),
190 int prot
= PROT_RDWR
,
191 int share
= MAP_SHARED
,
193 ACE_OFF_T offset
= 0,
194 LPSECURITY_ATTRIBUTES sa
= 0);
197 /// Base address of the memory-mapped file.
200 /// Name of the file that is mapped.
201 ACE_TCHAR filename_
[MAXPATHLEN
+ 1];
203 /// Length of the mapping.
206 /// HANDLE for the open file.
209 /// HANDLE for the open mapping.
210 ACE_HANDLE file_mapping_
;
212 /// Keeps track of whether we need to close the handle. This is set
213 /// if we opened the file.
217 ACE_END_VERSIONED_NAMESPACE_DECL
219 #if defined (__ACE_INLINE__)
220 #include "ace/Mem_Map.inl"
221 #endif /* __ACE_INLINE__ */
223 #include /**/ "ace/post.h"
225 #endif /* ACE_MEM_MAP_H */