forked from EasyRPG/liblcf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbarray.cpp
More file actions
91 lines (78 loc) · 1.96 KB
/
Copy pathdbarray.cpp
File metadata and controls
91 lines (78 loc) · 1.96 KB
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
#include "lcf/dbarrayalloc.h"
#include "lcf/dbarray.h"
#include "lcf/dbstring.h"
#include <cassert>
#include <cstddef>
//#define LCF_DEBUG_DBARRAY
#ifdef LCF_DEBUG_DBARRAY
#include <iostream>
#endif
namespace lcf {
constexpr DBArrayAlloc::size_type DBArrayAlloc::_empty_buf;
constexpr DBString::size_type DBString::npos;
static ptrdiff_t HeaderSize(size_t align) {
return std::max(sizeof(DBArrayAlloc::size_type), align);
}
static size_t AllocSize(size_t size, size_t align) {
return HeaderSize(align) + size;
}
static void* Adjust(void* p, ptrdiff_t off) {
return reinterpret_cast<void*>(reinterpret_cast<intptr_t>(p) + off);
}
void* DBArrayAlloc::alloc(size_type size, size_type field_size, size_type align) {
if (field_size == 0) {
return empty_buf();
}
assert(align <= alignof(std::max_align_t));
auto* raw = ::operator new(AllocSize(size, align));
auto* p = Adjust(raw, HeaderSize(align));
*get_size_ptr(p) = field_size;
#ifdef LCF_DEBUG_DBARRAY
std::cout << "DBArray: Allocated"
<< " size=" << size
<< " field_size=" << *get_size_ptr(p)
<< " align=" << align
<< " ptr=" << raw
<< " adjusted=" << p
<< std::endl;
#endif
return p;
}
void DBArrayAlloc::free(void* p, size_type align) noexcept {
assert(p != nullptr);
if (p != empty_buf()) {
auto* raw = Adjust(p, -HeaderSize(align));
#ifdef LCF_DEBUG_DBARRAY
std::cout << "DBArray: Free"
<< " align=" << align
<< " ptr=" << raw
<< " adjusted=" << p
<< " field_size=" << *get_size_ptr(p)
<< std::endl;
#endif
::operator delete(raw);
}
}
char* DBString::construct_z(const char* s, size_t len) {
auto* p = alloc(len);
if (len) {
std::memcpy(p, s, len + 1);
}
return p;
}
char* DBString::construct_sv(const char* s, size_t len) {
auto* p = alloc(len);
if (len) {
std::memcpy(p, s, len);
p[len] = '\0';
}
return p;
}
DBString& DBString::operator=(const DBString& o) {
if (this != &o) {
destroy();
_storage = construct_z(o.data(), o.size());
}
return *this;
}
} // namespace lcf