1 #include "lcf/dbarrayalloc.h"
2 #include "lcf/dbarray.h"
3 #include "lcf/dbstring.h"
9 #ifdef LCF_DEBUG_DBARRAY
15 const DBArrayAlloc::size_type DBArrayAlloc::_empty_buf[2] = { 0, 0 };
16 constexpr DBString::size_type DBString::npos;
19 return std::max(
sizeof(DBArrayAlloc::size_type), align);
22 static size_t AllocSize(
size_t size,
size_t align) {
26 static void*
Adjust(
void* p, ptrdiff_t off) {
27 return reinterpret_cast<void*
>(
reinterpret_cast<intptr_t
>(p) + off);
30 void* DBArrayAlloc::alloc(size_type size, size_type field_size, size_type align) {
31 if (field_size == 0) {
34 assert(align <=
alignof(std::max_align_t));
35 auto* raw = ::operator
new(
AllocSize(size, align));
37 *get_size_ptr(p) = field_size;
38 #ifdef LCF_DEBUG_DBARRAY
39 std::cout <<
"DBArray: Allocated"
41 <<
" field_size=" << *get_size_ptr(p)
50 void DBArrayAlloc::free(
void* p, size_type align) noexcept {
52 if (p != empty_buf()) {
54 #ifdef LCF_DEBUG_DBARRAY
55 std::cout <<
"DBArray: Free"
59 <<
" field_size=" << *get_size_ptr(p)
62 ::operator
delete(raw);
66 char* DBString::construct_z(
const char* s,
size_t len) {
69 std::memcpy(p, s, len + 1);
74 char* DBString::construct_sv(
const char* s,
size_t len) {
77 std::memcpy(p, s, len);
83 DBString& DBString::operator=(
const DBString& o) {
86 _storage = construct_z(o.data(), o.size());
static ptrdiff_t HeaderSize(size_t align)
static void * Adjust(void *p, ptrdiff_t off)
static size_t AllocSize(size_t size, size_t align)