STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
wincall_file.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * lib/io/wincall_file.cpp
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2005-2006 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008-2010 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_WINCALL_FILE
17 
18 #include <stxxl/bits/io/iostats.h>
20 
21 #ifndef NOMINMAX
22  #define NOMINMAX
23 #endif
24 #include <windows.h>
25 
27 
28 
29 void wincall_file::serve(const request* req) throw (io_error)
30 {
31  scoped_mutex_lock fd_lock(fd_mutex);
32  assert(req->get_file() == this);
33  offset_type offset = req->get_offset();
34  void* buffer = req->get_buffer();
35  size_type bytes = req->get_size();
36  request::request_type type = req->get_type();
37 
38  if (bytes > 32 * 1024 * 1024) {
39  STXXL_ERRMSG("Using a block size larger than 32 MiB may not work with the " << io_type() << " filetype");
40  }
41 
42  HANDLE handle = file_des;
43  LARGE_INTEGER desired_pos;
44  desired_pos.QuadPart = offset;
45  if (!SetFilePointerEx(handle, desired_pos, NULL, FILE_BEGIN))
46  {
48  "SetFilePointerEx in wincall_request::serve()" <<
49  " offset=" << offset <<
50  " this=" << this <<
51  " buffer=" << buffer <<
52  " bytes=" << bytes <<
53  " type=" << ((type == request::READ) ? "READ" : "WRITE"));
54  }
55  else
56  {
57  stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
58 
59  if (type == request::READ)
60  {
61  DWORD NumberOfBytesRead = 0;
62  assert(bytes <= std::numeric_limits<DWORD>::max());
63  if (!ReadFile(handle, buffer, (DWORD)bytes, &NumberOfBytesRead, NULL))
64  {
66  "ReadFile" <<
67  " this=" << this <<
68  " offset=" << offset <<
69  " buffer=" << buffer <<
70  " bytes=" << bytes <<
71  " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
72  " NumberOfBytesRead= " << NumberOfBytesRead);
73  }
74  else if (NumberOfBytesRead != bytes) {
75  STXXL_THROW_WIN_LASTERROR(io_error, " partial read: missing " << (bytes - NumberOfBytesRead) << " out of " << bytes << " bytes");
76  }
77  }
78  else
79  {
80  DWORD NumberOfBytesWritten = 0;
81  assert(bytes <= std::numeric_limits<DWORD>::max());
82  if (!WriteFile(handle, buffer, (DWORD)bytes, &NumberOfBytesWritten, NULL))
83  {
85  "WriteFile" <<
86  " this=" << this <<
87  " offset=" << offset <<
88  " buffer=" << buffer <<
89  " bytes=" << bytes <<
90  " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
91  " NumberOfBytesWritten= " << NumberOfBytesWritten);
92  }
93  else if (NumberOfBytesWritten != bytes) {
94  STXXL_THROW_WIN_LASTERROR(io_error, " partial write: missing " << (bytes - NumberOfBytesWritten) << " out of " << bytes << " bytes");
95  }
96  }
97  }
98 }
99 
100 const char* wincall_file::io_type() const
101 {
102  return "wincall";
103 }
104 
106 
107 #endif // #if STXXL_HAVE_WINCALL_FILE
108 // vim: et:ts=4:sw=4
static const int bytes
number of bytes in uint_pair
Definition: uint_types.h:99
#define STXXL_THROW_WIN_LASTERROR(exception_type, error_message)
Throws exception_type with &quot;Error in [function] : [error_message] : [formatted GetLastError()]&quot;.
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:244
#define STXXL_ERRMSG(x)
Definition: verbose.h:79
#define STXXL_END_NAMESPACE
Definition: namespace.h:17