| Disk ARchive 2.5.10 Full featured and portable backup and archiving tool

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
libdar::cache::skip
bool skip(const infinint &pos)
generic_file.hpp
class generic_file is defined here as well as class fichierthe generic_file interface is widely used ...
libdar::generic_file::read_ahead
virtual void read_ahead(const infinint &amount)
libdar::cache::inherited_read
U_I inherited_read(char *a, U_I size)
implementation of read() operation
libdar::cache::inherited_write
void inherited_write(const char *a, U_I size)
implementation of the write() operation
libdar::cache::skip_relative
bool skip_relative(S_I x)
skip relatively to the current position
libdar::cache::inherited_sync_write
void inherited_sync_write()
write down any pending data
Definition: cache.hpp:76
libdar::cache::get_position
infinint get_position() const
get the current read/write position
Definition: cache.hpp:69
libdar::cache::inherited_terminate
void inherited_terminate()
destructor-like call, except that it is allowed to throw exceptions
Definition: cache.hpp:78
libdar::cache::inherited_flush_read
void inherited_flush_read()
Definition: cache.hpp:77
libdar::generic_file::generic_file
generic_file(gf_mode m)
main constructor
Definition: generic_file.hpp:104
libdar::cache
Definition: cache.hpp:52
libdar::generic_file::get_mode
gf_mode get_mode() const
retreive the openning mode for this object
Definition: generic_file.hpp:120
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
libdar::cache::skippable
bool skippable(skippability direction, const infinint &amount)
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition: generic_file.hpp:100
libdar::gf_read_write
read and write access
Definition: generic_file.hpp:81
libdar::infinint
the arbitrary large positive integer class
Definition: real_infinint.hpp:59
libdar::cache::skip_to_eof
bool skip_to_eof()
skip to the end of file
libdar::gf_read_only
read only access
Definition: generic_file.hpp:79
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47
libdar::cache::inherited_read_ahead
void inherited_read_ahead(const infinint &amount)
Definition: cache.hpp:73