-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.cpp
59 lines (48 loc) · 1.15 KB
/
mem.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
/**
FlowDB Memory Manager
*/
#include "mem.h"
ManagedMemory::ManagedMemory() {
curSize = 64 * 1024 * 1024; // start with 64MB
}
ManagedMemory::ManagedMemory(int p_minSize) {
curSize = p_minSize;
}
ManagedMemory::~ManagedMemory() {
CloseFile();
}
void ManagedMemory::CloseFile() {
munmap(mem, curSize);
fclose(fileHandle);
}
void ManagedMemory::AllocateSpace() {
if(!fileHandle) exit(-1);
fseek(fileHandle, curSize-1, SEEK_SET);
//fputc('\0', fileHandle);
fwrite("", 1, 1, fileHandle);
fseek(fileHandle, 0, SEEK_SET);
}
void* ManagedMemory::OpenFile(const char* filename) {
std::cout << "Open File" << std::endl;
struct stat buffer;
int status;
status = stat(filename, &buffer);
if(buffer.st_size == 0) {
// File not yet created
std::cout << "No File" << std::endl;
fileHandle = fopen(filename, "w");
AllocateSpace();
} else {
std::cout << "File: " << buffer.st_size << std::endl;
// File already created
curSize = buffer.st_size;
fileHandle = fopen(filename, "r+");
}
if(!fileHandle) {
//TODO: log error
exit(-1);
}
int fno = fileno(fileHandle);
mem = mmap(0, curSize, PROT_READ | PROT_WRITE, MAP_SHARED, fno, 0);
return mem;
}