00001
00002
00003
00004
00005 #include "ace/OS_NS_fcntl.h"
00006 #include "ace/OS_NS_unistd.h"
00007 #include "ace/OS_NS_stdio.h"
00008 #include "ace/OS_NS_macros.h"
00009 #include "ace/OS_NS_errno.h"
00010
00011 #if defined (__Lynx__)
00012 # include "ace/OS_NS_sys_stat.h"
00013 #endif
00014
00015 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00016
00017 #if defined (ACE_HAS_VOIDPTR_MMAP)
00018
00019 typedef void *ACE_MMAP_TYPE;
00020 #else
00021 typedef char *ACE_MMAP_TYPE;
00022 #endif
00023
00024 ACE_INLINE int
00025 ACE_OS::madvise (caddr_t addr, size_t len, int map_advice)
00026 {
00027 ACE_OS_TRACE ("ACE_OS::madvise");
00028 #if !defined (ACE_LACKS_MADVISE)
00029 ACE_OSCALL_RETURN (::madvise (addr, len, map_advice), int, -1);
00030 #else
00031 ACE_UNUSED_ARG (addr);
00032 ACE_UNUSED_ARG (len);
00033 ACE_UNUSED_ARG (map_advice);
00034 ACE_NOTSUP_RETURN (-1);
00035 #endif
00036 }
00037
00038 ACE_INLINE void *
00039 ACE_OS::mmap (void *addr,
00040 size_t len,
00041 int prot,
00042 int flags,
00043 ACE_HANDLE file_handle,
00044 off_t off,
00045 ACE_HANDLE *file_mapping,
00046 LPSECURITY_ATTRIBUTES sa,
00047 const ACE_TCHAR *file_mapping_name)
00048 {
00049 ACE_OS_TRACE ("ACE_OS::mmap");
00050 #if !defined (ACE_WIN32) || defined (ACE_HAS_PHARLAP)
00051 ACE_UNUSED_ARG (file_mapping_name);
00052 #endif
00053
00054 #if defined (ACE_WIN32) && !defined (ACE_HAS_PHARLAP)
00055
00056 # if defined(ACE_HAS_WINCE)
00057 ACE_UNUSED_ARG (addr);
00058 if (ACE_BIT_ENABLED (flags, MAP_FIXED))
00059 {
00060 errno = EINVAL;
00061 return MAP_FAILED;
00062 }
00063 # else
00064 if (!ACE_BIT_ENABLED (flags, MAP_FIXED))
00065 addr = 0;
00066 else if (addr == 0)
00067 {
00068 errno = EINVAL;
00069 return MAP_FAILED;
00070 }
00071 # endif
00072
00073 int nt_flags = 0;
00074 ACE_HANDLE local_handle = ACE_INVALID_HANDLE;
00075
00076
00077 if (file_mapping == 0)
00078 file_mapping = &local_handle;
00079
00080 if (ACE_BIT_ENABLED (flags, MAP_PRIVATE))
00081 {
00082 # if !defined(ACE_HAS_WINCE)
00083 prot = PAGE_WRITECOPY;
00084 # endif // ACE_HAS_WINCE
00085 nt_flags = FILE_MAP_COPY;
00086 }
00087 else if (ACE_BIT_ENABLED (flags, MAP_SHARED))
00088 {
00089 if (ACE_BIT_ENABLED (prot, PAGE_READONLY))
00090 nt_flags = FILE_MAP_READ;
00091 if (ACE_BIT_ENABLED (prot, PAGE_READWRITE))
00092 nt_flags = FILE_MAP_WRITE;
00093 }
00094
00095
00096 if (*file_mapping == ACE_INVALID_HANDLE)
00097 {
00098 # if !defined(ACE_HAS_WINCE) && (!defined (ACE_HAS_WINNT4) || (ACE_HAS_WINNT4 == 0))
00099 int try_create = 1;
00100 if ((file_mapping_name != 0) && (*file_mapping_name != 0))
00101 {
00102
00103
00104
00105
00106
00107 *file_mapping = ACE_TEXT_OpenFileMapping (nt_flags,
00108 0,
00109 file_mapping_name);
00110 if (*file_mapping != 0
00111 || (::GetLastError () == ERROR_INVALID_NAME
00112 && ::GetLastError () == ERROR_FILE_NOT_FOUND))
00113 try_create = 0;
00114 }
00115
00116 if (try_create)
00117 # endif
00118 {
00119 SECURITY_ATTRIBUTES sa_buffer;
00120 SECURITY_DESCRIPTOR sd_buffer;
00121 const LPSECURITY_ATTRIBUTES attr =
00122 ACE_OS::default_win32_security_attributes_r (sa,
00123 &sa_buffer,
00124 &sd_buffer);
00125
00126 *file_mapping = ACE_TEXT_CreateFileMapping (file_handle,
00127 attr,
00128 prot,
00129 0,
00130 0,
00131 file_mapping_name);
00132 }
00133 }
00134
00135 if (*file_mapping == 0)
00136 ACE_FAIL_RETURN (MAP_FAILED);
00137
00138 # if defined (ACE_OS_EXTRA_MMAP_FLAGS)
00139 nt_flags |= ACE_OS_EXTRA_MMAP_FLAGS;
00140 # endif
00141
00142 # if !defined (ACE_HAS_WINCE)
00143 void *addr_mapping = ::MapViewOfFileEx (*file_mapping,
00144 nt_flags,
00145 0,
00146 off,
00147 len,
00148 addr);
00149 # else
00150 void *addr_mapping = ::MapViewOfFile (*file_mapping,
00151 nt_flags,
00152 0,
00153 off,
00154 len);
00155 # endif
00156
00157
00158 if (file_mapping == &local_handle)
00159 ::CloseHandle (*file_mapping);
00160
00161 if (addr_mapping == 0)
00162 ACE_FAIL_RETURN (MAP_FAILED);
00163 else
00164 return addr_mapping;
00165 #elif defined (ACE_HAS_LYNXOS_BROKEN_MMAP)
00166
00167
00168 ACE_UNUSED_ARG (sa);
00169
00170 char name [128];
00171 sprintf (name, "%d", file_handle);
00172
00173
00174
00175
00176
00177 int shm_handle;
00178 if (! file_mapping)
00179 file_mapping = &shm_handle;
00180 if ((*file_mapping = ::shm_open (name,
00181 O_RDWR | O_CREAT | O_TRUNC,
00182 ACE_DEFAULT_FILE_PERMS)) == -1)
00183 return MAP_FAILED;
00184 else
00185 {
00186
00187 const off_t filesize = ACE_OS::filesize (file_handle);
00188 if (::ftruncate (*file_mapping, filesize) == -1)
00189 return MAP_FAILED;
00190 else
00191 {
00192 # if defined (ACE_OS_EXTRA_MMAP_FLAGS)
00193 flags |= ACE_OS_EXTRA_MMAP_FLAGS;
00194 # endif
00195 char *map = (char *) ::mmap ((ACE_MMAP_TYPE) addr,
00196 len,
00197 prot,
00198 flags,
00199 *file_mapping,
00200 off);
00201 if (map == MAP_FAILED)
00202 return MAP_FAILED;
00203 else
00204
00205 return ::read (file_handle, map, (int) filesize) == filesize
00206 ? map
00207 : MAP_FAILED;
00208 }
00209 }
00210 #elif !defined (ACE_LACKS_MMAP)
00211 ACE_UNUSED_ARG (sa);
00212
00213 # if defined (ACE_OS_EXTRA_MMAP_FLAGS)
00214 flags |= ACE_OS_EXTRA_MMAP_FLAGS;
00215 # endif
00216 ACE_UNUSED_ARG (file_mapping);
00217 # if defined (ACE_OPENVMS)
00218 ::fsync(file_handle);
00219 # endif
00220 ACE_OSCALL_RETURN ((void *) ::mmap ((ACE_MMAP_TYPE) addr,
00221 len,
00222 prot,
00223 flags,
00224 file_handle,
00225 off),
00226 void *, MAP_FAILED);
00227 #else
00228 ACE_UNUSED_ARG (addr);
00229 ACE_UNUSED_ARG (len);
00230 ACE_UNUSED_ARG (prot);
00231 ACE_UNUSED_ARG (flags);
00232 ACE_UNUSED_ARG (file_handle);
00233 ACE_UNUSED_ARG (off);
00234 ACE_UNUSED_ARG (file_mapping);
00235 ACE_UNUSED_ARG (sa);
00236 ACE_NOTSUP_RETURN (MAP_FAILED);
00237 #endif
00238 }
00239
00240
00241
00242
00243
00244
00245 ACE_INLINE int
00246 ACE_OS::mprotect (void *addr, size_t len, int prot)
00247 {
00248 ACE_OS_TRACE ("ACE_OS::mprotect");
00249 #if defined (ACE_WIN32) && !defined (ACE_HAS_PHARLAP)
00250 DWORD dummy;
00251 return ::VirtualProtect(addr, len, prot, &dummy) ? 0 : -1;
00252 #elif !defined (ACE_LACKS_MPROTECT)
00253 ACE_OSCALL_RETURN (::mprotect ((ACE_MMAP_TYPE) addr, len, prot), int, -1);
00254 #else
00255 ACE_UNUSED_ARG (addr);
00256 ACE_UNUSED_ARG (len);
00257 ACE_UNUSED_ARG (prot);
00258 ACE_NOTSUP_RETURN (-1);
00259 #endif
00260 }
00261
00262 ACE_INLINE int
00263 ACE_OS::msync (void *addr, size_t len, int sync)
00264 {
00265 ACE_OS_TRACE ("ACE_OS::msync");
00266 #if defined (ACE_WIN32) && !defined (ACE_HAS_PHARLAP)
00267 ACE_UNUSED_ARG (sync);
00268
00269 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::FlushViewOfFile (addr, len), ace_result_), int, -1);
00270 #elif !defined (ACE_LACKS_MSYNC)
00271 # if !defined (ACE_HAS_BROKEN_NETBSD_MSYNC)
00272 ACE_OSCALL_RETURN (::msync ((ACE_MMAP_TYPE) addr, len, sync), int, -1);
00273 # else
00274 ACE_OSCALL_RETURN (::msync ((ACE_MMAP_TYPE) addr, len), int, -1);
00275 ACE_UNUSED_ARG (sync);
00276 # endif
00277 #else
00278 ACE_UNUSED_ARG (addr);
00279 ACE_UNUSED_ARG (len);
00280 ACE_UNUSED_ARG (sync);
00281 ACE_NOTSUP_RETURN (-1);
00282 #endif
00283 }
00284
00285 ACE_INLINE int
00286 ACE_OS::munmap (void *addr, size_t len)
00287 {
00288 ACE_OS_TRACE ("ACE_OS::munmap");
00289 #if defined (ACE_WIN32)
00290 ACE_UNUSED_ARG (len);
00291
00292 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::UnmapViewOfFile (addr), ace_result_), int, -1);
00293 #elif !defined (ACE_LACKS_MMAP)
00294 ACE_OSCALL_RETURN (::munmap ((ACE_MMAP_TYPE) addr, len), int, -1);
00295 #else
00296 ACE_UNUSED_ARG (addr);
00297 ACE_UNUSED_ARG (len);
00298 ACE_NOTSUP_RETURN (-1);
00299 #endif
00300 }
00301
00302 ACE_INLINE ACE_HANDLE
00303 ACE_OS::shm_open (const ACE_TCHAR *filename,
00304 int mode,
00305 int perms,
00306 LPSECURITY_ATTRIBUTES sa)
00307 {
00308 ACE_OS_TRACE ("ACE_OS::shm_open");
00309 # if defined (ACE_HAS_SHM_OPEN)
00310 ACE_UNUSED_ARG (sa);
00311 ACE_OSCALL_RETURN (::shm_open (ACE_TEXT_ALWAYS_CHAR(filename), mode, perms), ACE_HANDLE, ACE_INVALID_HANDLE);
00312 # elif defined (ACE_OPENVMS)
00313 ACE_OSCALL_RETURN (::open (filename, mode, perms, ACE_TEXT("shr=get,put,upd")), ACE_HANDLE, ACE_INVALID_HANDLE);
00314 # else
00315
00316 return ACE_OS::open (filename, mode, perms, sa);
00317 # endif
00318 }
00319
00320 ACE_INLINE int
00321 ACE_OS::shm_unlink (const ACE_TCHAR *path)
00322 {
00323 ACE_OS_TRACE ("ACE_OS::shm_unlink");
00324 # if defined (ACE_HAS_SHM_OPEN)
00325 ACE_OSCALL_RETURN (::shm_unlink (ACE_TEXT_ALWAYS_CHAR(path)), int, -1);
00326 # else
00327
00328 return ACE_OS::unlink (path);
00329 # endif
00330 }
00331
00332 ACE_END_VERSIONED_NAMESPACE_DECL