diff --git a/math/vecops/inc/ROOT/RVec.hxx b/math/vecops/inc/ROOT/RVec.hxx index eb50279bd2f30..7ecc5c075ac50 100644 --- a/math/vecops/inc/ROOT/RVec.hxx +++ b/math/vecops/inc/ROOT/RVec.hxx @@ -398,7 +398,7 @@ void R__CLING_PTRCHECK(off) SmallVectorTemplateBase::grow( // Always grow, even from zero. size_t NewCapacity = size_t(NextPowerOf2(this->capacity() + 2)); NewCapacity = std::min(std::max(NewCapacity, MinSize), this->SizeTypeMax()); - T *NewElts = static_cast(malloc(NewCapacity * sizeof(T))); + T *NewElts = static_cast(::operator new(NewCapacity * sizeof(T), std::nothrow)); R__ASSERT(NewElts != nullptr); // Move the elements over. @@ -410,7 +410,7 @@ void R__CLING_PTRCHECK(off) SmallVectorTemplateBase::grow( // If this wasn't grown from the inline copy, deallocate the old space. if (!this->isSmall()) - free(this->begin()); + ::operator delete(this->begin()); } this->fBeginX = NewElts; @@ -560,7 +560,7 @@ public: // Subclass has already destructed this vector's elements. // If this wasn't grown from the inline copy, deallocate the old space. if (!this->isSmall() && this->Owns()) - free(this->begin()); + ::operator delete(this->begin()); } // also give up adopted memory if applicable @@ -1037,7 +1037,7 @@ RVecImpl &RVecImpl::operator=(RVecImpl &&RHS) noexcept(kIsNoExcept) if (this->Owns()) { this->destroy_range(this->begin(), this->end()); if (!this->isSmall()) - free(this->begin()); + ::operator delete(this->begin()); } this->fBeginX = RHS.fBeginX; this->fSize = RHS.fSize; diff --git a/math/vecops/src/RVec.cxx b/math/vecops/src/RVec.cxx index 6dee0f204199b..9ca7bec51292f 100644 --- a/math/vecops/src/RVec.cxx +++ b/math/vecops/src/RVec.cxx @@ -71,17 +71,14 @@ void ROOT::Internal::VecOps::SmallVectorBase::grow_pod(void *FirstEl, size_t Min size_t NewCapacity = 2 * capacity() + 1; // Always grow. NewCapacity = std::min(std::max(NewCapacity, MinSize), SizeTypeMax()); - void *NewElts; - if (fBeginX == FirstEl || !this->Owns()) { - NewElts = malloc(NewCapacity * TSize); - R__ASSERT(NewElts != nullptr); - - // Copy the elements over. No need to run dtors on PODs. - memcpy(NewElts, this->fBeginX, size() * TSize); - } else { - // If this wasn't grown from the inline copy, grow the allocated space. - NewElts = realloc(this->fBeginX, NewCapacity * TSize); - R__ASSERT(NewElts != nullptr); + void *NewElts = ::operator new(NewCapacity * TSize, std::nothrow); + R__ASSERT(NewElts != nullptr); + // Copy the elements over. No need to run dtors on PODs. + memcpy(NewElts, this->fBeginX, size() * TSize); + + if (fBeginX != FirstEl && this->Owns()) { + // If this wasn't grown from the inline copy, delete the previously allocated space. + ::operator delete(this->fBeginX); } this->fBeginX = NewElts; diff --git a/tree/ntuple/src/RFieldSequenceContainer.cxx b/tree/ntuple/src/RFieldSequenceContainer.cxx index bea2179cae7dc..b417149f325f1 100644 --- a/tree/ntuple/src/RFieldSequenceContainer.cxx +++ b/tree/ntuple/src/RFieldSequenceContainer.cxx @@ -8,10 +8,9 @@ #include #include -#include // for malloc, free #include #include -#include // hardware_destructive_interference_size +#include // hardware_destructive_interference_size, operator new namespace { @@ -199,7 +198,7 @@ ROOT::RRVecField::RRVecField(std::string_view fieldName, std::unique_ptrGetAlignment() > sizeof(std::max_align_t)) { - // RVec uses malloc() and free() + // RVec uses operator new without alignment info throw RException(R__FAIL("RVec does not support over-aligned types")); } @@ -314,9 +313,8 @@ unsigned char *ROOT::RRVecField::ResizeRVec(void *rvec, std::size_t nItems, std: } Internal::DestroyRVecWithChecks(itemField->GetAlignment(), beginPtr, capacityPtr); - // We trust that malloc returns a buffer with large enough alignment. - // This might not be the case if T in RVec is over-aligned. - *beginPtr = static_cast(malloc(newCapacity * itemSize)); + // We checked that the item type is not over-aligned + *beginPtr = static_cast(::operator new(newCapacity * itemSize, std::nothrow)); R__ASSERT(*beginPtr != nullptr); *capacityPtr = static_cast(newCapacity); diff --git a/tree/ntuple/src/RFieldUtils.cxx b/tree/ntuple/src/RFieldUtils.cxx index d8b37d46baa60..74be56df762d0 100644 --- a/tree/ntuple/src/RFieldUtils.cxx +++ b/tree/ntuple/src/RFieldUtils.cxx @@ -894,5 +894,5 @@ void ROOT::Internal::DestroyRVecWithChecks(std::size_t alignOfT, unsigned char * const bool owns = (*capacityPtr != -1); if (!isSmall && owns) - free(*beginPtr); + ::operator delete(*beginPtr); }