mirror of
https://github.com/intel/compute-runtime.git
synced 2025-12-24 12:23:05 +08:00
fix: avoid calling free on non-heap object
Signed-off-by: Mateusz Jablonski <mateusz.jablonski@intel.com>
This commit is contained in:
committed by
Compute-Runtime-Automation
parent
84e43cc29f
commit
ebfa384a74
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2018-2023 Intel Corporation
|
* Copyright (C) 2018-2024 Intel Corporation
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*
|
*
|
||||||
@@ -40,16 +40,15 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
static constexpr SizeT onStackCaps = onStackCapacity;
|
static constexpr SizeT onStackCaps = onStackCapacity;
|
||||||
|
|
||||||
StackVec() {
|
StackVec() {
|
||||||
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
switchToStackMem();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename ItType>
|
template <typename ItType>
|
||||||
StackVec(ItType beginIt, ItType endIt) {
|
StackVec(ItType beginIt, ItType endIt) {
|
||||||
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
switchToStackMem();
|
||||||
size_t count = (endIt - beginIt);
|
size_t count = (endIt - beginIt);
|
||||||
if (count > onStackCapacity) {
|
if (count > onStackCapacity) {
|
||||||
dynamicMem = new std::vector<DataType>(beginIt, endIt);
|
dynamicMem = new std::vector<DataType>(beginIt, endIt);
|
||||||
setUsesDynamicMem();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,10 +60,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
}
|
}
|
||||||
|
|
||||||
StackVec(const StackVec &rhs) {
|
StackVec(const StackVec &rhs) {
|
||||||
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
switchToStackMem();
|
||||||
if (onStackCaps < rhs.size()) {
|
if (onStackCaps < rhs.size()) {
|
||||||
dynamicMem = new std::vector<DataType>(rhs.begin(), rhs.end());
|
dynamicMem = new std::vector<DataType>(rhs.begin(), rhs.end());
|
||||||
setUsesDynamicMem();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,12 +73,12 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
|
|
||||||
explicit StackVec(size_t initialSize)
|
explicit StackVec(size_t initialSize)
|
||||||
: StackVec() {
|
: StackVec() {
|
||||||
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
switchToStackMem();
|
||||||
resize(initialSize);
|
resize(initialSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
StackVec(std::initializer_list<DataType> init) {
|
StackVec(std::initializer_list<DataType> init) {
|
||||||
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
switchToStackMem();
|
||||||
reserve(init.size());
|
reserve(init.size());
|
||||||
for (const auto &obj : init) {
|
for (const auto &obj : init) {
|
||||||
push_back(obj);
|
push_back(obj);
|
||||||
@@ -100,7 +98,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
|
|
||||||
if (onStackCaps < rhs.size()) {
|
if (onStackCaps < rhs.size()) {
|
||||||
this->dynamicMem = new std::vector<DataType>(rhs.begin(), rhs.end());
|
this->dynamicMem = new std::vector<DataType>(rhs.begin(), rhs.end());
|
||||||
setUsesDynamicMem();
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,8 +112,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
||||||
if (rhs.usesDynamicMem()) {
|
if (rhs.usesDynamicMem()) {
|
||||||
this->dynamicMem = rhs.dynamicMem;
|
this->dynamicMem = rhs.dynamicMem;
|
||||||
setUsesDynamicMem();
|
rhs.switchToStackMem();
|
||||||
rhs.onStackSize = 0U;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,8 +134,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
delete this->dynamicMem;
|
delete this->dynamicMem;
|
||||||
}
|
}
|
||||||
this->dynamicMem = rhs.dynamicMem;
|
this->dynamicMem = rhs.dynamicMem;
|
||||||
this->setUsesDynamicMem();
|
rhs.switchToStackMem();
|
||||||
rhs.onStackSize = 0U;
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,7 +329,7 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool usesDynamicMem() const {
|
bool usesDynamicMem() const {
|
||||||
return std::numeric_limits<decltype(onStackSize)>::max() == this->onStackSize;
|
return reinterpret_cast<uintptr_t>(this->onStackMem) != reinterpret_cast<uintptr_t>(onStackMemRawBytes) && this->dynamicMem;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto data() {
|
auto data() {
|
||||||
@@ -347,9 +342,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
private:
|
private:
|
||||||
template <typename RhsDataType, size_t rhsOnStackCapacity, typename RhsStackSizeT>
|
template <typename RhsDataType, size_t rhsOnStackCapacity, typename RhsStackSizeT>
|
||||||
friend class StackVec;
|
friend class StackVec;
|
||||||
void setUsesDynamicMem() {
|
|
||||||
this->onStackSize = std::numeric_limits<decltype(onStackSize)>::max();
|
|
||||||
}
|
|
||||||
|
|
||||||
void resizeImpl(size_t newSize, const DataType *value) {
|
void resizeImpl(size_t newSize, const DataType *value) {
|
||||||
// new size does not fit into internal mem
|
// new size does not fit into internal mem
|
||||||
@@ -408,7 +400,6 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
}
|
}
|
||||||
clearStackObjects();
|
clearStackObjects();
|
||||||
}
|
}
|
||||||
setUsesDynamicMem();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearStackObjects() {
|
void clearStackObjects() {
|
||||||
@@ -422,6 +413,9 @@ class StackVec { // NOLINT(clang-analyzer-optin.performance.Padding)
|
|||||||
it->~DataType();
|
it->~DataType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void switchToStackMem() {
|
||||||
|
onStackMem = reinterpret_cast<DataType *const>(onStackMemRawBytes);
|
||||||
|
}
|
||||||
|
|
||||||
union {
|
union {
|
||||||
std::vector<DataType> *dynamicMem;
|
std::vector<DataType> *dynamicMem;
|
||||||
|
|||||||
Reference in New Issue
Block a user