Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Mem_Map.h
blob9f728fa2e09f9bd6a50b7e3430fbfc5c497164a9
1 // -*- C++ -*-
3 //==========================================================================
4 /**
5 * @file Mem_Map.h
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
8 */
9 //==========================================================================
11 #ifndef ACE_MEM_MAP_H
12 #define ACE_MEM_MAP_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/ACE_export.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 # 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
31 /**
32 * @class ACE_Mem_Map
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
41 public:
42 /// Default constructor.
43 ACE_Mem_Map ();
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),
49 int prot = PROT_RDWR,
50 int share = ACE_MAP_PRIVATE,
51 void *addr = 0,
52 ACE_OFF_T offset = 0,
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,
60 int prot = PROT_RDWR,
61 int share = ACE_MAP_PRIVATE,
62 void *addr = 0,
63 ACE_OFF_T offset = 0,
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),
70 int prot = PROT_RDWR,
71 int share = ACE_MAP_PRIVATE,
72 void *addr = 0,
73 ACE_OFF_T offset = 0,
74 LPSECURITY_ATTRIBUTES sa = 0);
76 /// Remap the file associated with <handle_>.
77 int map (size_t length = static_cast<size_t> (-1),
78 int prot = PROT_RDWR,
79 int share = ACE_MAP_PRIVATE,
80 void *addr = 0,
81 ACE_OFF_T offset = 0,
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,
89 int prot = PROT_RDWR,
90 int share = ACE_MAP_PRIVATE,
91 void *addr = 0,
92 ACE_OFF_T offset = 0,
93 LPSECURITY_ATTRIBUTES sa = 0);
95 /// Destructor.
96 ~ACE_Mem_Map ();
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.
105 int close ();
107 /// Close down the <handle_> if necessary.
108 int close_handle ();
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
118 /// file.
119 int operator () (void *&addr);
121 /// Return the base address.
122 void *addr () const;
124 /// This function returns the number of bytes currently mapped in the
125 /// file.
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
137 * region.
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.
168 int remove ();
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.
180 void dump () const;
182 /// Declare the dynamic allocation hooks.
183 ACE_ALLOC_HOOK_DECLARE;
185 private:
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,
192 void *addr = 0,
193 ACE_OFF_T offset = 0,
194 LPSECURITY_ATTRIBUTES sa = 0);
196 private:
197 /// Base address of the memory-mapped file.
198 void *base_addr_;
200 /// Name of the file that is mapped.
201 ACE_TCHAR filename_[MAXPATHLEN + 1];
203 /// Length of the mapping.
204 size_t length_;
206 /// HANDLE for the open file.
207 ACE_HANDLE handle_;
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.
214 bool close_handle_;
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 */