-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm_manager.cc
66 lines (54 loc) · 1.6 KB
/
pm_manager.cc
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
#include "pm_manager.h"
#include <string>
using std::string;
#define LAYOUT_NAME "pool_layout"
PMmanager::PMmanager(std::string db_name): used(0) {
static const size_t pmem_len = 1L * 1024 * 1024 * 1024;
static const std::string path = "/home/oslab/Desktop/pmem/";
std::string pool_name = path + db_name;
//std::cout<<"Enter PMmanager :"<<"path name = "<<pool_name<<std::endl;
pool = pmemobj_open(pool_name.c_str(), LAYOUT_NAME);
if (pool == NULL) {
perror("pmemobj_open error");
pool = pmemobj_create(pool_name.c_str(), LAYOUT_NAME, pmem_len, 0666);
if (pool == NULL) {
std::cout<<" pool create fail"<<std::endl;
perror("pmemobj_create error");
exit(1);
}
}
free = pmem_len;
}
PMmanager::~PMmanager() {
pmemobj_close(pool); //有問題要解決
}
void *PMmanager::Allocate(size_t bytes) {
PMEMoid oid;
int ret = pmemobj_alloc(pool, &oid, bytes, 0, NULL, NULL);
//std::cout<<"Allocate:"<<bytes<<"byte"<<std::endl;
if (ret == 0) {
return pmemobj_direct(oid);
} else {
// 分配失败,返回NULL
return NULL;
}
}
void PMmanager::Free(void* ptr) {
if (ptr == NULL) {
return; // 如果 ptr 為 NULL,則無需執行任何操作
}
PMEMoid oid = pmemobj_oid(ptr);
if (!OID_IS_NULL(oid)) {
pmemobj_free(&oid);
} else {
perror("pmemobj_free error");
}
}
//flush to PM
void PMmanager::Sync(void *start, size_t len) {
if (is_pmem) {
pmem_persist(start, len);
} else {
pmem_msync(start, len);
}
}