Open3D (C++ API)
Blob.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 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 <cstddef>
30 #include <iostream>
31 #include <string>
32 
33 #include "Open3D/Core/Device.h"
35 
36 namespace open3d {
37 
55 class Blob {
56 public:
61  Blob(int64_t byte_size, const Device& device)
62  : deleter_(nullptr),
63  data_ptr_(MemoryManager::Malloc(byte_size, device)),
64  device_(device) {}
65 
73  Blob(const Device& device,
74  void* data_ptr,
75  const std::function<void(void*)>& deleter)
76  : deleter_(deleter), data_ptr_(data_ptr), device_(device) {}
77 
78  ~Blob() {
79  if (deleter_) {
80  // Our custom deleter's void* argument is not used. The deleter
81  // function itself shall handle destruction without the argument.
82  // The void(void*) signature is kept to be consistent with DLPack's
83  // deleter.
84  deleter_(nullptr);
85  } else {
87  }
88  };
89 
90  Device GetDevice() const { return device_; }
91 
92  void* GetDataPtr() { return data_ptr_; }
93 
94  const void* GetDataPtr() const { return data_ptr_; }
95 
96 protected:
98  std::function<void(void*)> deleter_ = nullptr;
99 
101  void* data_ptr_ = nullptr;
102 
105 };
106 
107 } // namespace open3d
Definition: Blob.h:55
Blob(const Device &device, void *data_ptr, const std::function< void(void *)> &deleter)
Definition: Blob.h:73
Device device_
Device context for the blob.
Definition: Blob.h:104
void * data_ptr_
Device data pointer.
Definition: Blob.h:101
Definition: MemoryManager.h:41
const void * GetDataPtr() const
Definition: Blob.h:94
Blob(int64_t byte_size, const Device &device)
Definition: Blob.h:61
~Blob()
Definition: Blob.h:78
Definition: Open3DViewer.h:29
void * GetDataPtr()
Definition: Blob.h:92
std::function< void(void *)> deleter_
For externally managed memory, deleter != nullptr.
Definition: Blob.h:98
static void Free(void *ptr, const Device &device)
Definition: MemoryManager.cpp:43
Device GetDevice() const
Definition: Blob.h:90
Definition: Device.h:38