Loading...
Searching...
No Matches
cache.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2024 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author, see the AUTHOR file
/*********************************************************************/
#ifndef CACHE_HPP
#define CACHE_HPP
#include "../my_config.h"
#include "infinint.hpp"
#include "generic_file.hpp"
namespace libdar
{
class cache : public generic_file
{
public:
cache(generic_file & hidden,
bool shift_mode,
U_I size = 102400
);
cache(const cache & ref) = delete;
cache(cache && ref) = delete;
cache & operator = (const cache & ref) = delete;
cache & operator = (cache && ref) = delete;
~cache();
void change_to_read_write() { if(get_mode() == gf_read_only) throw SRC_BUG; set_mode(gf_read_write); };
// inherited from generic_file
virtual bool skippable(skippability direction, const infinint & amount) override;
virtual bool skip(const infinint & pos) override;
virtual bool skip_to_eof() override;
virtual bool skip_relative(S_I x) override;
virtual bool truncatable(const infinint & pos) const override;
virtual infinint get_position() const override { return buffer_offset + next; };
protected:
// inherited from generic_file
virtual void inherited_read_ahead(const infinint & amount) override;
virtual U_I inherited_read(char *a, U_I size) override;
virtual void inherited_write(const char *a, U_I size) override;
virtual void inherited_truncate(const infinint & pos) override;
virtual void inherited_sync_write() override { flush_write(); };
virtual void inherited_flush_read() override { flush_write(); clear_buffer(); };
virtual void inherited_terminate() override { flush_write(); };
private:
generic_file *ref;
char *buffer;
U_I size;
U_I half;
U_I next;
U_I last;
U_I first_to_write;
infinint buffer_offset;
bool shifted_mode;
infinint eof_offset;
bool need_flush_write() const { return first_to_write < last; };
void alloc_buffer(size_t x_size);
void release_buffer();
void shift_by_half();
void clear_buffer();
void flush_write();
void fulfill_read();
U_I available_in_cache(skippability direction) const;
};
} // end of namespace
#endif
libdar::cache
the cache class implements a fixed length read/write caching mechanism
Definition cache.hpp:53
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
this is the interface class from which all other data transfer classes inherit
Definition generic_file.hpp:77
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