00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef STXXL_IO_FILE_HEADER
00015 #define STXXL_IO_FILE_HEADER
00016
00017 #ifdef STXXL_BOOST_CONFIG
00018 #include <boost/config.hpp>
00019 #endif
00020
00021 #if defined (__linux__)
00022 #define STXXL_CHECK_BLOCK_ALIGNING
00023 #endif
00024
00025 #include <fcntl.h>
00026 #include <sys/types.h>
00027 #include <sys/stat.h>
00028
00029 #ifdef BOOST_MSVC
00030
00031 #include <io.h>
00032 #else
00033 #include <unistd.h>
00034 #include <sys/resource.h>
00035 #include <sys/wait.h>
00036 #endif
00037
00038
00039
00040
00041
00042
00043 #ifndef O_SYNC
00044 #define O_SYNC 0
00045 #endif
00046 #ifndef O_RSYNC
00047 #define O_RSYNC 0
00048 #endif
00049 #ifndef O_DSYNC
00050 #define O_DSYNC 0
00051 #endif
00052
00053 #if defined (__linux__)
00054 #if ! defined(O_DIRECT)
00055 #error O_DIRECT is not defined while __linux__ is - PLEASE REPORT THIS BUG
00056 #endif
00057
00058
00059 #if !defined (O_DIRECT) && (defined (__alpha__) || defined (__i386__))
00060 #define O_DIRECT 040000
00061 #endif
00062 #endif
00063
00064 #ifndef O_DIRECT
00065 #define O_DIRECT O_SYNC
00066 #endif
00067
00068
00069 #include <cassert>
00070
00071 #include <stxxl/bits/namespace.h>
00072 #include <stxxl/bits/noncopyable.h>
00073 #include <stxxl/bits/common/exceptions.h>
00074 #include <stxxl/bits/common/mutex.h>
00075 #include <stxxl/bits/io/request.h>
00076
00077
00078 __STXXL_BEGIN_NAMESPACE
00079
00082
00084
00087 class file : private noncopyable
00088 {
00089 mutex request_ref_cnt_mutex;
00090 int request_ref_cnt;
00091
00092 protected:
00096 file() : request_ref_cnt(0) { }
00097
00098 public:
00099
00100 typedef request::offset_type offset_type;
00101
00102 typedef request::size_type size_type;
00103
00105
00108 enum open_mode
00109 {
00110 RDONLY = 1,
00111 WRONLY = 2,
00112 RDWR = 4,
00113 CREAT = 8,
00114 DIRECT = 16,
00115 TRUNC = 32
00116 };
00117
00118 static const int DEFAULT_QUEUE = -1;
00119 static const int NO_QUEUE = -2;
00120 static const int NO_ALLOCATOR = -1;
00121
00128 virtual request_ptr aread(void * buffer, offset_type pos, size_type bytes,
00129 const completion_handler & on_cmpl) = 0;
00130
00137 virtual request_ptr awrite(void * buffer, offset_type pos, size_type bytes,
00138 const completion_handler & on_cmpl) = 0;
00139
00140 virtual void serve(const request * req) throw (io_error) = 0;
00141
00142 void add_request_ref()
00143 {
00144 scoped_mutex_lock Lock(request_ref_cnt_mutex);
00145 ++request_ref_cnt;
00146 }
00147
00148 void delete_request_ref()
00149 {
00150 scoped_mutex_lock Lock(request_ref_cnt_mutex);
00151 assert(request_ref_cnt > 0);
00152 --request_ref_cnt;
00153 }
00154
00155 int get_request_nref()
00156 {
00157 scoped_mutex_lock Lock(request_ref_cnt_mutex);
00158 return request_ref_cnt;
00159 }
00160
00163 virtual void set_size(offset_type newsize) = 0;
00166 virtual offset_type size() = 0;
00170 virtual int get_queue_id() const = 0;
00174 virtual int get_allocator_id() const = 0;
00175
00176 virtual int get_physical_device_id() const
00177 {
00178 return get_queue_id();
00179 }
00180
00182 virtual void lock() = 0;
00183
00185 virtual void discard(offset_type offset, offset_type size)
00186 {
00187 STXXL_UNUSED(offset);
00188 STXXL_UNUSED(size);
00189 }
00190
00191 virtual void export_files(offset_type offset, offset_type length, std::string prefix)
00192 {
00193 STXXL_UNUSED(offset);
00194 STXXL_UNUSED(length);
00195 STXXL_UNUSED(prefix);
00196 }
00197
00198 virtual void remove() { }
00199
00200 virtual ~file()
00201 {
00202 int nr = get_request_nref();
00203 if (nr != 0)
00204 STXXL_ERRMSG("stxxl::file is being deleted while there are still " << nr << " (unfinished) requests referencing it");
00205 }
00206
00209 virtual const char * io_type() const
00210 {
00211 return "none";
00212 }
00213 };
00214
00216
00217 __STXXL_END_NAMESPACE
00218
00219 #endif // !STXXL_IO_FILE_HEADER
00220