STXXL  1.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mmap_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/mmap_file.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008 Andreas Beckmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
15 
16 #if STXXL_HAVE_MMAP_FILE
17 
18 #include <stxxl/bits/io/iostats.h>
20 #include "ufs_platform.h"
21 #include <sys/mman.h>
22 
24 
25 void mmap_file::serve(void* buffer, offset_type offset, size_type bytes,
27 {
28  scoped_mutex_lock fd_lock(fd_mutex);
29 
30  //assert(offset + bytes <= _size());
31 
32  stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
33 
34  int prot = (type == request::READ) ? PROT_READ : PROT_WRITE;
35  void* mem = mmap(NULL, bytes, prot, MAP_SHARED, file_des, offset);
36  // void *mem = mmap (buffer, bytes, prot , MAP_SHARED|MAP_FIXED , file_des, offset);
37  // STXXL_MSG("Mmaped to "<<mem<<" , buffer suggested at "<<buffer);
38  if (mem == MAP_FAILED)
39  {
41  " mmap() failed." <<
42  " path=" << filename <<
43  " bytes=" << bytes <<
44  " Page size: " << sysconf(_SC_PAGESIZE) <<
45  " offset modulo page size " << (offset % sysconf(_SC_PAGESIZE)));
46  }
47  else if (mem == 0)
48  {
49  STXXL_THROW_ERRNO(io_error, "mmap() returned NULL");
50  }
51  else
52  {
53  if (type == request::READ)
54  {
55  memcpy(buffer, mem, bytes);
56  }
57  else
58  {
59  memcpy(mem, buffer, bytes);
60  }
61  STXXL_THROW_ERRNO_NE_0(munmap(mem, bytes), io_error,
62  "munmap() failed");
63  }
64 }
65 
66 const char* mmap_file::io_type() const
67 {
68  return "mmap";
69 }
70 
72 
73 #endif // #if STXXL_HAVE_MMAP_FILE
74 // vim: et:ts=4:sw=4
#define STXXL_THROW_ERRNO_NE_0(expr, exception_type, error_message)
Throws exception_type if (expr != 0) with &quot;Error in [function] : [error_message] : [errno message]&quot;...
request::size_type size_type
the size of a request
Definition: file.h:62
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:105
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
#define STXXL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with &quot;Error in [function] : [error_message] : [errno message]&quot;.
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.h:96
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:60
#define STXXL_END_NAMESPACE
Definition: namespace.h:17