Open3D (C++ API)  0.15.1
StdAllocator.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2021 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26
27#pragma once
28
29#include "open3d/core/Device.h"
31
32namespace open3d {
33namespace core {
34
41template <typename T>
43public:
45 using value_type = T;
46
48 StdAllocator() = default;
49
51 explicit StdAllocator(const Device& device) : device_(device) {}
52
54 StdAllocator(const StdAllocator&) = default;
55
58
61
64
66 template <typename U>
67 StdAllocator(const StdAllocator<U>& other) : device_(other.device_) {}
68
71 return static_cast<T*>(MemoryManager::Malloc(n * sizeof(T), device_));
72 }
73
75 void deallocate(T* p, std::size_t n) { MemoryManager::Free(p, device_); }
76
78 bool operator==(const StdAllocator& other) const {
79 return device_ == other.device_;
80 }
81
83 bool operator!=(const StdAllocator& other) const {
84 return !operator==(other);
85 }
86
88 Device GetDevice() const { return device_; }
89
90private:
91 // Allow access in rebind constructor.
92 template <typename T2>
93 friend class StdAllocator;
94
95 Device device_;
96};
97
98} // namespace core
99} // namespace open3d
Definition: Device.h:39
static void * Malloc(size_t byte_size, const Device &device)
Definition: MemoryManager.cpp:41
static void Free(void *ptr, const Device &device)
Frees previously allocated memory at address ptr on device device.
Definition: MemoryManager.cpp:47
Definition: StdAllocator.h:42
void deallocate(T *p, std::size_t n)
Deallocates memory from pointer p of size n .
Definition: StdAllocator.h:75
StdAllocator & operator=(const StdAllocator &)=default
Default copy assignment operator.
T * allocate(std::size_t n)
Allocates memory of size n.
Definition: StdAllocator.h:70
StdAllocator(const StdAllocator &)=default
Default copy constructor.
StdAllocator & operator=(StdAllocator &&)=default
Default move assignment operator.
bool operator==(const StdAllocator &other) const
Returns true if the instances are equal, false otherwise.
Definition: StdAllocator.h:78
T value_type
T.
Definition: StdAllocator.h:45
bool operator!=(const StdAllocator &other) const
Returns true if the instances are not equal, false otherwise.
Definition: StdAllocator.h:83
Device GetDevice() const
Returns the device on which memory is allocated.
Definition: StdAllocator.h:88
StdAllocator(const StdAllocator< U > &other)
Rebind copy constructor.
Definition: StdAllocator.h:67
StdAllocator(const Device &device)
Constructor from device.
Definition: StdAllocator.h:51
StdAllocator()=default
Default constructor.
StdAllocator(StdAllocator &&)=default
Default move constructor.
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode value const const k4a_calibration_t calibration char size_t
Definition: K4aPlugin.cpp:738
Definition: PinholeCameraIntrinsic.cpp:35