This repository has been archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Storage.cpp
71 lines (61 loc) · 1.99 KB
/
Storage.cpp
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
#ifndef STORAGE_HEADER
#define STORAGE_HEADER
#include <iostream>
#include <vector>
#include <math.h>
#include "record.h"
using namespace std;
class Storage {
public:
vector<char*> blockspointers;
vector<void*> datapointers;
size_t storagesize;
size_t blocksize;
unsigned int numallocatedblocks;
size_t currentblockutilized;
char* storageptr;
char* blockptr;
Storage(size_t storagesize, size_t blocksize){
this->storagesize = storagesize;
this->blocksize = blocksize;
this->storageptr = nullptr;
this->storageptr = new char[storagesize];
this->blockptr = nullptr;
this->numallocatedblocks = 0;
this->currentblockutilized = 0;
};
//int getNumberOfDataBlocks();
bool AllocateBlock(){
if((numallocatedblocks * blocksize) < storagesize){
blockptr = storageptr + (numallocatedblocks * blocksize);
blockspointers.push_back(blockptr);
numallocatedblocks += 1;
currentblockutilized = 0;
return true;
} else {
return false;
}
}
void* writeToDisk(void* ptr, size_t size){
if((currentblockutilized + size) > blocksize or numallocatedblocks == 0){
if(!AllocateBlock()){
throw "Unable to allocate block";
}
}
char* destptr = blockptr + currentblockutilized;
memcpy(destptr, ptr, size);
datapointers.push_back(destptr);
currentblockutilized += size;
return destptr;
}
void* loadFromDisk(void* ptr, size_t size){
void* destptr = operator new(size);
memcpy(destptr, (char *)ptr, size);
return destptr;
}
~Storage(){
delete storageptr;
storageptr = nullptr;
}
};
#endif