STXXL  1.4-dev
 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 void wincall_file::serve(void* buffer, offset_type offset, size_type bytes,
29  request::request_type type)
30 {
31  scoped_mutex_lock fd_lock(fd_mutex);
32 
33  if (bytes > 32 * 1024 * 1024) {
34  STXXL_ERRMSG("Using a block size larger than 32 MiB may not work with the " << io_type() << " filetype");
35  }
36 
37  HANDLE handle = file_des;
38  LARGE_INTEGER desired_pos;
39  desired_pos.QuadPart = offset;
40  if (!SetFilePointerEx(handle, desired_pos, NULL, FILE_BEGIN))
41  {
43  "SetFilePointerEx in wincall_request::serve()" <<
44  " offset=" << offset <<
45  " this=" << this <<
46  " buffer=" << buffer <<
47  " bytes=" << bytes <<
48  " type=" << ((type == request::READ) ? "READ" : "WRITE"));
49  }
50  else
51  {
52  stats::scoped_read_write_timer read_write_timer(bytes, type == request::WRITE);
53 
54  if (type == request::READ)
55  {
56  DWORD NumberOfBytesRead = 0;
57  assert(bytes <= std::numeric_limits<DWORD>::max());
58  if (!ReadFile(handle, buffer, (DWORD)bytes, &NumberOfBytesRead, NULL))
59  {
61  "ReadFile" <<
62  " this=" << this <<
63  " offset=" << offset <<
64  " buffer=" << buffer <<
65  " bytes=" << bytes <<
66  " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
67  " NumberOfBytesRead= " << NumberOfBytesRead);
68  }
69  else if (NumberOfBytesRead != bytes) {
70  STXXL_THROW_WIN_LASTERROR(io_error, " partial read: missing " << (bytes - NumberOfBytesRead) << " out of " << bytes << " bytes");
71  }
72  }
73  else
74  {
75  DWORD NumberOfBytesWritten = 0;
76  assert(bytes <= std::numeric_limits<DWORD>::max());
77  if (!WriteFile(handle, buffer, (DWORD)bytes, &NumberOfBytesWritten, NULL))
78  {
80  "WriteFile" <<
81  " this=" << this <<
82  " offset=" << offset <<
83  " buffer=" << buffer <<
84  " bytes=" << bytes <<
85  " type=" << ((type == request::READ) ? "READ" : "WRITE") <<
86  " NumberOfBytesWritten= " << NumberOfBytesWritten);
87  }
88  else if (NumberOfBytesWritten != bytes) {
89  STXXL_THROW_WIN_LASTERROR(io_error, " partial write: missing " << (bytes - NumberOfBytesWritten) << " out of " << bytes << " bytes");
90  }
91  }
92  }
93 }
94 
95 const char* wincall_file::io_type() const
96 {
97  return "wincall";
98 }
99 
101 
102 #endif // #if STXXL_HAVE_WINCALL_FILE
103 // vim: et:ts=4:sw=4
#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:241
#define STXXL_ERRMSG(x)
Definition: verbose.h:94
static const size_t bytes
number of bytes in uint_pair
Definition: uint_types.h:96
#define STXXL_END_NAMESPACE
Definition: namespace.h:17