Disk ARchive  2.5.0
Full featured and portable backup and archiving tool
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
cache.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
25 
26 #ifndef CACHE_HPP
27 #define CACHE_HPP
28 
29 #include "../my_config.h"
30 #include "infinint.hpp"
31 #include "generic_file.hpp"
32 
33 namespace libdar
34 {
35 
38 
52  class cache : public generic_file
53  {
54  public:
55  cache(generic_file & hidden, //< is the file to cache, it is never deleted by the cache object,
56  bool shift_mode, //< if true, when all cached data has been read, half of the data is flushed from the cache, the other half is shifted and new data take place to fill the cache. This is necessary for sequential reading, but has some CPU overhead.
57  U_I size = 102400); //< is the (fixed) size of the cache
58  cache(const cache & ref) : generic_file(ref.get_mode()) { throw SRC_BUG; };
59  const cache & operator = (const cache & ref) { throw SRC_BUG; };
60  ~cache();
61  void change_to_read_write() { if(get_mode() == gf_read_only) throw SRC_BUG; set_mode(gf_read_write); };
62 
63  // inherited from generic_file
64 
65  bool skippable(skippability direction, const infinint & amount);
66  bool skip(const infinint & pos);
67  bool skip_to_eof();
68  bool skip_relative(S_I x);
69  infinint get_position() const { return buffer_offset + next; };
70 
71  protected:
72  // inherited from generic_file
73  void inherited_read_ahead(const infinint & amount) { ref->read_ahead(amount - available_in_cache(generic_file::skip_forward)); };
74  U_I inherited_read(char *a, U_I size);
75  void inherited_write(const char *a, U_I size);
76  void inherited_sync_write() { flush_write(); };
77  void inherited_flush_read() { flush_write(); clear_buffer(); };
78  void inherited_terminate() { flush_write(); };
79 
80  private:
81  generic_file *ref; //< underlying file, (not owned by "this', not to be delete by "this")
82  char *buffer; //< data in transit
83  U_I size; //< allocated size
84  U_I next; //< next to read or next place to write to
85  U_I last; //< last valid data in the cache. we have: next <= last < size
86  U_I first_to_write; //< position of the first byte that need to be written. if greater than last, no byte need writing
87  infinint buffer_offset; //< position of the first byte in buffer
88  bool shifted_mode; //< whether to half flush and shift or totally flush data
89 
90  bool need_flush_write() const { return first_to_write < last; };
91  void alloc_buffer(size_t x_size); //< allocate x_size byte in buffer field and set size accordingly
92  void release_buffer(); //< release memory set buffer to nullptr and size to zero
93  void shift_by_half();
94  void clear_buffer();
95  void flush_write();
96  void fulfill_read();
97  U_I available_in_cache(skippability direction) const;
98  };
99 
101 
102 } // end of namespace
103 
104 #endif
105 
bool skip(const infinint &pos)
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
virtual void read_ahead(const infinint &amount)
U_I inherited_read(char *a, U_I size)
implementation of read() operation
void inherited_write(const char *a, U_I size)
implementation of the write() operation
bool skip_relative(S_I x)
skip relatively to the current position
void inherited_sync_write()
write down any pending data
Definition: cache.hpp:76
infinint get_position() const
get the current read/write position
Definition: cache.hpp:69
void inherited_terminate()
destructor-like call, except that it is allowed to throw exceptions
Definition: cache.hpp:78
void inherited_flush_read()
Definition: cache.hpp:77
generic_file(gf_mode m)
main constructor
gf_mode get_mode() const
retreive the openning mode for this object
switch module to limitint (32 ou 64 bits integers) or infinint
bool skippable(skippability direction, const infinint &amount)
this is the interface class from which all other data transfer classes inherit
read and write access
the arbitrary large positive integer class
bool skip_to_eof()
skip to the end of file
read only access
void inherited_read_ahead(const infinint &amount)
Definition: cache.hpp:73