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

Loading...
Searching...
No Matches
cache.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2026 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, see the AUTHOR file
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 bool truncatable(const infinint & pos) const override;
73 virtual infinint get_position() const override { return buffer_offset + next; };
74
75 protected:
76 // inherited from generic_file
77 virtual void inherited_read_ahead(const infinint & amount) override;
78 virtual U_I inherited_read(char *a, U_I size) override;
79 virtual void inherited_write(const char *a, U_I size) override;
80 virtual void inherited_truncate(const infinint & pos) override;
81 virtual void inherited_sync_write() override { flush_write(); };
82 virtual void inherited_flush_read() override { flush_write(); clear_buffer(); };
83 virtual void inherited_terminate() override { flush_write(); };
84
85 private:
86 generic_file *ref;
87 char *buffer;
88 U_I size;
89 U_I half;
90 U_I next;
91 U_I last;
92 U_I first_to_write;
93 infinint buffer_offset;
94 bool shifted_mode;
95 infinint eof_offset;
96
97 bool need_flush_write() const { return first_to_write < last; };
98 void alloc_buffer(size_t x_size);
99 void release_buffer();
100 void shift_by_half();
101 void clear_buffer();
102 void flush_write();
103 void fulfill_read();
104 U_I available_in_cache(skippability direction) const;
105 };
106
108
109} // end of namespace
110
111#endif
112
libdar::cache::inherited_truncate
virtual void inherited_truncate(const infinint &pos) override
truncate file at the give offset
libdar::cache::eof_offset
infinint eof_offset
size of the underlying file (read-only mode), set to zero if unknown
Definition cache.hpp:95
libdar::cache::inherited_read
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
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:82
libdar::cache::shifted_mode
bool shifted_mode
whether to half flush and shift or totally flush data
Definition cache.hpp:94
libdar::cache::cache
cache(generic_file &hidden, bool shift_mode, U_I size=102400)
libdar::cache::skip_relative
virtual bool skip_relative(S_I x) override
skip relatively to the current position
libdar::cache::truncatable
virtual bool truncatable(const infinint &pos) const override
whether the implementation is able to truncate to the given position
libdar::cache::skip
virtual bool skip(const infinint &pos) override
skip at the absolute position
libdar::cache::skip_to_eof
virtual bool skip_to_eof() override
skip to the end of file
libdar::cache::buffer
char * buffer
data in transit
Definition cache.hpp:87
libdar::cache::last
U_I last
first byte of invalid data in the cache. we have: next <= last < size
Definition cache.hpp:91
libdar::cache::alloc_buffer
void alloc_buffer(size_t x_size)
allocate x_size byte in buffer field and set size accordingly
libdar::cache::ref
generic_file * ref
underlying file, (not owned by "this', not to be delete by "this")
Definition cache.hpp:86
libdar::cache::buffer_offset
infinint buffer_offset
position of the first byte in buffer
Definition cache.hpp:93
libdar::cache::inherited_terminate
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
Definition cache.hpp:83
libdar::cache::next
U_I next
next to read or next place to write to
Definition cache.hpp:90
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:92
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::half
U_I half
precalculated half = size / 2
Definition cache.hpp:89
libdar::cache::inherited_sync_write
virtual void inherited_sync_write() override
write down any pending data
Definition cache.hpp:81
libdar::cache::skippable
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
libdar::cache::release_buffer
void release_buffer()
release memory set buffer to nullptr and size to zero
libdar::cache::size
U_I size
allocated size
Definition cache.hpp:88
libdar::cache::get_position
virtual infinint get_position() const override
get the current read/write position
Definition cache.hpp:73
libdar::cache::inherited_write
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
libdar::generic_file::generic_file
generic_file(gf_mode m)
main constructor
Definition generic_file.hpp:80
libdar::generic_file::get_mode
gf_mode get_mode() const
retreive the openning mode for this object
Definition generic_file.hpp:109
libdar::infinint
the arbitrary large positive integer class
Definition real_infinint.hpp:62
generic_file.hpp
class generic_file is defined here as well as class fichier
libdar::gf_read_only
@ gf_read_only
read only access
Definition gf_mode.hpp:45
libdar::gf_read_write
@ gf_read_write
read and write access
Definition gf_mode.hpp:47
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47