Stxxl  1.3.2
file.h
1 /***************************************************************************
2  * include/stxxl/bits/io/file.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2010 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2008, 2009 Johannes Singler <[email protected]>
9  *
10  * Distributed under the Boost Software License, Version 1.0.
11  * (See accompanying file LICENSE_1_0.txt or copy at
12  * http://www.boost.org/LICENSE_1_0.txt)
13  **************************************************************************/
14 
15 #ifndef STXXL_IO_FILE_HEADER
16 #define STXXL_IO_FILE_HEADER
17 
18 #ifdef STXXL_BOOST_CONFIG
19  #include <boost/config.hpp>
20 #endif
21 
22 #if defined (__linux__)
23  #define STXXL_CHECK_BLOCK_ALIGNING
24 #endif
25 
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 
30 #ifdef BOOST_MSVC
31 // this is not stxxl/bits/io/io.h !
32  #include <io.h>
33 #else
34  #include <unistd.h>
35  #include <sys/resource.h>
36  #include <sys/wait.h>
37 #endif
38 
39 
40 //#ifdef __sun__
41 //#define O_DIRECT 0
42 //#endif
43 
44 #ifndef O_SYNC
45  #define O_SYNC 0
46 #endif
47 #ifndef O_RSYNC
48  #define O_RSYNC 0
49 #endif
50 #ifndef O_DSYNC
51  #define O_DSYNC 0
52 #endif
53 
54 #if defined (__linux__)
55  #if ! defined(O_DIRECT)
56  #error O_DIRECT is not defined while __linux__ is - PLEASE REPORT THIS BUG
57  #endif
58 //#include <asm/fcntl.h>
59 // FIXME: In which conditions is this not defined? Why only i386 and alpha? Why not amd64?
60  #if !defined (O_DIRECT) && (defined (__alpha__) || defined (__i386__))
61  #define O_DIRECT 040000 /* direct disk access */
62  #endif
63 #endif
64 
65 #ifndef O_DIRECT
66  #define O_DIRECT O_SYNC
67 #endif
68 
69 
70 #include <cassert>
71 
72 #include <stxxl/bits/libstxxl.h>
73 #include <stxxl/bits/namespace.h>
74 #include <stxxl/bits/noncopyable.h>
75 #include <stxxl/bits/common/exceptions.h>
76 #include <stxxl/bits/common/mutex.h>
77 #include <stxxl/bits/io/request.h>
78 #include <stxxl/bits/io/request_ptr.h>
79 
80 
81 __STXXL_BEGIN_NAMESPACE
82 
85 
87 
90 class file : private noncopyable
91 {
92  mutex request_ref_cnt_mutex;
93  int request_ref_cnt;
94 
95 protected:
99  file() : request_ref_cnt(0) { }
100 
101 public:
102  // the offset of a request, also the size of the file
103  typedef request::offset_type offset_type;
104  // the size of a request
105  typedef request::size_type size_type;
106 
108 
112  {
113  RDONLY = 1,
114  WRONLY = 2,
115  RDWR = 4,
116  CREAT = 8,
117  DIRECT = 16,
118  TRUNC = 32,
119  SYNC = 64,
120  NO_LOCK = 128,
121  };
122 
123  static const int DEFAULT_QUEUE = -1;
124  static const int NO_QUEUE = -2;
125  static const int NO_ALLOCATOR = -1;
126 
133  virtual request_ptr aread(void * buffer, offset_type pos, size_type bytes,
134  const completion_handler & on_cmpl) = 0;
135 
142  virtual request_ptr awrite(void * buffer, offset_type pos, size_type bytes,
143  const completion_handler & on_cmpl) = 0;
144 
145  virtual void serve(const request * req) throw (io_error) = 0;
146 
147  void add_request_ref()
148  {
149  scoped_mutex_lock Lock(request_ref_cnt_mutex);
150  ++request_ref_cnt;
151  }
152 
153  void delete_request_ref()
154  {
155  scoped_mutex_lock Lock(request_ref_cnt_mutex);
156  assert(request_ref_cnt > 0);
157  --request_ref_cnt;
158  }
159 
160  int get_request_nref()
161  {
162  scoped_mutex_lock Lock(request_ref_cnt_mutex);
163  return request_ref_cnt;
164  }
165 
168  virtual void set_size(offset_type newsize) = 0;
171  virtual offset_type size() = 0;
175  virtual int get_queue_id() const = 0;
178  virtual int get_allocator_id() const = 0;
179 
180  virtual int get_physical_device_id() const
181  {
182  return get_queue_id();
183  }
184 
186  virtual void lock() = 0;
187 
190  virtual void discard(offset_type offset, offset_type size)
191  {
192  STXXL_UNUSED(offset);
193  STXXL_UNUSED(size);
194  }
195 
196  virtual void export_files(offset_type offset, offset_type length, std::string prefix)
197  {
198  STXXL_UNUSED(offset);
199  STXXL_UNUSED(length);
200  STXXL_UNUSED(prefix);
201  }
202 
203  virtual void remove() { }
204 
205  virtual ~file()
206  {
207  int nr = get_request_nref();
208  if (nr != 0)
209  STXXL_ERRMSG("stxxl::file is being deleted while there are still " << nr << " (unfinished) requests referencing it");
210  }
211 
214  virtual const char * io_type() const
215  {
216  return "none";
217  }
218 };
219 
221 
222 __STXXL_END_NAMESPACE
223 
224 #endif // !STXXL_IO_FILE_HEADER
225 // vim: et:ts=4:sw=4
read and write of the file are allowed
Definition: file.h:115
virtual void discard(offset_type offset, offset_type size)
Discard a region of the file (mark it unused) some specialized file types may need to know freed regi...
Definition: file.h:190
virtual void set_size(offset_type newsize)=0
Changes the size of the file.
Defines interface of file.
Definition: file.h:90
virtual const char * io_type() const
Identifies the type of I/O implementation.
Definition: file.h:214
virtual request_ptr awrite(void *buffer, offset_type pos, size_type bytes, const completion_handler &on_cmpl)=0
Schedules an asynchronous write request to the file.
Request with basic properties like file and offset.
Definition: request.h:39
Completion handler class (Loki-style)
Definition: completion_handler.h:63
only writing of the file is allowed
Definition: file.h:114
open_mode
Definition of acceptable file open modes.
Definition: file.h:111
virtual int get_allocator_id() const =0
Returns the file&#39;s allocator.
I/Os proceed bypassing file system buffers, i.e. unbuffered I/O.
Definition: file.h:117
only reading of the file is allowed
Definition: file.h:113
open the file with O_SYNC | O_DSYNC | O_RSYNC flags set
Definition: file.h:119
virtual int get_queue_id() const =0
Returns the identifier of the file&#39;s queue.
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:82
virtual offset_type size()=0
Returns size of the file.
Implemented as reference counting smart pointer.
Definition: request_ptr.h:34
virtual request_ptr aread(void *buffer, offset_type pos, size_type bytes, const completion_handler &on_cmpl)=0
Schedules an asynchronous read request to the file.
once file is opened its length becomes zero
Definition: file.h:118
in case file does not exist no error occurs and file is newly created
Definition: file.h:116
do not aquire an exclusive lock by default
Definition: file.h:120
virtual void lock()=0
Locks file for reading and writing (acquires a lock in the file system)
file()
Initializes file object.
Definition: file.h:99