forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPUCommonAlignedAlloc.h
More file actions
61 lines (52 loc) · 2.09 KB
/
Copy pathGPUCommonAlignedAlloc.h
File metadata and controls
61 lines (52 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file GPUCommonAlignedAlloc.h
/// \author David Rohr
#ifndef GPUCOMMONAKUGBEDALLOC_H
#define GPUCOMMONAKUGBEDALLOC_H
#include <memory>
namespace o2::gpu
{
template <typename T, std::size_t MIN_ALIGN = 0>
struct alignedDeleter {
void operator()(void* ptr) { ::operator delete(ptr, std::align_val_t(std::max(MIN_ALIGN, alignof(T)))); };
};
template <typename T, std::size_t MIN_ALIGN = 0>
struct alignedAllocator {
using value_type = T;
T* allocate(std::size_t n)
{
return (T*)::operator new(n, std::align_val_t(std::max(MIN_ALIGN, alignof(T))));
}
void deallocate(T* ptr, std::size_t)
{
alignedDeleter<T, MIN_ALIGN>()(ptr);
}
};
template <typename T>
struct aligned_unique_buffer_ptr : public std::unique_ptr<char[], alignedDeleter<T>> {
aligned_unique_buffer_ptr() = default;
aligned_unique_buffer_ptr(size_t n) { alloc(n); }
aligned_unique_buffer_ptr(T* ptr) { std::unique_ptr<char[], alignedDeleter<T>>::reset((char*)ptr); }
char* getraw() { return std::unique_ptr<char[], alignedDeleter<T>>::get(); }
const char* getraw() const { return std::unique_ptr<char[], alignedDeleter<T>>::get(); }
T* get() { return (T*)std::unique_ptr<char[], alignedDeleter<T>>::get(); }
const T* get() const { return (T*)std::unique_ptr<char[], alignedDeleter<T>>::get(); }
T* operator->() { return get(); }
const T* operator->() const { return get(); }
T* alloc(std::size_t n)
{
std::unique_ptr<char[], alignedDeleter<T>>::reset((char*)alignedAllocator<T>().allocate(n));
return get();
}
};
} // namespace o2::gpu
#endif // GPUCOMMONAKUGBEDALLOC_H