Disk ARchive 2.7.16
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-2024 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
33namespace libdar
34{
35
38
40
52 class cache : public generic_file
53 {
54 public:
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:
87 char *buffer;
88 U_I size;
89 U_I half;
90 U_I next;
91 U_I last;
96
97 bool need_flush_write() const { return first_to_write < last; };
98 void alloc_buffer(size_t x_size);
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
the cache class implements a fixed length read/write caching mechanism
Definition: cache.hpp:53
virtual void inherited_truncate(const infinint &pos) override
truncate file at the give offset
infinint eof_offset
size of the underlying file (read-only mode), set to zero if unknown
Definition: cache.hpp:95
virtual U_I inherited_read(char *a, U_I size) override
implementation of read() operation
virtual void inherited_flush_read() override
reset internal engine, flush caches in order to read the data at current position
Definition: cache.hpp:82
bool shifted_mode
whether to half flush and shift or totally flush data
Definition: cache.hpp:94
cache(generic_file &hidden, bool shift_mode, U_I size=102400)
virtual bool skip_relative(S_I x) override
skip relatively to the current position
virtual bool truncatable(const infinint &pos) const override
whether the implementation is able to truncate to the given position
virtual bool skip(const infinint &pos) override
skip at the absolute position
virtual bool skip_to_eof() override
skip to the end of file
char * buffer
data in transit
Definition: cache.hpp:87
U_I last
first byte of invalid data in the cache. we have: next <= last < size
Definition: cache.hpp:91
void alloc_buffer(size_t x_size)
allocate x_size byte in buffer field and set size accordingly
generic_file * ref
underlying file, (not owned by "this', not to be delete by "this")
Definition: cache.hpp:86
infinint buffer_offset
position of the first byte in buffer
Definition: cache.hpp:93
virtual void inherited_terminate() override
destructor-like call, except that it is allowed to throw exceptions
Definition: cache.hpp:83
U_I next
next to read or next place to write to
Definition: cache.hpp:90
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
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...
U_I half
precalculated half = size / 2
Definition: cache.hpp:89
virtual void inherited_sync_write() override
write down any pending data
Definition: cache.hpp:81
virtual bool skippable(skippability direction, const infinint &amount) override
whether the implementation is able to skip
void release_buffer()
release memory set buffer to nullptr and size to zero
U_I size
allocated size
Definition: cache.hpp:88
virtual infinint get_position() const override
get the current read/write position
Definition: cache.hpp:73
virtual void inherited_write(const char *a, U_I size) override
implementation of the write() operation
this is the interface class from which all other data transfer classes inherit
gf_mode get_mode() const
retreive the openning mode for this object
the arbitrary large positive integer class
class generic_file is defined here as well as class fichier
@ gf_read_only
read only access
Definition: gf_mode.hpp:45
@ gf_read_write
read and write access
Definition: gf_mode.hpp:47
switch module to limitint (32 ou 64 bits integers) or infinint
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47