CbmRoot
Loading...
Searching...
No Matches
PODAllocator.h
Go to the documentation of this file.
1/* Copyright (C) 2022 FIAS Frankfurt Institute for Advanced Studies, Frankfurt / Main
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Felix Weiglhofer [committer], Sebastian Heinemann */
4#ifndef CBM_ALGO_BASE_POD_ALLOCATOR_H
5#define CBM_ALGO_BASE_POD_ALLOCATOR_H
6
7#include <cstdlib>
8#include <type_traits>
9#include <utility>
10
11namespace cbm::algo
12{
18 template<class T>
20 public:
21 // Eventually we should enable this assert, but not all Digis have a trivial constructor yet.
22 // static_assert(std::is_trivially_constructible_v<T>, "PODAllocator only works for POD types");
23
24 // Ensure T has no vtable
25 static_assert(std::is_standard_layout_v<T>, "PODAllocator only works with types with standard layout");
26
27 using value_type = T;
28
29 T* allocate(size_t size) { return static_cast<T*>(std::malloc(size * sizeof(T))); }
30 void deallocate(T* p_t, size_t) { std::free(p_t); }
31
32 template<class... Args>
33 void construct([[maybe_unused]] T* obj, Args&&... args)
34 {
35 if constexpr (sizeof...(args) > 0) new (obj) T(std::forward<Args>(args)...);
36 }
37
38 // Required for std::move
39 bool operator==(const PODAllocator&) const { return true; }
40 bool operator!=(const PODAllocator&) const { return false; }
41 };
42} // namespace cbm::algo
43
44#endif // CBM_ALGO_BASE_POD_ALLOCATOR_H
static constexpr size_t size()
Definition KfSimdPseudo.h:2
Allocator for plain old data types.
void deallocate(T *p_t, size_t)
bool operator==(const PODAllocator &) const
bool operator!=(const PODAllocator &) const
void construct(T *obj, Args &&... args)
T * allocate(size_t size)