1
1
/*
2
- * Copyright (C) 2018-2023 Intel Corporation
2
+ * Copyright (C) 2018-2024 Intel Corporation
3
3
*
4
4
* SPDX-License-Identifier: MIT
5
5
*
@@ -40,16 +40,15 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
40
40
static constexpr SizeT onStackCaps = onStackCapacity;
41
41
42
42
StackVec () {
43
- onStackMem = reinterpret_cast <DataType * const >(onStackMemRawBytes );
43
+ switchToStackMem ( );
44
44
}
45
45
46
46
template <typename ItType>
47
47
StackVec (ItType beginIt, ItType endIt) {
48
- onStackMem = reinterpret_cast <DataType * const >(onStackMemRawBytes );
48
+ switchToStackMem ( );
49
49
size_t count = (endIt - beginIt);
50
50
if (count > onStackCapacity) {
51
51
dynamicMem = new std::vector<DataType>(beginIt, endIt);
52
- setUsesDynamicMem ();
53
52
return ;
54
53
}
55
54
@@ -61,10 +60,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
61
60
}
62
61
63
62
StackVec (const StackVec &rhs) {
64
- onStackMem = reinterpret_cast <DataType * const >(onStackMemRawBytes );
63
+ switchToStackMem ( );
65
64
if (onStackCaps < rhs.size ()) {
66
65
dynamicMem = new std::vector<DataType>(rhs.begin (), rhs.end ());
67
- setUsesDynamicMem ();
68
66
return ;
69
67
}
70
68
@@ -75,12 +73,12 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
75
73
76
74
explicit StackVec (size_t initialSize)
77
75
: StackVec() {
78
- onStackMem = reinterpret_cast <DataType * const >(onStackMemRawBytes );
76
+ switchToStackMem ( );
79
77
resize (initialSize);
80
78
}
81
79
82
80
StackVec (std::initializer_list<DataType> init) {
83
- onStackMem = reinterpret_cast <DataType * const >(onStackMemRawBytes );
81
+ switchToStackMem ( );
84
82
reserve (init.size ());
85
83
for (const auto &obj : init) {
86
84
push_back (obj);
@@ -100,7 +98,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
100
98
101
99
if (onStackCaps < rhs.size ()) {
102
100
this ->dynamicMem = new std::vector<DataType>(rhs.begin (), rhs.end ());
103
- setUsesDynamicMem ();
104
101
return *this ;
105
102
}
106
103
@@ -115,8 +112,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
115
112
onStackMem = reinterpret_cast <DataType *const >(onStackMemRawBytes);
116
113
if (rhs.usesDynamicMem ()) {
117
114
this ->dynamicMem = rhs.dynamicMem ;
118
- setUsesDynamicMem ();
119
- rhs.onStackSize = 0U ;
115
+ rhs.switchToStackMem ();
120
116
return ;
121
117
}
122
118
@@ -138,8 +134,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
138
134
delete this ->dynamicMem ;
139
135
}
140
136
this ->dynamicMem = rhs.dynamicMem ;
141
- this ->setUsesDynamicMem ();
142
- rhs.onStackSize = 0U ;
137
+ rhs.switchToStackMem ();
143
138
return *this ;
144
139
}
145
140
@@ -334,7 +329,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
334
329
}
335
330
336
331
bool usesDynamicMem () const {
337
- return std::numeric_limits< decltype (onStackSize)>:: max () == this ->onStackSize ;
332
+ return reinterpret_cast < uintptr_t >( this -> onStackMem ) != reinterpret_cast < uintptr_t >(onStackMemRawBytes) && this ->dynamicMem ;
338
333
}
339
334
340
335
auto data () {
@@ -347,9 +342,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
347
342
private:
348
343
template <typename RhsDataType, size_t rhsOnStackCapacity, typename RhsStackSizeT>
349
344
friend class StackVec ;
350
- void setUsesDynamicMem () {
351
- this ->onStackSize = std::numeric_limits<decltype (onStackSize)>::max ();
352
- }
353
345
354
346
void resizeImpl (size_t newSize, const DataType *value) {
355
347
// new size does not fit into internal mem
@@ -408,7 +400,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
408
400
}
409
401
clearStackObjects ();
410
402
}
411
- setUsesDynamicMem ();
412
403
}
413
404
414
405
void clearStackObjects () {
@@ -422,6 +413,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
422
413
it->~DataType ();
423
414
}
424
415
}
416
+ void switchToStackMem () {
417
+ onStackMem = reinterpret_cast <DataType *const >(onStackMemRawBytes);
418
+ }
425
419
426
420
union {
427
421
std::vector<DataType> *dynamicMem;
0 commit comments