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

cache.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2020 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
40
52 class cache : public generic_file
53 {
54 public:
55 cache(generic_file & hidden,
56 bool shift_mode,
57 U_I size = 102400
58 );
59 cache(const cache & ref) = delete;
60 cache(cache && ref) = delete;
61 cache & operator = (const cache & ref) = delete;
62 cache & operator = (cache && ref) = delete;
63 ~cache();
64 void change_to_read_write() { if(get_mode() == gf_read_only) throw SRC_BUG; set_mode(gf_read_write); };
65
66 // inherited from generic_file
67
68 virtual bool skippable(skippability direction, const infinint & amount) override;
69 virtual bool skip(const infinint & pos) override;
70 virtual bool skip_to_eof() override;
71 virtual bool skip_relative(S_I x) override;
72 virtual infinint get_position() const override { return buffer_offset + next; };
73
74 protected:
75 // inherited from generic_file
76 virtual void inherited_read_ahead(const infinint & amount) override;
77 virtual U_I inherited_read(char *a, U_I size) override;
78 virtual void inherited_write(const char *a, U_I size) override;
79 virtual void inherited_sync_write() override { flush_write(); };
80 virtual void inherited_flush_read() override { flush_write(); clear_buffer(); };
81 virtual void inherited_terminate() override { flush_write(); };
82
83 private:
84 generic_file *ref;
85 char *buffer;
86 U_I size;
87 U_I half;
88 U_I next;
89 U_I last;
90 U_I first_to_write;
91 infinint buffer_offset;
92 bool shifted_mode;
93 infinint eof_offset;
94
95 bool need_flush_write() const { return first_to_write < last; };
96 void alloc_buffer(size_t x_size);
97 void release_buffer();
98 void shift_by_half();
99 void clear_buffer();
100 void flush_write();
101 void fulfill_read();
102 U_I available_in_cache(skippability direction) const;
103 };
104
106
107 } // end of namespace
108
109 #endif
110
libdar::cache::cache
cache(generic_file &hidden, bool shift_mode, U_I size=102400)
libdar::cache::skip_to_eof
virtual bool skip_to_eof() override
skip to the end of file
libdar::cache::half
U_I half
precalculated half = size / 2
Definition: cache.hpp:87
libdar::cache
the cache class implements a fixed length read/write caching mechanism
Definition: cache.hpp:53
libdar::cache::skip
virtual bool skip(const infinint &pos) override
skip at the absolute position
libdar::cache::inherited_terminate
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
Definition: cache.hpp:81
libdar::cache::inherited_write
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
libdar::cache::inherited_read
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
libdar::infinint
the arbitrary large positive integer class
Definition: real_infinint.hpp:62
libdar::cache::next
U_I next
next to read or next place to write to
Definition: cache.hpp:88
libdar::cache::last
U_I last
first byte of invalid data in the cache. we have: next <= last < size
Definition: cache.hpp:89
libdar::generic_file::get_mode
gf_mode get_mode() const
retreive the openning mode for this object
Definition: generic_file.hpp:109
libdar::cache::size
U_I size
allocated size
Definition: cache.hpp:86
libdar::cache::inherited_read_ahead
virtual void inherited_read_ahead(const infinint &amount) override
tells the object that several calls to read() will follow to probably obtain at least the given amoun...
libdar::cache::alloc_buffer
void alloc_buffer(size_t x_size)
allocate x_size byte in buffer field and set size accordingly
generic_file.hpp
class generic_file is defined here as well as class fichier
libdar::cache::buffer
char * buffer
data in transit
Definition: cache.hpp:85
libdar::cache::skippable
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
libdar::cache::inherited_sync_write
virtual void inherited_sync_write() override
write down any pending data
Definition: cache.hpp:79
libdar::cache::shifted_mode
bool shifted_mode
whether to half flush and shift or totally flush data
Definition: cache.hpp:92
libdar::cache::first_to_write
U_I first_to_write
position of the first byte that need to be written. if greater than last, no byte need writing
Definition: cache.hpp:90
libdar::cache::ref
generic_file * ref
underlying file, (not owned by "this', not to be delete by "this")
Definition: cache.hpp:81
libdar::generic_file
this is the interface class from which all other data transfer classes inherit
Definition: generic_file.hpp:77
libdar::cache::eof_offset
infinint eof_offset
size of the underlying file (read-only mode), set to zero if unknown
Definition: cache.hpp:93
libdar::cache::release_buffer
void release_buffer()
release memory set buffer to nullptr and size to zero
libdar::cache::buffer_offset
infinint buffer_offset
position of the first byte in buffer
Definition: cache.hpp:91
libdar::gf_read_write
@ gf_read_write
read and write access
Definition: gf_mode.hpp:47
libdar::cache::inherited_flush_read
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position
Definition: cache.hpp:80
libdar::gf_read_only
@ gf_read_only
read only access
Definition: gf_mode.hpp:45
libdar::cache::get_position
virtual infinint get_position() const override
get the current read/write position
Definition: cache.hpp:72
libdar::cache::skip_relative
virtual bool skip_relative(S_I x) override
skip relatively to the current position
libdar
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47