Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

MemoryMap.cc

Go to the documentation of this file.
00001 00002 #ifdef _MSC_VER 00003 #include <io.h> 00004 #else 00005 //#include <sys/io.h> 00006 #endif 00007 00008 00009 #include "MemoryMap.h" 00010 #include <plearn/base/general.h> 00011 #include <fcntl.h> 00012 00013 00014 #if !defined(_MSC_VER) && !defined(_MINGW_) 00015 00016 // NOT MICROSOFT: LINUX, SOLARIS, etc. 00017 #include <sys/mman.h> 00018 00019 #else 00020 00021 // OUINNEDOZE 00022 00023 //#define _X86_ 00024 #if !defined (_MINGW_) && !defined(WIN32) 00025 #include <varargs.h> 00026 #else 00027 //#include <windef.h> 00028 //#include <winbase.h> 00029 #endif 00030 00031 #endif 00032 // 00033 00034 #include <plearn/base/general.h> 00035 00036 //#include <sys/stat.h> 00037 00038 namespace PLearn { 00039 using namespace std; 00040 00041 #if !(!defined(_MSC_VER) && !defined(_MINGW_)) 00042 00044 static int fileSize(const char *filename) 00045 { 00046 struct stat t; 00047 stat(filename,&t); 00048 return t.st_size; 00049 } 00050 00052 void * memoryMap(const char *filename, tFileHandle & file_handle, bool readonly, int offset_, int length) 00053 { 00054 #if defined(_MINGW_) || defined(WIN32) 00055 PLERROR("memoryMap() - Not supported for MINGW"); 00056 return 0; 00057 #else 00058 if ((file_handle = (tFileHandle)CreateFile( filename, 00059 readonly ? GENERIC_READ: GENERIC_READ | GENERIC_WRITE, 00060 readonly ? FILE_SHARE_READ : 0, 00061 0, 00062 OPEN_ALWAYS, 00063 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, 00064 0))) 00065 { 00066 int file_size = fileSize(filename); 00067 HANDLE map_handle = CreateFileMapping( (LPVOID)file_handle, 00068 0, 00069 readonly ? PAGE_READONLY : PAGE_READWRITE, 00070 0, 00071 file_size, 00072 0); 00073 if (map_handle) 00074 return (LPVOID) MapViewOfFile( map_handle, 00075 readonly ? FILE_MAP_READ : FILE_MAP_WRITE, 00076 0, 00077 offset_, 00078 length ? length : file_size-offset_); 00079 else { 00080 PLERROR("In Storage: Could not open specified memory-mapping file for reading"); 00081 return 0; // to keep the compiler quiet 00082 } 00083 } 00084 else { 00085 PLERROR("In Storage: Could not open specified memory-mapping file for reading"); 00086 return 0; // to keep the compiler quiet 00087 } 00088 #endif 00089 } 00090 00092 bool memoryUnmap(void * p, tFileHandle file_handle) 00093 { 00094 #if defined(_MINGW_) || defined(WIN32) 00095 PLERROR("memoryUnmap - Not supported for MINGW"); 00096 return false; 00097 #else 00098 // first parameter: start address of byte range to flush 00099 // second parameter: number of bytes in range (0==flush all bytes) 00100 // flush==(linux)msync(...) 00101 if ((bool)FlushViewOfFile(p,0)) 00102 { 00103 // UnmapViewOfFile==(linux)munmap(...) 00104 bool b=UnmapViewOfFile(p); // address where mapped view begins 00105 CloseHandle((LPVOID)file_handle); 00106 return b; 00107 } 00108 else return false; 00109 #endif 00110 } 00111 00112 00113 #else // fin de version windows 00114 00116 void * MemoryMap(const char *filename,tFileHandle & handle, bool read_only, off_t & filesize) 00117 { 00118 void * addr; 00119 if (read_only) 00120 { 00121 handle = open(filename,O_RDONLY); 00122 if (handle<0) 00123 PLERROR("In Storage: Could not open specified memory-mapping file for reading"); 00124 // get the size of the file in bytes 00125 filesize = lseek(handle,0,SEEK_END); 00126 addr = mmap(0, filesize, PROT_READ, MAP_SHARED, handle, 0); 00127 } 00128 else { 00129 int handle = open(filename,O_RDWR); 00130 if (handle<0) 00131 PLERROR("In Storage: Could not open specified memory-mapping file for read-write"); 00132 filesize = lseek(handle,0,SEEK_END); 00133 00134 addr = mmap(0, filesize, PROT_READ|PROT_WRITE, MAP_SHARED, handle, 0); 00135 } 00136 return addr; 00137 } 00138 00140 //void FreeMemoryMap(void * data, tFileHandle handle, int length) 00141 void memoryUnmap(void * data, tFileHandle handle, int length) 00142 { 00143 msync((char*)data, length, MS_SYNC); 00144 munmap((char *)data, length); 00145 close(handle); 00146 } 00147 00148 #endif // version linux 00149 00150 } // end of namespace PLearn

Generated on Tue Aug 17 15:58:32 2004 for PLearn by doxygen 1.3.7