mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
memory.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <mutable/mutable-config.hpp>
4#include <mutable/util/fn.hpp>
5#include <cstdint>
6#include <iostream>
7#include <memory>
8
9
10namespace m {
11
12namespace memory {
13
14struct Memory;
15
17struct M_EXPORT Allocator
18{
19 friend struct Memory;
20
21 private:
22 int fd_;
23
24 public:
25 Allocator();
26 virtual ~Allocator();
27
29 int fd() const { return fd_; }
30
32 virtual Memory allocate(std::size_t size) = 0;
33
34 protected:
36 virtual void deallocate(Memory &&mem) = 0;
37
39 Memory create_memory(void *addr, std::size_t size, std::size_t offset);
40};
41
44struct M_EXPORT AddressSpace
45{
46 friend void swap(AddressSpace &first, AddressSpace &second) {
47 using std::swap;
48 swap(first.addr_, second.addr_);
49 swap(first.size_, second.size_);
50 }
51
52 private:
53 void *addr_;
54 std::size_t size_;
55
56 private:
57 AddressSpace() : addr_(nullptr), size_(0) { }
58 public:
59 AddressSpace(std::size_t size);
61 AddressSpace(const AddressSpace&) = delete;
62 AddressSpace(AddressSpace &&other) : AddressSpace() { swap(*this, other); }
63
65 void * addr() const { return addr_; }
67 std::size_t size() const { return size_; }
68
70 template<typename T>
71 T as() const { return reinterpret_cast<T>(addr()); }
72};
73
77struct M_EXPORT Memory
78{
79 friend struct Allocator;
80
81 private:
82 Allocator *allocator_ = nullptr;
83 void *addr_ = nullptr;
84 std::size_t size_ = 0;
85 std::size_t offset_ = 0;
86
87 Memory(Allocator &allocator, void *addr, std::size_t size, std::size_t offset);
88
89 public:
90 friend void swap(Memory &first, Memory &second) {
91 using std::swap;
92 swap(first.allocator_, second.allocator_);
93 swap(first.addr_, second.addr_);
94 swap(first.size_, second.size_);
95 swap(first.offset_, second.offset_);
96 }
97
98 Memory() { }
99 Memory(void *addr, std::size_t size) : addr_(addr), size_(size) { }
100 ~Memory() { if (allocator_) allocator().deallocate(std::move(*this)); }
101 Memory(const Memory&) = delete;
102 Memory(Memory &&other) { swap(*this, other); }
103
104 Memory & operator=(Memory &&other) { swap(*this, other); return *this; }
105
107 Allocator & allocator() const { M_insist(allocator_); return *allocator_; }
109 void * addr() const { return addr_; }
111 std::size_t size() const { return size_; }
113 std::size_t offset() const { return offset_; }
114
117 template<typename T>
118 T as() { return reinterpret_cast<T>(addr()); }
121 template<typename T>
122 const T as() const { return reinterpret_cast<T>(addr()); }
123
125 void map(std::size_t size, std::size_t offset_src, const AddressSpace &vm, std::size_t offset_dst) const;
126
127 void dump(std::ostream &out) const;
128 void dump() const;
129};
130
138struct M_EXPORT LinearAllocator : Allocator
139{
140 private:
141 std::size_t offset_ = 0;
142
144 std::vector<std::size_t> allocations_;
145
146 public:
149
150 Memory allocate(std::size_t size) override;
151
153 std::size_t offset() const { return offset_; }
154
155 private:
156 void deallocate(Memory &&mem) override;
157};
158
159}
160
161}
#define M_insist(...)
Definition: macro.hpp:129
‍mutable namespace
Definition: Backend.hpp:10
void swap(PlanTableBase< Actual > &first, PlanTableBase< Actual > &second)
Definition: PlanTable.hpp:394
T(x)
void deallocate(void *ptr, size_type size)
Deallocate the allocation at ptr of size size.
This class represents a reserved address space in virtual memory.
Definition: memory.hpp:45
void * addr_
pointer to the beginning of the virtual address space
Definition: memory.hpp:53
AddressSpace(const AddressSpace &)=delete
std::size_t size_
size in bytes of the address space
Definition: memory.hpp:54
T as() const
Get a pointer to the beginning of the virtual address space, converted to type T.
Definition: memory.hpp:71
void * addr() const
Returns a pointer to the beginning of the virtual address space.
Definition: memory.hpp:65
std::size_t size() const
Returns the size in bytes of the virtual address space.
Definition: memory.hpp:67
AddressSpace(AddressSpace &&other)
Definition: memory.hpp:62
friend void swap(AddressSpace &first, AddressSpace &second)
Definition: memory.hpp:46
This is the common interface for all memory allocators that support rewiring.
Definition: memory.hpp:18
int fd_
file descriptor of the underlying memory file
Definition: memory.hpp:22
virtual void deallocate(Memory &&mem)=0
Deallocates a memory object.
virtual Memory allocate(std::size_t size)=0
Creates a new memory object with size bytes of freshly allocated memory.
int fd() const
Return the file descriptor of the underlying memory file.
Definition: memory.hpp:29
This is the simplest kind of allocator.
Definition: memory.hpp:139
std::vector< std::size_t > allocations_
‍stack of allocations; allocations can be marked deallocated for later reclaiming
Definition: memory.hpp:144
std::size_t offset() const
Returns the offset in the underlying memory file where the next allocation is placed.
Definition: memory.hpp:153
Represents a mapping created by a memory::Allocator.
Definition: memory.hpp:78
void * addr_
pointer to the virtual address space where this allocation is mapped to
Definition: memory.hpp:83
friend void swap(Memory &first, Memory &second)
Definition: memory.hpp:90
T as()
Returns a pointer to the beginning of the virtual address space where this allocation is mapped to,...
Definition: memory.hpp:118
void * addr() const
Returns a pointer to the beginning of the virtual address space where this allocation is mapped to.
Definition: memory.hpp:109
Memory(void *addr, std::size_t size)
Definition: memory.hpp:99
Memory(const Memory &)=delete
std::size_t size_
the size of this allocation
Definition: memory.hpp:84
Memory(Memory &&other)
Definition: memory.hpp:102
Memory & operator=(Memory &&other)
Definition: memory.hpp:104
std::size_t offset() const
Returns the offset in bytes of this allocation within its allocator.
Definition: memory.hpp:113
Allocator * allocator_
the allocator that created this memory allocation
Definition: memory.hpp:82
Allocator & allocator() const
Returns a reference to the allocator that created this allocation.
Definition: memory.hpp:107
std::size_t size() const
Returns the size in bytes of this allocation.
Definition: memory.hpp:111
std::size_t offset_
the offset of this allocation within its allocator
Definition: memory.hpp:85
const T as() const
Returns a pointer to the beginning of the virtual address space where this allocation is mapped to,...
Definition: memory.hpp:122