STXXL  1.4.0
 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 
23 
25 
26 
27 void mmap_file::serve(const request* req) throw (io_error)
28 {
29  scoped_mutex_lock fd_lock(fd_mutex);
30  assert(req->get_file() == this);
31  offset_type offset = req->get_offset();
32  void* buffer = req->get_buffer();
33  size_type bytes = req->get_size();
34  request::request_type type = req->get_type();
35 
36  //assert(offset + bytes <= _size());
37 
38  stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
39 
40  int prot = (type == request::READ) ? PROT_READ : PROT_WRITE;
41  void* mem = mmap(NULL, bytes, prot, MAP_SHARED, file_des, offset);
42  // void *mem = mmap (buffer, bytes, prot , MAP_SHARED|MAP_FIXED , file_des, offset);
43  // STXXL_MSG("Mmaped to "<<mem<<" , buffer suggested at "<<buffer);
44  if (mem == MAP_FAILED)
45  {
47  " mmap() failed." <<
48  " path=" << filename <<
49  " bytes=" << bytes <<
50  " Page size: " << sysconf(_SC_PAGESIZE) <<
51  " offset modulo page size " << (offset % sysconf(_SC_PAGESIZE)));
52  }
53  else if (mem == 0)
54  {
55  STXXL_THROW_ERRNO(io_error, "mmap() returned NULL");
56  }
57  else
58  {
59  if (type == request::READ)
60  {
61  memcpy(buffer, mem, bytes);
62  }
63  else
64  {
65  memcpy(mem, buffer, bytes);
66  }
67  STXXL_THROW_ERRNO_NE_0(munmap(mem, bytes), io_error,
68  "munmap() failed");
69  }
70 }
71 
72 const char* mmap_file::io_type() const
73 {
74  return "mmap";
75 }
76 
78 
79 #endif // #if STXXL_HAVE_MMAP_FILE
80 // vim: et:ts=4:sw=4
static const int bytes
number of bytes in uint_pair
Definition: uint_types.h:99
#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:66
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#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;.
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:64
Request with basic properties like file and offset.
Definition: request.h:39
#define STXXL_END_NAMESPACE
Definition: namespace.h:17