ReUseX  0.0.5
3D Point Cloud Processing for Building Reuse
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Povl Filip Sonne-Frederiksen
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5#pragma once
6#include <functional>
7#include <initializer_list>
8#include <memory>
9#include <string>
10#include <vector>
11
13
15 public:
16 BaseMemory() = default;
17 BaseMemory(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes);
18 virtual ~BaseMemory();
19
20 virtual void *gpu_realloc(size_t bytes);
21 virtual void *cpu_realloc(size_t bytes);
22
25 void release();
26
27 // Status query
28 inline size_t cpu_bytes() const { return cpu_bytes_; }
29 inline size_t gpu_bytes() const { return gpu_bytes_; }
30
31 // Get raw pointer (for computation)
32 virtual inline void *get_gpu() const { return gpu_; }
33 virtual inline void *get_cpu() const { return cpu_; }
34
35 // Reference external memory (does not own)
36 void reference(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes);
37
38 // [New]: Share memory of another Memory object (shared ownership)
39 void set_shared_memory(const BaseMemory &other);
40
41 protected:
42 // Raw pointer kept for fast access, but lifetime managed by shared_ptr
43 void *cpu_ = nullptr;
44 size_t cpu_bytes_ = 0;
45 size_t cpu_capacity_ = 0;
46
47 void *gpu_ = nullptr;
48 size_t gpu_bytes_ = 0;
49 size_t gpu_capacity_ = 0;
50
51 // Smart pointer manages lifetime
52 std::shared_ptr<void> cpu_ptr_ = nullptr;
53 std::shared_ptr<void> gpu_ptr_ = nullptr;
54};
55
56template <typename _DT> class Memory : public BaseMemory {
57 public:
58 Memory() = default;
59 Memory(const Memory &other) { this->set_shared_memory(other); }
60 Memory &operator=(const Memory &other) {
61 if (this != &other)
62 this->set_shared_memory(other);
63 return *this;
64 }
65
66 virtual _DT *gpu(size_t size) {
67 return (_DT *)BaseMemory::gpu_realloc(size * sizeof(_DT));
68 }
69 virtual _DT *cpu(size_t size) {
70 return (_DT *)BaseMemory::cpu_realloc(size * sizeof(_DT));
71 }
72
73 inline size_t cpu_size() const { return cpu_bytes_ / sizeof(_DT); }
74 inline size_t gpu_size() const { return gpu_bytes_ / sizeof(_DT); }
75
76 virtual inline _DT *gpu() const { return (_DT *)gpu_; }
77 virtual inline _DT *cpu() const { return (_DT *)cpu_; }
78};
79
80} // namespace reusex::vision::tensor_rt::tensor
void set_shared_memory(const BaseMemory &other)
virtual void * cpu_realloc(size_t bytes)
void reference(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes)
BaseMemory(void *cpu, size_t cpu_bytes, void *gpu, size_t gpu_bytes)
virtual void * gpu_realloc(size_t bytes)
virtual _DT * cpu(size_t size)
Definition memory.hpp:69
virtual _DT * gpu(size_t size)
Definition memory.hpp:66
Memory & operator=(const Memory &other)
Definition memory.hpp:60