ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
StdAllocator.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
12 
13 namespace cloudViewer {
14 namespace core {
15 
22 template <typename T>
23 class StdAllocator {
24 public:
26  using value_type = T;
27 
29  StdAllocator() = default;
30 
32  explicit StdAllocator(int device_id) : device_id_(device_id) {}
33 
35  StdAllocator(const StdAllocator&) = default;
36 
38  StdAllocator& operator=(const StdAllocator&) = default;
39 
41  StdAllocator(StdAllocator&&) = default;
42 
45 
47  template <typename U>
48  StdAllocator(const StdAllocator<U>& other) : device_id_(other.device_id_) {}
49 
51  T* allocate(std::size_t n) {
52  void* ptr = MemoryManager::Malloc(
53  n * sizeof(T), Device(Device::DeviceType::CUDA, device_id_));
54  return static_cast<T*>(ptr);
55  }
56 
58  void deallocate(T* p, std::size_t n) {
60  }
61 
63  bool operator==(const StdAllocator& other) const {
64  return device_id_ == other.device_id_;
65  }
66 
68  bool operator!=(const StdAllocator& other) const {
69  return !operator==(other);
70  }
71 
73  int GetDeviceID() const { return device_id_; }
74 
75 private:
76  // Allow access in rebind constructor.
77  template <typename T2>
78  friend class StdAllocator;
79 
80  int device_id_;
81 };
82 
83 } // namespace core
84 } // namespace cloudViewer
static void * Malloc(size_t byte_size, const Device &device)
static void Free(void *ptr, const Device &device)
Frees previously allocated memory at address ptr on device device.
StdAllocator(StdAllocator &&)=default
Default move constructor.
StdAllocator(const StdAllocator< U > &other)
Rebind copy constructor.
Definition: StdAllocator.h:48
StdAllocator()=default
Default constructor.
void deallocate(T *p, std::size_t n)
Deallocates memory from pointer p of size n .
Definition: StdAllocator.h:58
T * allocate(std::size_t n)
Allocates memory of size n.
Definition: StdAllocator.h:51
StdAllocator(const StdAllocator &)=default
Default copy constructor.
StdAllocator & operator=(const StdAllocator &)=default
Default copy assignment operator.
bool operator!=(const StdAllocator &other) const
Returns true if the instances are not equal, false otherwise.
Definition: StdAllocator.h:68
int GetDeviceID() const
Returns the device on which memory is allocated.
Definition: StdAllocator.h:73
StdAllocator(int device_id)
Constructor from device.
Definition: StdAllocator.h:32
bool operator==(const StdAllocator &other) const
Returns true if the instances are equal, false otherwise.
Definition: StdAllocator.h:63
StdAllocator & operator=(StdAllocator &&)=default
Default move assignment operator.
Generic file read and write utility for python interface.