wavelength 1.0.0
Loading...
Searching...
No Matches
blockmgr.h
Go to the documentation of this file.
1#pragma once
2
3#include <list>
4#include <memory>
5#include <vector>
6
7#include "os/arkfile.h"
8
15class Block {
16public:
24
32 static int NextBuffer();
33
41 static int CurrentTime();
42
49 void Touch();
50
52 char *GetBuffer() const {
53 return mBuffer;
54 }
55
57 int GetBlockNum() const {
58 return mBlockNum;
59 }
60
66 void SetBlockNum(int nBlockNum) {
67 mBlockNum = nBlockNum;
68 }
69
71 int GetTimestamp() const {
72 return mTimestamp;
73 }
74
75private:
76 char *mBuffer;
77 int mBlockNum;
78 int mTimestamp;
79};
80
86struct AsyncTask {
98 AsyncTask(ArkFile *pFile, char *pBuffer, int nBlock, int nStart, int nEnd);
99
108
109 int mBlock;
110 int mStart;
111 int mEnd;
112 char *mBuffer;
114};
115
130 BlockRequest(int nBlock, const AsyncTask &task);
131
133 std::list<AsyncTask> mTasks;
134};
135
142class BlockMgr {
143public:
150 void Init();
151
163 void GetBlockSpan(long long nOffset, int nBytes, int *pnFirst, int *pnCount, int *pnBlockSize);
164
173
182 int SectorToByte(int nSector);
183
192 char *GetBlockData(int nBlock);
193
201 void AddRequest(const AsyncTask &task);
202
211 void Poll();
212
221 Block *FindBlock(int nBlock);
222
231
239 static bool ReadDone();
240
248 static int ReadError();
249
260 static bool Read(int nSector, int nCount, void *pBuffer);
261
263 const std::vector<std::unique_ptr<Block>> &GetBlocks() const {
264 return mBlocks;
265 }
266
267private:
268 std::list<BlockRequest> mRequests;
269 std::vector<std::unique_ptr<Block>> mBlocks;
270 Block *mLoading;
271 int mSectorsPerBlock;
272};
273
279extern BlockMgr TheBlockMgr;
BlockMgr TheBlockMgr
The disc cache.
Read-only file of the main archive, read through the disc cache.
Definition arkfile.h:11
Cache of disc blocks that ArkFile reads go through.
Definition blockmgr.h:142
void GetBlockSpan(long long nOffset, int nBytes, int *pnFirst, int *pnCount, int *pnBlockSize)
Report the blocks a run of bytes of the disc covers.
void KillBlockRequests(ArkFile *pFile)
Drop every task of a file, and every request left with no task unless its block is loading.
static bool ReadDone()
Report whether the read of the drive finished.
void AddRequest(const AsyncTask &task)
Queue a task, joining the request for its block or inserting a new one in block order.
Block * FindBlock(int nBlock)
Find the block holding a block of the disc.
std::vector< Block * > mBlocks
Definition BlockMgr.h:91
int SectorToByte(int nSector)
Report the byte of the disc a sector starts at.
char * GetBlockData(int nBlock)
Report the data of a loaded block and mark the block used.
Block * FindOldestBlock()
Find the block used longest ago.
const std::vector< std::unique_ptr< Block > > & GetBlocks() const
Definition blockmgr.h:263
void Init()
Build the blocks of the cache.
void Poll()
Finish the block that loaded, completing its tasks, and start loading the next request.
static bool Read(int nSector, int nCount, void *pBuffer)
Start reading sectors of the disc.
static int ReadError()
Report the error of the last read of the drive.
One block buffer of the disc cache.
Definition blockmgr.h:15
int GetBlockNum() const
Definition blockmgr.h:57
void Touch()
Mark the block used now.
Block()
Take the next buffer and mark the block used now.
static int NextBuffer()
Report the index of the next buffer a block takes, and advance it.
int GetTimestamp() const
Definition blockmgr.h:71
static int CurrentTime()
Report the latest use mark Touch() handed out.
void SetBlockNum(int nBlockNum)
Set the block of the disc the buffer stores.
Definition blockmgr.h:66
char * GetBuffer() const
Definition blockmgr.h:52
One part of an ArkFile read, which a single block of the disc satisfies.
Definition blockmgr.h:86
int mBlock
Definition blockmgr.h:109
int mStart
Definition blockmgr.h:110
int mEnd
Definition blockmgr.h:111
AsyncTask(ArkFile *pFile, char *pBuffer, int nBlock, int nStart, int nEnd)
Describe the part of a read.
char * mBuffer
Definition blockmgr.h:112
bool TryComplete()
Copy the bytes from the cache when their block is loaded, and report them to the file.
ArkFile * mFile
Definition blockmgr.h:113
BlockRequest(int nBlock, const AsyncTask &task)
Start a request with its first task.
std::list< AsyncTask > mTasks
Definition blockmgr.h:133
int mBlockNum
Definition blockmgr.h:132