Thrill  0.1
mmap_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/mmap_file.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
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 
14 #include <foxxll/io/mmap_file.hpp>
15 
16 #if FOXXLL_HAVE_MMAP_FILE
17 
18 #include <sys/mman.h>
19 
21 #include <foxxll/io/iostats.hpp>
23 
24 namespace foxxll {
25 
26 void mmap_file::serve(void* buffer, offset_type offset, size_type bytes,
28 {
29  std::unique_lock<std::mutex> fd_lock(fd_mutex_);
30 
31  //assert(offset + bytes <= _size());
32 
33  file_stats::scoped_read_write_timer read_write_timer(
34  file_stats_, bytes, op == request::WRITE);
35 
36  int prot = (op == request::READ) ? PROT_READ : PROT_WRITE;
37  void* mem = mmap(nullptr, bytes, prot, MAP_SHARED, file_des_, offset);
38 
39  if (mem == MAP_FAILED)
40  {
42  io_error,
43  " mmap() failed." <<
44  " path=" << filename_ <<
45  " bytes=" << bytes <<
46  " Page size: " << sysconf(_SC_PAGESIZE) <<
47  " offset modulo page size " << (offset % sysconf(_SC_PAGESIZE))
48  );
49  }
50  else if (mem == 0)
51  {
52  FOXXLL_THROW_ERRNO(io_error, "mmap() returned nullptr");
53  }
54  else
55  {
56  if (op == request::READ)
57  {
58  memcpy(buffer, mem, bytes);
59  }
60  else
61  {
62  memcpy(mem, buffer, bytes);
63  }
65  munmap(mem, bytes), io_error,
66  "munmap() failed"
67  );
68  }
69 }
70 
71 const char* mmap_file::io_type() const
72 {
73  return "mmap";
74 }
75 
76 } // namespace foxxll
77 
78 #endif // #if FOXXLL_HAVE_MMAP_FILE
79 
80 /**************************************************************************/
#define FOXXLL_THROW_ERRNO(exception_type, error_message)
Throws exception_type with "Error in [function] : [error_message] : [errno message]".
FOXXLL library namespace
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.hpp:75
#define FOXXLL_THROW_ERRNO_NE_0(expr, exception_type, error_message)
Throws exception_type if (expr != 0) with "Error in [function] : [error_message] : [errno message]"...