Stxxl
1.3.1
|
00001 /*************************************************************************** 00002 * include/stxxl/bits/io/file.h 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00007 * Copyright (C) 2008, 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00008 * Copyright (C) 2008, 2009 Johannes Singler <singler@ira.uka.de> 00009 * 00010 * Distributed under the Boost Software License, Version 1.0. 00011 * (See accompanying file LICENSE_1_0.txt or copy at 00012 * http://www.boost.org/LICENSE_1_0.txt) 00013 **************************************************************************/ 00014 00015 #ifndef STXXL_IO_FILE_HEADER 00016 #define STXXL_IO_FILE_HEADER 00017 00018 #ifdef STXXL_BOOST_CONFIG 00019 #include <boost/config.hpp> 00020 #endif 00021 00022 #if defined (__linux__) 00023 #define STXXL_CHECK_BLOCK_ALIGNING 00024 #endif 00025 00026 #include <fcntl.h> 00027 #include <sys/types.h> 00028 #include <sys/stat.h> 00029 00030 #ifdef BOOST_MSVC 00031 // this is not stxxl/bits/io/io.h ! 00032 #include <io.h> 00033 #else 00034 #include <unistd.h> 00035 #include <sys/resource.h> 00036 #include <sys/wait.h> 00037 #endif 00038 00039 00040 //#ifdef __sun__ 00041 //#define O_DIRECT 0 00042 //#endif 00043 00044 #ifndef O_SYNC 00045 #define O_SYNC 0 00046 #endif 00047 #ifndef O_RSYNC 00048 #define O_RSYNC 0 00049 #endif 00050 #ifndef O_DSYNC 00051 #define O_DSYNC 0 00052 #endif 00053 00054 #if defined (__linux__) 00055 #if ! defined(O_DIRECT) 00056 #error O_DIRECT is not defined while __linux__ is - PLEASE REPORT THIS BUG 00057 #endif 00058 //#include <asm/fcntl.h> 00059 // FIXME: In which conditions is this not defined? Why only i386 and alpha? Why not amd64? 00060 #if !defined (O_DIRECT) && (defined (__alpha__) || defined (__i386__)) 00061 #define O_DIRECT 040000 /* direct disk access */ 00062 #endif 00063 #endif 00064 00065 #ifndef O_DIRECT 00066 #define O_DIRECT O_SYNC 00067 #endif 00068 00069 00070 #include <cassert> 00071 00072 #include <stxxl/bits/libstxxl.h> 00073 #include <stxxl/bits/namespace.h> 00074 #include <stxxl/bits/noncopyable.h> 00075 #include <stxxl/bits/common/exceptions.h> 00076 #include <stxxl/bits/common/mutex.h> 00077 #include <stxxl/bits/io/request.h> 00078 #include <stxxl/bits/io/request_ptr.h> 00079 00080 00081 __STXXL_BEGIN_NAMESPACE 00082 00085 00087 00090 class file : private noncopyable 00091 { 00092 mutex request_ref_cnt_mutex; 00093 int request_ref_cnt; 00094 00095 protected: 00099 file() : request_ref_cnt(0) { } 00100 00101 public: 00102 // the offset of a request, also the size of the file 00103 typedef request::offset_type offset_type; 00104 // the size of a request 00105 typedef request::size_type size_type; 00106 00108 00111 enum open_mode 00112 { 00113 RDONLY = 1, 00114 WRONLY = 2, 00115 RDWR = 4, 00116 CREAT = 8, 00117 DIRECT = 16, 00118 TRUNC = 32, 00119 SYNC = 64, 00120 NO_LOCK = 128, 00121 }; 00122 00123 static const int DEFAULT_QUEUE = -1; 00124 static const int NO_QUEUE = -2; 00125 static const int NO_ALLOCATOR = -1; 00126 00133 virtual request_ptr aread(void * buffer, offset_type pos, size_type bytes, 00134 const completion_handler & on_cmpl) = 0; 00135 00142 virtual request_ptr awrite(void * buffer, offset_type pos, size_type bytes, 00143 const completion_handler & on_cmpl) = 0; 00144 00145 virtual void serve(const request * req) throw (io_error) = 0; 00146 00147 void add_request_ref() 00148 { 00149 scoped_mutex_lock Lock(request_ref_cnt_mutex); 00150 ++request_ref_cnt; 00151 } 00152 00153 void delete_request_ref() 00154 { 00155 scoped_mutex_lock Lock(request_ref_cnt_mutex); 00156 assert(request_ref_cnt > 0); 00157 --request_ref_cnt; 00158 } 00159 00160 int get_request_nref() 00161 { 00162 scoped_mutex_lock Lock(request_ref_cnt_mutex); 00163 return request_ref_cnt; 00164 } 00165 00168 virtual void set_size(offset_type newsize) = 0; 00171 virtual offset_type size() = 0; 00175 virtual int get_queue_id() const = 0; 00178 virtual int get_allocator_id() const = 0; 00179 00180 virtual int get_physical_device_id() const 00181 { 00182 return get_queue_id(); 00183 } 00184 00186 virtual void lock() = 0; 00187 00190 virtual void discard(offset_type offset, offset_type size) 00191 { 00192 STXXL_UNUSED(offset); 00193 STXXL_UNUSED(size); 00194 } 00195 00196 virtual void export_files(offset_type offset, offset_type length, std::string prefix) 00197 { 00198 STXXL_UNUSED(offset); 00199 STXXL_UNUSED(length); 00200 STXXL_UNUSED(prefix); 00201 } 00202 00203 virtual void remove() { } 00204 00205 virtual ~file() 00206 { 00207 int nr = get_request_nref(); 00208 if (nr != 0) 00209 STXXL_ERRMSG("stxxl::file is being deleted while there are still " << nr << " (unfinished) requests referencing it"); 00210 } 00211 00214 virtual const char * io_type() const 00215 { 00216 return "none"; 00217 } 00218 }; 00219 00221 00222 __STXXL_END_NAMESPACE 00223 00224 #endif // !STXXL_IO_FILE_HEADER 00225 // vim: et:ts=4:sw=4