pytorch/c10/util/SmallBuffer.h
Lukas N Wirz 301d9c0556 Remove deprecated usage of is_pod/is_pod_v (#88918)
… as equivalent replacements for std::is_pod and std::is_pod_v because they are deprecated in C++20.

When consuming libtorch header files in a project that uses C++20, there are warnings about std::is_pod being deprecated.  This patch fixes that issue.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/88918
Approved by: https://github.com/ezyang
2022-12-05 16:50:00 +00:00

71 lines
1.2 KiB
C++

#pragma once
#include <type_traits>
/** Helper class for allocating temporary fixed size arrays with SBO.
*
* This is intentionally much simpler than SmallVector, to improve performace at
* the expense of many features:
* - No zero-initialization for numeric types
* - No resizing after construction
* - No copy/move
* - No non-trivial types
*/
namespace c10 {
template <typename T, size_t N>
class SmallBuffer {
static_assert(
std::is_trivial<T>::value,
"SmallBuffer is intended for POD types");
T storage_[N];
size_t size_;
T* data_;
public:
SmallBuffer(size_t size) : size_(size) {
if (size > N) {
data_ = new T[size];
} else {
data_ = &storage_[0];
}
}
~SmallBuffer() {
if (size_ > N) {
delete[] data_;
}
}
T& operator[](int64_t idx) {
return data()[idx];
}
const T& operator[](int64_t idx) const {
return data()[idx];
}
T* data() {
return data_;
}
const T* data() const {
return data_;
}
size_t size() const {
return size_;
}
T* begin() {
return data_;
}
const T* begin() const {
return data_;
}
T* end() {
return data_ + size_;
}
const T* end() const {
return data_ + size_;
}
};
} // namespace c10