diff --git a/src/common/hist_util.h b/src/common/hist_util.h index be48bdbbb49a..442bddfcdaab 100644 --- a/src/common/hist_util.h +++ b/src/common/hist_util.h @@ -642,39 +642,6 @@ class GHistBuilder { /*! \brief number of all bins over all features */ uint32_t nbins_ { 0 }; }; - -/*! - * \brief A C-style array with in-stack allocation. As long as the array is smaller than - * MaxStackSize, it will be allocated inside the stack. Otherwise, it will be - * heap-allocated. - */ -template -class MemStackAllocator { - public: - explicit MemStackAllocator(size_t required_size) : required_size_(required_size) { - if (MaxStackSize >= required_size_) { - ptr_ = stack_mem_; - } else { - ptr_ = reinterpret_cast(malloc(required_size_ * sizeof(T))); - } - if (!ptr_) { - throw std::bad_alloc{}; - } - } - - ~MemStackAllocator() { - if (required_size_ > MaxStackSize) { - free(ptr_); - } - } - T& operator[](size_t i) { return ptr_[i]; } - T const& operator[](size_t i) const { return ptr_[i]; } - - private: - T* ptr_ = nullptr; - size_t required_size_; - T stack_mem_[MaxStackSize]; -}; } // namespace common } // namespace xgboost #endif // XGBOOST_COMMON_HIST_UTIL_H_ diff --git a/src/common/threading_utils.h b/src/common/threading_utils.h index 5c8dfc3ff05f..95357ff6316f 100644 --- a/src/common/threading_utils.h +++ b/src/common/threading_utils.h @@ -246,6 +246,40 @@ inline int32_t OmpGetNumThreads(int32_t n_threads) { n_threads = std::max(n_threads, 1); return n_threads; } + + +/*! + * \brief A C-style array with in-stack allocation. As long as the array is smaller than + * MaxStackSize, it will be allocated inside the stack. Otherwise, it will be + * heap-allocated. + */ +template +class MemStackAllocator { + public: + explicit MemStackAllocator(size_t required_size) : required_size_(required_size) { + if (MaxStackSize >= required_size_) { + ptr_ = stack_mem_; + } else { + ptr_ = reinterpret_cast(malloc(required_size_ * sizeof(T))); + } + if (!ptr_) { + throw std::bad_alloc{}; + } + } + + ~MemStackAllocator() { + if (required_size_ > MaxStackSize) { + free(ptr_); + } + } + T& operator[](size_t i) { return ptr_[i]; } + T const& operator[](size_t i) const { return ptr_[i]; } + + private: + T* ptr_ = nullptr; + size_t required_size_; + T stack_mem_[MaxStackSize]; +}; } // namespace common } // namespace xgboost diff --git a/src/data/gradient_index.cc b/src/data/gradient_index.cc index a56232360791..eef8f9519807 100644 --- a/src/data/gradient_index.cc +++ b/src/data/gradient_index.cc @@ -10,6 +10,7 @@ #include "../common/column_matrix.h" #include "../common/hist_util.h" +#include "../common/threading_utils.h" namespace xgboost {