forked from manticoresoftware/manticoresearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.h
113 lines (88 loc) · 2.61 KB
/
storage.h
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// Copyright (c) 2017-2023, Manticore Software LTD (https://manticoresearch.com)
// Copyright (c) 2001-2016, Andrew Aksyonoff
// Copyright (c) 2008-2016, Sphinx Technologies Inc
// All rights reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org
//
#pragma once
#include "type_traits.h"
//////////////////////////////////////////////////////////////////////////
/// Storage backends for vector
/// Each backend provides Allocate and Deallocate
namespace sph
{
/// Default backend - uses plain old new/delete
template<typename T>
class DefaultStorage_T
{
public:
using TYPE = T;
inline static TYPE* Allocate ( int64_t iLimit )
{
return new T[iLimit];
}
inline static void Deallocate ( TYPE* pData )
{
delete[] pData;
}
static constexpr bool is_sized = false;
static constexpr bool is_constructed = true;
static constexpr bool is_owned = false;
};
/// Static backend: small blobs stored localy,
/// bigger came to plain old new/delete
template<typename T, int STATICSIZE = 4096>
class LazyStorage_T
{
public:
using TYPE = T;
// don't allow moving (it has no sence with embedded buffer)
inline LazyStorage_T ( LazyStorage_T&& rhs ) noexcept = delete;
inline LazyStorage_T& operator= ( LazyStorage_T&& rhs ) noexcept = delete;
LazyStorage_T() = default;
static const int iSTATICSIZE = STATICSIZE;
public:
inline TYPE* Allocate ( int64_t iLimit )
{
if ( iLimit <= STATICSIZE )
return m_dData;
return new T[iLimit];
}
inline void Deallocate ( TYPE* pData ) const
{
if ( pData != m_dData )
delete[] pData;
}
static constexpr bool is_sized = false;
static constexpr bool is_constructed = true;
static constexpr bool is_owned = true;
private:
T m_dData[iSTATICSIZE];
};
/// optional backend - allocates space but *not* calls ctrs and dtrs
/// bigger came to plain old new/delete
template<typename T>
class RawStorage_T
{
using StorageType = typename std::aligned_storage<sizeof ( T ), alignof ( T )>::type;
public:
using TYPE = T;
inline static TYPE* Allocate ( int64_t iLimit )
{
return (TYPE*)new StorageType[iLimit];
}
inline static void Deallocate ( TYPE* pData )
{
delete[] reinterpret_cast<StorageType*> ( pData );
}
static constexpr bool is_sized = false;
// static const bool is_constructed = IS_TRIVIALLY_COPYABLE( T );
static constexpr bool is_constructed = IS_TRIVIALLY_DEFAULT_CONSTRUCTIBLE ( T );
static constexpr bool is_owned = false;
};
} // namespace sph