Stxxl  1.4.0
include/stxxl/bits/io/file.h
Go to the documentation of this file.
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 
00083 //! \addtogroup iolayer
00084 //! \{
00085 
00086 //! \brief Defines interface of file
00087 
00088 //! It is a base class for different implementations that might
00089 //! base on various file systems or even remote storage interfaces
00090 class file : private noncopyable
00091 {
00092     mutex request_ref_cnt_mutex;
00093     int request_ref_cnt;
00094 
00095 protected:
00096     //! \brief Initializes file object
00097     //! \param _id file identifier
00098     //! \remark Called in implementations of file
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 
00107     //! \brief Definition of acceptable file open modes
00108 
00109     //! Various open modes in a file system must be
00110     //! converted to this set of acceptable modes
00111     enum open_mode
00112     {
00113         RDONLY = 1,                         //!< only reading of the file is allowed
00114         WRONLY = 2,                         //!< only writing of the file is allowed
00115         RDWR = 4,                           //!< read and write of the file are allowed
00116         CREAT = 8,                          //!< in case file does not exist no error occurs and file is newly created
00117         DIRECT = 16,                        //!< I/Os proceed bypassing file system buffers, i.e. unbuffered I/O
00118         TRUNC = 32,                         //!< once file is opened its length becomes zero
00119         SYNC = 64,                          //!< open the file with O_SYNC | O_DSYNC | O_RSYNC flags set
00120         NO_LOCK = 128,                      //!< do not aquire an exclusive lock by default
00121     };
00122 
00123     static const int DEFAULT_QUEUE = -1;
00124     static const int NO_QUEUE = -2;
00125     static const int NO_ALLOCATOR = -1;
00126 
00127     //! \brief Schedules an asynchronous read request to the file
00128     //! \param buffer pointer to memory buffer to read into
00129     //! \param pos file position to start read from
00130     //! \param bytes number of bytes to transfer
00131     //! \param on_cmpl I/O completion handler
00132     //! \return \c request_ptr request object, which can be used to track the status of the operation
00133     virtual request_ptr aread(void * buffer, offset_type pos, size_type bytes,
00134                               const completion_handler & on_cmpl) = 0;
00135 
00136     //! \brief Schedules an asynchronous write request to the file
00137     //! \param buffer pointer to memory buffer to write from
00138     //! \param pos starting file position to write
00139     //! \param bytes number of bytes to transfer
00140     //! \param on_cmpl I/O completion handler
00141     //! \return \c request_ptr request object, which can be used to track the status of the operation
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 
00166     //! \brief Changes the size of the file
00167     //! \param newsize new file size
00168     virtual void set_size(offset_type newsize) = 0;
00169     //! \brief Returns size of the file
00170     //! \return file size in bytes
00171     virtual offset_type size() = 0;
00172     //! \brief Returns the identifier of the file's queue
00173     //! \remark Files allocated on the same physical device usually share the same queue
00174     //! \return queue number
00175     virtual int get_queue_id() const = 0;
00176     //! \brief Returns the file's allocator
00177     //! \return allocator number
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 
00185     //! \brief Locks file for reading and writing (acquires a lock in the file system)
00186     virtual void lock() = 0;
00187 
00188     //! \brief Discard a region of the file (mark it unused)
00189     //! some specialized file types may need to know freed regions
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 
00212     //! \brief Identifies the type of I/O implementation
00213     //! \return pointer to null terminated string of characters, containing the name of I/O implementation
00214     virtual const char * io_type() const
00215     {
00216         return "none";
00217     }
00218 };
00219 
00220 //! \}
00221 
00222 __STXXL_END_NAMESPACE
00223 
00224 #endif // !STXXL_IO_FILE_HEADER
00225 // vim: et:ts=4:sw=4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines