00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00048 #ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE
00049 #define UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_MAJOR 4
00053 # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_MINOR 4
00054 # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_REVISION 4
00055 # define UNIXSTL_VER_UNIXSTL_FILESYSTEM_HPP_MEMORY_MAPPED_FILE_EDIT 92
00056 #endif
00057
00058
00059
00060
00061
00062 #ifndef UNIXSTL_INCL_UNIXSTL_H_UNIXSTL
00063 # include <unixstl/unixstl.h>
00064 #endif
00065 #ifndef UNIXSTL_INCL_UNIXSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
00066 # include <unixstl/filesystem/filesystem_traits.hpp>
00067 #endif
00068 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00069 # ifndef UNIXSTL_INCL_UNIXSTL_HPP_ERROR_UNIX_EXCEPTIONS
00070 # include <unixstl/error/exceptions.hpp>
00071 # endif
00072 #endif
00073 #ifndef STLSOFT_INCL_STLSOFT_SMARTPTR_HPP_SCOPED_HANDLE
00074 # include <stlsoft/smartptr/scoped_handle.hpp>
00075 #endif
00076 #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_STRING_H_FWD
00077 # include <stlsoft/shims/access/string/fwd.h>
00078 #endif
00079
00080 #ifndef STLSOFT_INCL_SYS_H_MMAN
00081 # define STLSOFT_INCL_SYS_H_MMAN
00082 # include <sys/mman.h>
00083 #endif
00084 #ifndef STLSOFT_INCL_SYS_H_STAT
00085 # define STLSOFT_INCL_SYS_H_STAT
00086 # include <sys/stat.h>
00087 #endif
00088
00089 #ifdef STLSOFT_UNITTEST
00090 # include <unixstl/filesystem/file_path_buffer.hpp>
00091 #endif
00092
00093
00094
00095
00096
00097 #ifndef _UNIXSTL_NO_NAMESPACE
00098 # if defined(_STLSOFT_NO_NAMESPACE) || \
00099 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00100
00101 namespace unixstl
00102 {
00103 # else
00104
00105
00106 namespace stlsoft
00107 {
00108
00109 namespace unixstl_project
00110 {
00111
00112 # endif
00113 #endif
00114
00115
00116
00117
00118
00123 class memory_mapped_file
00124 {
00127 private:
00129 typedef us_char_a_t char_type;
00131 typedef filesystem_traits<us_char_a_t> traits_type;
00132 public:
00134 typedef memory_mapped_file class_type;
00136 #ifdef STLSOFT_CF_64BIT_INT_SUPPORT
00137 typedef us_uint64_t size_type;
00138 #else
00139 typedef us_uint32_t size_type;
00140 #endif
00142 typedef int error_type;
00144 typedef off_t offset_type;
00146 typedef us_bool_t bool_type;
00148
00151 private:
00152 void open_(
00153 char_type const* fileName
00154 , offset_type offset
00155 , size_type requestSize
00156 )
00157 {
00158 scoped_handle<int> hfile( traits_type::open( fileName
00159 , O_RDONLY
00160 , PROT_READ)
00161 , &traits_type::close
00162 , -1);
00163
00164 if(hfile.empty())
00165 {
00166 on_error_("Failed to open file for mapping");
00167 }
00168 else
00169 {
00170 struct stat st;
00171
00172 if(0 != ::fstat(hfile.get(), &st))
00173 {
00174 on_error_("Failed to determine mapped file size");
00175 }
00176 else if(0 == st.st_size)
00177 {
00178 m_memory = NULL;
00179 m_cb = 0;
00180 }
00181 else
00182 {
00183 if(0 == requestSize)
00184 {
00185 requestSize = static_cast<size_type>(st.st_size);
00186 }
00187 else if(requestSize + offset > static_cast<size_type>(st.st_size))
00188 {
00189 requestSize = static_cast<size_type>(st.st_size) - offset;
00190 }
00191
00192 void* memory = ::mmap(NULL, static_cast<size_t>(requestSize), PROT_READ, MAP_PRIVATE, hfile.get(), offset);
00193
00194 if(MAP_FAILED == memory)
00195 {
00196 on_error_("Failed to map view of file");
00197 }
00198 else
00199 {
00200 m_memory = memory;
00201 m_cb = requestSize;
00202 }
00203 }
00204 }
00205 }
00207
00210 public:
00211 ss_explicit_k memory_mapped_file(char_type const* fileName)
00212 : m_cb(0)
00213 , m_memory(NULL)
00214 {
00215 open_(fileName, 0, 0);
00216 }
00217 template <ss_typename_param_k S>
00218 ss_explicit_k memory_mapped_file(S const& fileName)
00219 : m_cb(0)
00220 , m_memory(NULL)
00221 {
00222 open_(stlsoft_ns_qual(c_str_ptr)(fileName), 0, 0);
00223 }
00224 memory_mapped_file(
00225 char_type const* fileName
00226 , offset_type offset
00227 , size_type requestSize
00228 )
00229 : m_cb(0)
00230 , m_memory(NULL)
00231 {
00232 open_(fileName, offset, requestSize);
00233 }
00234 template <ss_typename_param_k S>
00235 memory_mapped_file(
00236 S const& fileName
00237 , offset_type offset
00238 , size_type requestSize
00239 )
00240 : m_cb(0)
00241 , m_memory(NULL)
00242 {
00243 open_(stlsoft_ns_qual(c_str_ptr)(fileName), offset, requestSize);
00244 }
00245
00247 ~memory_mapped_file() stlsoft_throw_0()
00248 {
00249 UNIXSTL_ASSERT(is_valid());
00250
00251 if(NULL != m_memory)
00252 {
00253 ::munmap(m_memory, static_cast<us_size_t>(m_cb));
00254 }
00255 }
00256
00258 void swap(class_type& rhs) stlsoft_throw_0()
00259 {
00260 UNIXSTL_ASSERT(is_valid());
00261
00262 std_swap(m_cb, rhs.m_cb);
00263 std_swap(m_memory, rhs.m_memory);
00264 #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
00265 std_swap(m_lastError, rhs.m_lastError);
00266 #endif
00267
00268 UNIXSTL_ASSERT(is_valid());
00269 }
00271
00274 public:
00277 void const* memory() const
00278 {
00279 return m_memory;
00280 }
00282 size_type size() const
00283 {
00284 return m_cb;
00285 }
00286
00287 #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
00288 error_type lastError() const
00289 {
00290 return m_lastError;
00291 }
00292 #endif
00294
00297 private:
00298 void on_error_(
00299 char const* message
00300 , error_type error = errno
00301 )
00302 {
00303 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00304 STLSOFT_THROW_X(unix_exception(message, error));
00305 #else
00306 m_lastError = error;
00307 #endif
00308 }
00309
00310 bool_type is_valid() const
00311 {
00312 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00313 if((NULL != m_memory) != (0 != m_cb))
00314 {
00315 return false;
00316 }
00317 #endif
00318
00319 return true;
00320 }
00322
00325 private:
00326 size_type m_cb;
00327 void* m_memory;
00328 #ifndef STLSOFT_CF_EXCEPTION_SUPPORT
00329 error_type m_lastError;
00330 #endif
00332
00335 private:
00336 memory_mapped_file(class_type const&);
00337 class_type& operator =(class_type const&);
00339 };
00340
00342
00343
00344 #ifdef STLSOFT_UNITTEST
00345 # include "./unittest/memory_mapped_file_unittest_.h"
00346 #endif
00347
00348
00349
00350 #ifndef _UNIXSTL_NO_NAMESPACE
00351 # if defined(_STLSOFT_NO_NAMESPACE) || \
00352 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00353 }
00354 # else
00355 }
00356 }
00357 # endif
00358 #endif
00359
00360 namespace std
00361 {
00362
00363 inline void swap(
00364 unixstl_ns_qual(memory_mapped_file)& lhs
00365 , unixstl_ns_qual(memory_mapped_file)& rhs
00366 )
00367 {
00368 lhs.swap(rhs);
00369 }
00370
00371 }
00372
00373
00374
00375 #endif
00376
00377