Author: sebor
Date: Sun May 4 15:22:00 2008
New Revision: 653279
URL: http://svn.apache.org/viewvc?rev=653279&view=rev
Log:
2008-05-04 Martin Sebor <[EMAIL PROTECTED]>
STDCXX-908
* etc/config/src/MMAP.cpp: New test to check for mmap() and munmap()
in <sys/mman.h>.
* src/mman.cpp (__rw_mmap): Avoided using mmap() when _RWSTD_NO_MMAP
is #defined and instead dynamically allocated memory and copied the
file into it.
Defined _RWSTD_NO_MUNMAP when _RWSTD_NO_MMAP is #defined.
[_RWSTD_NO_MUNMAP](__rw_munmap): Avoided calling munmap() and instead
deallocated the block using operator delete.
Added:
stdcxx/branches/4.2.x/etc/config/src/MMAP.cpp (with props)
Modified:
stdcxx/branches/4.2.x/src/mman.cpp
Added: stdcxx/branches/4.2.x/etc/config/src/MMAP.cpp
URL:
http://svn.apache.org/viewvc/stdcxx/branches/4.2.x/etc/config/src/MMAP.cpp?rev=653279&view=auto
==============================================================================
--- stdcxx/branches/4.2.x/etc/config/src/MMAP.cpp (added)
+++ stdcxx/branches/4.2.x/etc/config/src/MMAP.cpp Sun May 4 15:22:00 2008
@@ -0,0 +1,46 @@
+// checking for mmap() and munmap() in <sys/mman.h>
+
+/***************************************************************************
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the License); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ *
+ * Copyright 2008 Rogue Wave Software, Inc.
+ *
+ **************************************************************************/
+
+#include <sys/mman.h>
+#include <sys/types.h>
+
+
+void* map_file (int fd)
+{
+ return mmap (0, 4096, PROT_READ, MAP_PRIVATE, fd, 0);
+}
+
+
+int main (int argc, char *argv[])
+{
+ // avoid executing the code except when one or more command
+ // line arguments have been specified
+ if (argc < 2)
+ return 0;
+
+ void *p = map_file (argc);
+ munmap (p, 0);
+
+ return 0;
+}
Propchange: stdcxx/branches/4.2.x/etc/config/src/MMAP.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: stdcxx/branches/4.2.x/etc/config/src/MMAP.cpp
------------------------------------------------------------------------------
svn:keywords = Id
Modified: stdcxx/branches/4.2.x/src/mman.cpp
URL:
http://svn.apache.org/viewvc/stdcxx/branches/4.2.x/src/mman.cpp?rev=653279&r1=653278&r2=653279&view=diff
==============================================================================
--- stdcxx/branches/4.2.x/src/mman.cpp (original)
+++ stdcxx/branches/4.2.x/src/mman.cpp Sun May 4 15:22:00 2008
@@ -22,7 +22,7 @@
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
- * Copyright 2001-2006 Rogue Wave Software.
+ * Copyright 2001-2008 Rogue Wave Software, Inc.
*
**************************************************************************/
@@ -36,9 +36,9 @@
#include <sys/stat.h>
-#ifndef _MSC_VER
+#ifndef _RWSTD_NO_MMAP
# include <sys/mman.h>
-#else
+#elif defined (_WIN32)
# include <windows.h>
# include <io.h>
#endif // _MSC_VER
@@ -69,12 +69,17 @@
*size = sb.st_size;
-#if !defined(_MSC_VER)
- const int fd = open (fname, O_RDONLY);
+#ifndef _WIN32
+ const int fd = open (fname, O_RDONLY);
+
if (-1 == fd)
return 0;
+#endif // _WIN32
+
+#ifndef _RWSTD_NO_MMAP
+
// On HPUX systems MAP_SHARED will prevent a second mapping of the same
// file if the regions are overlapping; one solution is to make the
// mapping private.
@@ -92,7 +97,9 @@
if (MAP_FAILED == data) // failure
return 0;
-#else
+
+#elif defined (_WIN32)
+
HANDLE mmf =
CreateFile (fname, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
@@ -114,6 +121,23 @@
CloseHandle (mmf);
CloseHandle (mmfv);
+#else // no mmap() or equivalent
+
+# ifndef _RWSTD_NO_MUNMAP
+# define _RWSTD_NO_MUNMAP
+# endif // _RWSTD_NO_MUNMAP
+
+ // read() takes a size_t argument, convert off_t to it
+ const size_t mapsize = size_t (sb.st_size);
+
+ void* data = operator new (mapsize);
+ const ssize_t nread = read (fd, data, mapsize);
+
+ if (size_t (nread) != mapsize) {
+ operator delete (data);
+ data = 0;
+ }
+
#endif // _MSC_VER
return data;
@@ -128,10 +152,12 @@
void* pv = _RWSTD_CONST_CAST (void*, pcv);
// POSIX munmap() takes a void*, but not all platforms conform
-#ifndef _MSC_VER
+#ifndef _RWSTD_NO_MUNMAP
munmap (_RWSTD_STATIC_CAST (_RWSTD_MUNMAP_ARG1_T, pv), size);
-#else
+#elif defined (_WIN32)
UnmapViewOfFile (pv);
+#else // no munmap()
+ operator delete (pv);
#endif // _MSC_VER
}