2 #include "ace/OS_NS_fcntl.h"
3 #include "ace/OS_NS_unistd.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "ace/OS_NS_macros.h"
6 #include "ace/OS_NS_errno.h"
8 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
10 #if defined (ACE_HAS_VOIDPTR_MMAP)
11 // Needed for some odd OS's
12 typedef void *ACE_MMAP_TYPE;
14 typedef char *ACE_MMAP_TYPE;
15 #endif /* ACE_HAS_VOIDPTR_MMAP */
18 ACE_OS::madvise (caddr_t addr, size_t len, int map_advice)
20 ACE_OS_TRACE ("ACE_OS::madvise");
21 #if !defined (ACE_LACKS_MADVISE)
22 return ::madvise (addr, len, map_advice);
24 ACE_UNUSED_ARG (addr);
26 ACE_UNUSED_ARG (map_advice);
27 ACE_NOTSUP_RETURN (-1);
28 #endif /* ACE_WIN32 */
32 ACE_OS::mmap (void *addr,
36 ACE_HANDLE file_handle,
38 ACE_HANDLE *file_mapping,
39 LPSECURITY_ATTRIBUTES sa,
40 const ACE_TCHAR *file_mapping_name)
42 ACE_OS_TRACE ("ACE_OS::mmap");
43 #if !defined (ACE_WIN32)
44 ACE_UNUSED_ARG (file_mapping_name);
45 #endif /* !defined (ACE_WIN32) */
47 #if defined (ACE_WIN32)
48 if (!ACE_BIT_ENABLED (flags, MAP_FIXED))
50 else if (addr == 0) // can not map to address 0
57 ACE_HANDLE local_handle = ACE_INVALID_HANDLE;
59 // Ensure that file_mapping is non-zero.
60 if (file_mapping == 0)
61 file_mapping = &local_handle;
63 if (ACE_BIT_ENABLED (flags, MAP_PRIVATE))
65 prot = PAGE_WRITECOPY;
66 nt_flags = FILE_MAP_COPY;
68 else if (ACE_BIT_ENABLED (flags, MAP_SHARED))
70 if (ACE_BIT_ENABLED (prot, PAGE_READONLY))
71 nt_flags = FILE_MAP_READ;
72 if (ACE_BIT_ENABLED (prot, PAGE_READWRITE))
73 nt_flags = FILE_MAP_WRITE;
76 // Only create a new handle if we didn't have a valid one passed in.
77 if (*file_mapping == ACE_INVALID_HANDLE)
79 SECURITY_ATTRIBUTES sa_buffer;
80 SECURITY_DESCRIPTOR sd_buffer;
81 LPSECURITY_ATTRIBUTES const attr =
82 ACE_OS::default_win32_security_attributes_r (sa,
87 const DWORD len_low = static_cast<DWORD>(len),
88 len_high = static_cast<DWORD>(len >> 32);
90 const DWORD len_low = static_cast<DWORD>(len), len_high = 0;
93 *file_mapping = ACE_TEXT_CreateFileMapping (file_handle,
96 (file_handle == ACE_INVALID_HANDLE) ? len_high : 0,
97 (file_handle == ACE_INVALID_HANDLE) ? len_low : 0,
101 if (*file_mapping == 0)
102 ACE_FAIL_RETURN (MAP_FAILED);
104 # if defined (ACE_OS_EXTRA_MMAP_FLAGS)
105 nt_flags |= ACE_OS_EXTRA_MMAP_FLAGS;
106 # endif /* ACE_OS_EXTRA_MMAP_FLAGS */
108 DWORD low_off = ACE_LOW_PART (off);
109 DWORD high_off = ACE_HIGH_PART (off);
111 void *addr_mapping = ::MapViewOfFileEx (*file_mapping,
118 // Only close this down if we used the temporary.
119 if (file_mapping == &local_handle)
120 ::CloseHandle (*file_mapping);
122 if (addr_mapping == 0)
123 ACE_FAIL_RETURN (MAP_FAILED);
126 #elif !defined (ACE_LACKS_MMAP)
129 # if defined (ACE_OS_EXTRA_MMAP_FLAGS)
130 flags |= ACE_OS_EXTRA_MMAP_FLAGS;
131 # endif /* ACE_OS_EXTRA_MMAP_FLAGS */
132 ACE_UNUSED_ARG (file_mapping);
133 //FUZZ: disable check_for_lack_ACE_OS
134 ACE_OSCALL_RETURN ((void *) ::mmap ((ACE_MMAP_TYPE) addr,
141 //FUZZ: enable check_for_lack_ACE_OS
143 ACE_UNUSED_ARG (addr);
144 ACE_UNUSED_ARG (len);
145 ACE_UNUSED_ARG (prot);
146 ACE_UNUSED_ARG (flags);
147 ACE_UNUSED_ARG (file_handle);
148 ACE_UNUSED_ARG (off);
149 ACE_UNUSED_ARG (file_mapping);
151 ACE_NOTSUP_RETURN (MAP_FAILED);
152 #endif /* ACE_WIN32 */
155 // Implements simple read/write control for pages. Affects a page if
156 // part of the page is referenced. Currently PROT_READ, PROT_WRITE,
157 // and PROT_RDWR has been mapped in OS.h. This needn't have anything
158 // to do with a mmap region.
161 ACE_OS::mprotect (void *addr, size_t len, int prot)
163 ACE_OS_TRACE ("ACE_OS::mprotect");
164 #if defined (ACE_WIN32)
165 DWORD dummy; // Sigh!
166 return ::VirtualProtect(addr, len, prot, &dummy) ? 0 : -1;
167 #elif !defined (ACE_LACKS_MPROTECT)
168 return ::mprotect ((ACE_MMAP_TYPE) addr, len, prot);
170 ACE_UNUSED_ARG (addr);
171 ACE_UNUSED_ARG (len);
172 ACE_UNUSED_ARG (prot);
173 ACE_NOTSUP_RETURN (-1);
174 #endif /* ACE_WIN32 */
178 ACE_OS::msync (void *addr, size_t len, int sync)
180 ACE_OS_TRACE ("ACE_OS::msync");
181 #if defined (ACE_WIN32)
182 ACE_UNUSED_ARG (sync);
183 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::FlushViewOfFile (addr, len), ace_result_), int, -1);
184 #elif !defined (ACE_LACKS_MSYNC)
185 return ::msync ((ACE_MMAP_TYPE) addr, len, sync);
187 ACE_UNUSED_ARG (addr);
188 ACE_UNUSED_ARG (len);
189 ACE_UNUSED_ARG (sync);
190 ACE_NOTSUP_RETURN (-1);
191 #endif /* ACE_WIN32 */
195 ACE_OS::munmap (void *addr, size_t len)
197 ACE_OS_TRACE ("ACE_OS::munmap");
198 #if defined (ACE_WIN32)
199 ACE_UNUSED_ARG (len);
201 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::UnmapViewOfFile (addr), ace_result_), int, -1);
202 #elif !defined (ACE_LACKS_MMAP) && !defined (ACE_LACKS_MUNMAP)
203 return ::munmap ((ACE_MMAP_TYPE) addr, len);
205 ACE_UNUSED_ARG (addr);
206 ACE_UNUSED_ARG (len);
207 ACE_NOTSUP_RETURN (-1);
208 #endif /* ACE_WIN32 */
211 ACE_INLINE ACE_HANDLE
212 ACE_OS::shm_open (const ACE_TCHAR *filename,
215 LPSECURITY_ATTRIBUTES sa)
217 ACE_OS_TRACE ("ACE_OS::shm_open");
218 #if defined (ACE_HAS_SHM_OPEN)
220 #if defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670)
221 // With VxWorks the file should just start with / and no other
222 // slashes, so replace all other / by _
223 ACE_TCHAR buf [MAXPATHLEN + 1];
224 ACE_OS::sprintf (buf,
227 for (size_t i = 1; i < MAXPATHLEN + 1; i++)
236 return ::shm_open (ACE_TEXT_ALWAYS_CHAR(filename), mode, perms);
237 #else /* ! ACE_HAS_SHM_OPEN */
239 return ACE_OS::open (filename, mode, perms, sa);
240 #endif /* ACE_HAS_SHM_OPEN */
244 ACE_OS::shm_unlink (const ACE_TCHAR *path)
246 ACE_OS_TRACE ("ACE_OS::shm_unlink");
247 #if defined (ACE_HAS_SHM_OPEN)
248 # if defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670)
249 // With VxWorks the file should just start with / and no other
250 // slashes, so replace all other / by _
251 ACE_TCHAR buf [MAXPATHLEN + 1];
252 ACE_OS::sprintf (buf,
255 for (size_t i = 1; i < MAXPATHLEN + 1; i++)
264 # if defined (ACE_LACKS_SHM_UNLINK)
265 ACE_UNUSED_ARG (path);
266 ACE_NOTSUP_RETURN (-1);
268 return ::shm_unlink (ACE_TEXT_ALWAYS_CHAR(path));
269 # endif /* ACE_LACKS_SHM_UNLINK */
270 #else /* ! ACE_HAS_SHM_OPEN */
271 // Just use ::unlink.
272 return ACE_OS::unlink (path);
273 #endif /* ACE_HAS_SHM_OPEN */
276 ACE_END_VERSIONED_NAMESPACE_DECL