重构对齐分配器和缓冲区类,统一类型参数命名,提升代码可读性
This commit is contained in:
@@ -61,32 +61,32 @@ inline void aligned_free(void* ptr) {
|
||||
}
|
||||
|
||||
// 对齐分配器模板类
|
||||
template<typename type, size_t alignment = ALIGNMENT_AVX>
|
||||
template<typename T, size_t Alignment = ALIGNMENT_AVX>
|
||||
class aligned_allocator {
|
||||
public:
|
||||
using value_type = type;
|
||||
using pointer = type*;
|
||||
using const_pointer = const type*;
|
||||
using reference = type&;
|
||||
using const_reference = const type&;
|
||||
using value_type = T;
|
||||
using pointer = T*;
|
||||
using const_pointer = const T*;
|
||||
using reference = T&;
|
||||
using const_reference = const T&;
|
||||
using size_type = std::size_t;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
|
||||
template<typename u>
|
||||
template<typename U>
|
||||
struct rebind {
|
||||
using other = aligned_allocator<u, alignment>;
|
||||
using other = aligned_allocator<U, Alignment>;
|
||||
};
|
||||
|
||||
aligned_allocator() noexcept = default;
|
||||
|
||||
template<typename u>
|
||||
aligned_allocator(const aligned_allocator<u, alignment>&) noexcept {}
|
||||
template<typename U>
|
||||
aligned_allocator(const aligned_allocator<U, Alignment>&) noexcept {}
|
||||
|
||||
auto allocate(size_type n) -> pointer {
|
||||
if (n == 0) return nullptr;
|
||||
|
||||
size_type size = n * sizeof(type);
|
||||
void* ptr = aligned_malloc(size, alignment);
|
||||
size_type size = n * sizeof(T);
|
||||
void* ptr = aligned_malloc(size, Alignment);
|
||||
|
||||
if (!ptr) {
|
||||
throw std::bad_alloc();
|
||||
@@ -99,44 +99,44 @@ public:
|
||||
aligned_free(p);
|
||||
}
|
||||
|
||||
template<typename u, typename... args>
|
||||
void construct(u* p, args&&... in_args) {
|
||||
::new(static_cast<void*>(p)) u(std::forward<args>(in_args)...);
|
||||
template<typename U, typename... Args>
|
||||
void construct(U* p, Args&&... args) {
|
||||
::new(static_cast<void*>(p)) U(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename u>
|
||||
void destroy(u* p) {
|
||||
template<typename U>
|
||||
void destroy(U* p) {
|
||||
p->~u();
|
||||
}
|
||||
|
||||
static auto max_size() noexcept {
|
||||
return std::numeric_limits<size_type>::max() / sizeof(type);
|
||||
return std::numeric_limits<size_type>::max() / sizeof(T);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename t1, size_t a1, typename t2, size_t a2>
|
||||
bool operator==(const aligned_allocator<t1, a1>&, const aligned_allocator<t2, a2>&) noexcept {
|
||||
return a1 == a2;
|
||||
template<typename T1, size_t A1, typename T2, size_t A2>
|
||||
bool operator==(const aligned_allocator<T1, A1>&, const aligned_allocator<T2, A2>&) noexcept {
|
||||
return A1 == A2;
|
||||
}
|
||||
|
||||
template<typename t1, size_t a1, typename t2, size_t a2>
|
||||
bool operator!=(const aligned_allocator<t1, a1>&, const aligned_allocator<t2, a2>&) noexcept {
|
||||
return a1 != a2;
|
||||
template<typename T1, size_t A1, typename T2, size_t A2>
|
||||
bool operator!=(const aligned_allocator<T1, A1>&, const aligned_allocator<T2, A2>&) noexcept {
|
||||
return A1 != A2;
|
||||
}
|
||||
|
||||
// 类型别名,方便使用不同对齐方式的分配器
|
||||
template<typename type>
|
||||
using sse_aligned_allocator = aligned_allocator<type, ALIGNMENT_SSE>;
|
||||
template<typename type>
|
||||
using avx_aligned_allocator = aligned_allocator<type, ALIGNMENT_AVX>;
|
||||
template<typename type>
|
||||
using avx512_aligned_allocator = aligned_allocator<type, ALIGNMENT_AVX512>;
|
||||
template<typename type>
|
||||
using cache_aligned_allocator = aligned_allocator<type, ALIGNMENT_CACHE>;
|
||||
template<typename T>
|
||||
using sse_aligned_allocator = aligned_allocator<T, ALIGNMENT_SSE>;
|
||||
template<typename T>
|
||||
using avx_aligned_allocator = aligned_allocator<T, ALIGNMENT_AVX>;
|
||||
template<typename T>
|
||||
using avx512_aligned_allocator = aligned_allocator<T, ALIGNMENT_AVX512>;
|
||||
template<typename T>
|
||||
using cache_aligned_allocator = aligned_allocator<T, ALIGNMENT_CACHE>;
|
||||
|
||||
template<size_t alignment>
|
||||
template<size_t Alignment>
|
||||
auto is_aligned(void* ptr) -> bool {
|
||||
return (reinterpret_cast<uintptr_t>(ptr) % alignment) == 0;
|
||||
return (reinterpret_cast<uintptr_t>(ptr) % Alignment) == 0;
|
||||
}
|
||||
|
||||
inline auto is_aligned(const void* ptr, size_t alignment) -> bool {
|
||||
@@ -147,14 +147,14 @@ inline auto align_size(size_t size, size_t alignment) -> size_t {
|
||||
return (size + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
template<size_t alignment>
|
||||
template<size_t Alignment>
|
||||
auto align_pointer(void* ptr) -> void* {
|
||||
const auto addr = reinterpret_cast<uintptr_t>(ptr);
|
||||
const auto aligned_addr = (addr + alignment - 1) & ~(alignment - 1);
|
||||
const auto aligned_addr = (addr + Alignment - 1) & ~(Alignment - 1);
|
||||
return reinterpret_cast<void*>(aligned_addr);
|
||||
}
|
||||
|
||||
template<typename type, size_t alignment>
|
||||
template<typename T, size_t Alignment>
|
||||
class aligned_buffer {
|
||||
public:
|
||||
aligned_buffer() = default;
|
||||
@@ -174,7 +174,7 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
data_ = static_cast<type*>(aligned_malloc(new_size * sizeof(type), alignment));
|
||||
data_ = static_cast<T*>(aligned_malloc(new_size * sizeof(T), Alignment));
|
||||
if (!data_) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
@@ -182,9 +182,9 @@ public:
|
||||
size_ = new_size;
|
||||
|
||||
// 对于非POD类型,需要构造对象
|
||||
if constexpr (!std::is_trivially_constructible_v<type>) {
|
||||
if constexpr (!std::is_trivially_constructible_v<T>) {
|
||||
for (size_t i = 0; i < size_; ++i) {
|
||||
new(&data_[i]) type();
|
||||
new(&data_[i]) T();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -193,7 +193,7 @@ public:
|
||||
if (!data_)
|
||||
return;
|
||||
// 对于非POD类型,需要析构对象
|
||||
if constexpr (!std::is_trivially_destructible_v<type>) {
|
||||
if constexpr (!std::is_trivially_destructible_v<T>) {
|
||||
for (size_t i = 0; i < size_; ++i) {
|
||||
data_[i].~T();
|
||||
}
|
||||
@@ -223,10 +223,10 @@ public:
|
||||
auto end() const noexcept { return data_ + size_; }
|
||||
|
||||
[[nodiscard]] auto is_properly_aligned() const noexcept -> bool {
|
||||
return is_aligned<alignment>(data_);
|
||||
return is_aligned<Alignment>(data_);
|
||||
}
|
||||
private:
|
||||
type* data_ = nullptr;
|
||||
T* data_ = nullptr;
|
||||
size_t size_ = 0;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user