STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
file.h
Go to the documentation of this file.
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  * Copyright (C) 2013 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_IO_FILE_HEADER
17 #define STXXL_IO_FILE_HEADER
18 
19 #include <stxxl/bits/config.h>
20 
21 #if defined (__linux__)
22  #define STXXL_CHECK_BLOCK_ALIGNING
23 #endif
24 
25 #include <cassert>
26 #include <ostream>
27 #include <string>
28 
32 #include <stxxl/bits/io/request.h>
34 #include <stxxl/bits/libstxxl.h>
35 #include <stxxl/bits/namespace.h>
36 #include <stxxl/bits/noncopyable.h>
37 #include <stxxl/bits/unused.h>
38 #include <stxxl/bits/verbose.h>
39 
40 
42 
43 //! \addtogroup iolayer
44 //! \{
45 
46 class completion_handler;
47 
48 //! Defines interface of file.
49 //!
50 //! It is a base class for different implementations that might
51 //! base on various file systems or even remote storage interfaces
52 class file : private noncopyable
53 {
56 
57 protected:
58  //! Initializes file object.
59  //! \remark Called in implementations of file
60  file() : request_ref_cnt(0) { }
61 
62 public:
63  //! the offset of a request, also the size of the file
64  typedef request::offset_type offset_type;
65  //! the size of a request
67 
68  //! Definition of acceptable file open modes.
69  //!
70  //! Various open modes in a file system must be
71  //! converted to this set of acceptable modes
72  enum open_mode
73  {
74  RDONLY = 1, //!< only reading of the file is allowed
75  WRONLY = 2, //!< only writing of the file is allowed
76  RDWR = 4, //!< read and write of the file are allowed
77  CREAT = 8, //!< in case file does not exist no error occurs and file is newly created
78  DIRECT = 16, //!< I/Os proceed bypassing file system buffers, i.e. unbuffered I/O.
79  //!< Tries to open with appropriate flags, if fails print warning and open normally.
80  TRUNC = 32, //!< once file is opened its length becomes zero
81  SYNC = 64, //!< open the file with O_SYNC | O_DSYNC | O_RSYNC flags set
82  NO_LOCK = 128, //!< do not aquire an exclusive lock by default
83  REQUIRE_DIRECT = 256 //!< implies DIRECT, fail if opening with DIRECT flag does not work.
84  };
85 
86  static const int DEFAULT_QUEUE = -1;
87  static const int NO_QUEUE = -2;
88  static const int NO_ALLOCATOR = -1;
89 
90  //! Schedules an asynchronous read request to the file.
91  //! \param buffer pointer to memory buffer to read into
92  //! \param pos file position to start read from
93  //! \param bytes number of bytes to transfer
94  //! \param on_cmpl I/O completion handler
95  //! \return \c request_ptr request object, which can be used to track the status of the operation
96  virtual request_ptr aread(void* buffer, offset_type pos, size_type bytes,
97  const completion_handler& on_cmpl) = 0;
98 
99  //! Schedules an asynchronous write request to the file.
100  //! \param buffer pointer to memory buffer to write from
101  //! \param pos starting file position to write
102  //! \param bytes number of bytes to transfer
103  //! \param on_cmpl I/O completion handler
104  //! \return \c request_ptr request object, which can be used to track the status of the operation
105  virtual request_ptr awrite(void* buffer, offset_type pos, size_type bytes,
106  const completion_handler& on_cmpl) = 0;
107 
108  virtual void serve(const request* req) throw (io_error) = 0;
109 
111  {
112  scoped_mutex_lock Lock(request_ref_cnt_mutex);
113  ++request_ref_cnt;
114  }
115 
117  {
118  scoped_mutex_lock Lock(request_ref_cnt_mutex);
119  assert(request_ref_cnt > 0);
120  --request_ref_cnt;
121  }
122 
124  {
125  scoped_mutex_lock Lock(request_ref_cnt_mutex);
126  return request_ref_cnt;
127  }
128 
129  //! Changes the size of the file.
130  //! \param newsize new file size
131  virtual void set_size(offset_type newsize) = 0;
132 
133  //! Returns size of the file.
134  //! \return file size in bytes
135  virtual offset_type size() = 0;
136 
137  //! Returns the identifier of the file's queue.
138  //! \remark Files allocated on the same physical device usually share the same queue
139  //! \return queue number
140  virtual int get_queue_id() const = 0;
141 
142  //! Returns the file's allocator.
143  //! \return allocator number
144  virtual int get_allocator_id() const = 0;
145 
146  virtual int get_physical_device_id() const
147  {
148  return get_queue_id();
149  }
150 
151  //! Locks file for reading and writing (acquires a lock in the file system).
152  virtual void lock() = 0;
153 
154  //! Discard a region of the file (mark it unused).
155  //! Some specialized file types may need to know freed regions
156  virtual void discard(offset_type offset, offset_type size)
157  {
158  STXXL_UNUSED(offset);
159  STXXL_UNUSED(size);
160  }
161 
162  virtual void export_files(offset_type offset, offset_type length, std::string prefix)
163  {
164  STXXL_UNUSED(offset);
165  STXXL_UNUSED(length);
166  STXXL_UNUSED(prefix);
167  }
168 
169  //! close and remove file
170  virtual void close_remove() { }
171 
172  virtual ~file()
173  {
174  int nr = get_request_nref();
175  if (nr != 0)
176  STXXL_ERRMSG("stxxl::file is being deleted while there are still " << nr << " (unfinished) requests referencing it");
177  }
178 
179  //! Identifies the type of I/O implementation.
180  //! \return pointer to null terminated string of characters, containing the name of I/O implementation
181  virtual const char * io_type() const
182  {
183  return "none";
184  }
185 
186 public:
187  //! \name Static Functions for Platform Abstraction
188  //! \{
189 
190  //! unlink path from filesystem
191  static int unlink(const char* path);
192 
193  //! truncate a path to given length. Use this only if you dont have a
194  //! fileio-specific object, which provides truncate().
195  static int truncate(const char* path, external_size_type length);
196 
197  //! \}
198 };
199 
200 //! \defgroup fileimpl File I/O Implementations
201 //! Implementations of \c stxxl::file and \c stxxl::request
202 //! for various file access methods
203 //! \{
204 //! \}
205 
206 //! \}
207 
209 
210 #endif // !STXXL_IO_FILE_HEADER
211 // vim: et:ts=4:sw=4
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 reg...
Definition: file.h:156
static const int bytes
number of bytes in uint_pair
Definition: uint_types.h:99
int get_request_nref()
Definition: file.h:123
Completion handler class (Loki-style).
request::size_type size_type
the size of a request
Definition: file.h:66
virtual ~file()
Definition: file.h:172
Defines interface of file.
Definition: file.h:52
virtual const char * io_type() const
Identifies the type of I/O implementation.
Definition: file.h:181
Aquire a lock that&#39;s valid until the end of scope.
Definition: mutex.h:106
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void STXXL_UNUSED(const U &)
Definition: unused.h:23
virtual void export_files(offset_type offset, offset_type length, std::string prefix)
Definition: file.h:162
open_mode
Definition of acceptable file open modes.
Definition: file.h:72
void add_request_ref()
Definition: file.h:110
stxxl::internal_size_type size_type
file()
Initializes file object.
Definition: file.h:60
#define STXXL_ERRMSG(x)
Definition: verbose.h:79
uint64 external_size_type
Definition: types.h:70
mutex request_ref_cnt_mutex
Definition: file.h:54
virtual int get_physical_device_id() const
Definition: file.h:146
request::offset_type offset_type
the offset of a request, also the size of the file
Definition: file.h:64
int request_ref_cnt
Definition: file.h:55
Request with basic properties like file and offset.
Definition: request.h:39
virtual void close_remove()
close and remove file
Definition: file.h:170
void delete_request_ref()
Definition: file.h:116
#define STXXL_END_NAMESPACE
Definition: namespace.h:17