1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/***************************************************************************
* Copyright (C) 2005 by Joris Guisson *
* joris.guisson@gmail.com *
* *
* 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. *
***************************************************************************/
#ifndef BTCACHEFILE_H
#define BTCACHEFILE_H
#include <tqmap.h>
#include <tqmutex.h>
#include <tqstring.h>
#include <util/constants.h>
namespace bt
{
class PreallocationThread;
/**
* Interface which classes must implement to be able to map something from a CacheFile
* It will also be used to notify when things get unmapped or remapped
*/
class MMappeable
{
public:
virtual ~MMappeable() {}
/**
* When a CacheFile is closed, this will be called on all existing mappings.
*/
virtual void unmapped() = 0;
};
/**
@author Joris Guisson <joris.guisson@gmail.com>
Used by Single and MultiFileCache to write to disk.
*/
class CacheFile
{
public:
CacheFile();
virtual ~CacheFile();
enum Mode
{
READ,WRITE,RW
};
/**
* Open the file.
* @param path Path of the file
* @param size Max size of the file
* @throw Error when something goes wrong
*/
void open(const TQString & path,Uint64 size);
/// Change the path of the file
void changePath(const TQString & npath);
/**
* Map a part of the file into memory, will expand the file
* if it is to small, but will not go past the limit set in open.
* @param thing The thing that wishes to map the mmapping
* @param off Offset into the file
* @param size Size of the region to map
* @param mode How the region will be mapped
* @return A ptr to the mmaped region, or 0 if something goes wrong
*/
void* map(MMappeable* thing,Uint64 off,Uint32 size,Mode mode);
/**
* Unmap a previously mapped region.
* @param ptr Ptr to the region
* @param size Size of the region
*/
void unmap(void* ptr,Uint32 size);
/**
* Close the file, everything will be unmapped.
* @param to_be_reopened Indicates if the close is temporarely (i.e. it will be reopened)
*/
void close();
/**
* Read from the file.
* @param buf Buffer to store data
* @param size Size to read
* @param off Offset to read from in file
*/
void read(Uint8* buf,Uint32 size,Uint64 off);
/**
* Write to the file.
* @param buf Buffer to write
* @param size Size to read
* @param off Offset to read from in file
*/
void write(const Uint8* buf,Uint32 size,Uint64 off);
/**
* Preallocate disk space
*/
void preallocate(PreallocationThread* prealloc);
/// Get the number of bytes this cache file is taking up
Uint64 diskUsage();
private:
void growFile(Uint64 to_write);
void closeTemporary();
void openFile(Mode mode);
private:
int fd;
bool read_only;
Uint64 max_size,file_size;
TQString path;
struct Entry
{
MMappeable* thing;
void* ptr;
Uint32 size;
Uint64 offset;
Uint32 diff;
Mode mode;
};
TQMap<void*,Entry> mappings; // mappings where offset wasn't a multiple of 4K
mutable TQMutex mutex;
};
}
#endif
|