WPILibC++ 2023.4.3-108-ge5452e3
MemoryBuffer.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5//===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
6//
7// The LLVM Compiler Infrastructure
8//
9// This file is distributed under the University of Illinois Open Source
10// License. See LICENSE.TXT for details.
11//
12//===----------------------------------------------------------------------===//
13//
14// This file defines the MemoryBuffer interface.
15//
16//===----------------------------------------------------------------------===//
17
18#pragma once
19
20#include <stdint.h>
21
22#include <cstddef>
23#include <memory>
24#include <span>
25#include <string_view>
26#include <system_error>
27
28// Duplicated from fs.h to avoid a dependency
29namespace fs {
30#if defined(_WIN32)
31// A Win32 HANDLE is a typedef of void*
32using file_t = void*;
33#else
34using file_t = int;
35#endif
36} // namespace fs
37
38namespace wpi {
39
40class MemoryBufferRef;
41
42/// This interface provides simple read-only access to a block of memory, and
43/// provides simple methods for reading files and standard input into a memory
44/// buffer.
46 const uint8_t* m_bufferStart; // Start of the buffer.
47 const uint8_t* m_bufferEnd; // End of the buffer.
48
49 protected:
50 MemoryBuffer() = default;
51
52 void Init(const uint8_t* bufStart, const uint8_t* bufEnd);
53
54 public:
55 MemoryBuffer(const MemoryBuffer&) = delete;
57 virtual ~MemoryBuffer();
58
59 const uint8_t* begin() const { return m_bufferStart; }
60 const uint8_t* end() const { return m_bufferEnd; }
61 size_t size() const { return m_bufferEnd - m_bufferStart; }
62
63 std::span<const uint8_t> GetBuffer() const { return {begin(), end()}; }
64
65 /// Return an identifier for this buffer, typically the filename it was read
66 /// from.
68 return "Unknown buffer";
69 }
70
71 /// Open the specified file as a MemoryBuffer, returning a new MemoryBuffer
72 /// if successful, otherwise returning null. If FileSize is specified, this
73 /// means that the client knows that the file exists and that it has the
74 /// specified size.
75 static std::unique_ptr<MemoryBuffer> GetFile(std::string_view filename,
76 std::error_code& ec,
77 int64_t fileSize = -1);
78
79 /// Read all of the specified file into a MemoryBuffer as a stream
80 /// (i.e. until EOF reached). This is useful for special files that
81 /// look like a regular file but have 0 size (e.g. /proc/cpuinfo on Linux).
82 static std::unique_ptr<MemoryBuffer> GetFileAsStream(
83 std::string_view filename, std::error_code& ec);
84
85 /// Given an already-open file descriptor, map some slice of it into a
86 /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize.
87 static std::unique_ptr<MemoryBuffer> GetOpenFileSlice(
88 fs::file_t f, std::string_view filename, std::error_code& ec,
89 uint64_t mapSize, int64_t offset);
90
91 /// Given an already-open file descriptor, read the file and return a
92 /// MemoryBuffer.
93 static std::unique_ptr<MemoryBuffer> GetOpenFile(fs::file_t f,
94 std::string_view filename,
95 std::error_code& ec,
96 uint64_t fileSize);
97
98 /// Open the specified memory range as a MemoryBuffer.
99 static std::unique_ptr<MemoryBuffer> GetMemBuffer(
100 std::span<const uint8_t> inputData, std::string_view bufferName = "");
101
102 static std::unique_ptr<MemoryBuffer> GetMemBuffer(MemoryBufferRef ref);
103
104 /// Open the specified memory range as a MemoryBuffer, copying the contents
105 /// and taking ownership of it.
106 static std::unique_ptr<MemoryBuffer> GetMemBufferCopy(
107 std::span<const uint8_t> inputData, std::string_view bufferName = "");
108
109 /// Map a subrange of the specified file as a MemoryBuffer.
110 static std::unique_ptr<MemoryBuffer> GetFileSlice(std::string_view filename,
111 std::error_code& ec,
112 uint64_t mapSize,
113 uint64_t offset);
114
115 //===--------------------------------------------------------------------===//
116 // Provided for performance analysis.
117 //===--------------------------------------------------------------------===//
118
119 /// The kind of memory backing used to support the MemoryBuffer.
121
122 /// Return information on the memory mechanism used to support the
123 /// MemoryBuffer.
124 virtual BufferKind GetBufferKind() const = 0;
125
127};
128
129/// This class is an extension of MemoryBuffer, which allows copy-on-write
130/// access to the underlying contents. It only supports creation methods that
131/// are guaranteed to produce a writable buffer. For example, mapping a file
132/// read-only is not supported.
134 protected:
136
137 public:
139 using MemoryBuffer::end;
141 using MemoryBuffer::size;
142
143 // const_cast is well-defined here, because the underlying buffer is
144 // guaranteed to have been initialized with a mutable buffer.
145 uint8_t* begin() { return const_cast<uint8_t*>(MemoryBuffer::begin()); }
146 uint8_t* end() { return const_cast<uint8_t*>(MemoryBuffer::end()); }
147 std::span<uint8_t> GetBuffer() { return {begin(), end()}; }
148
149 static std::unique_ptr<WritableMemoryBuffer> GetFile(
150 std::string_view filename, std::error_code& ec, int64_t fileSize = -1);
151
152 /// Map a subrange of the specified file as a WritableMemoryBuffer.
153 static std::unique_ptr<WritableMemoryBuffer> GetFileSlice(
154 std::string_view filename, std::error_code& ec, uint64_t mapSize,
155 uint64_t offset);
156
157 /// Allocate a new MemoryBuffer of the specified size that is not initialized.
158 /// Note that the caller should initialize the memory allocated by this
159 /// method. The memory is owned by the MemoryBuffer object.
160 static std::unique_ptr<WritableMemoryBuffer> GetNewUninitMemBuffer(
161 size_t size, std::string_view bufferName = "");
162
163 /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note
164 /// that the caller need not initialize the memory allocated by this method.
165 /// The memory is owned by the MemoryBuffer object.
166 static std::unique_ptr<WritableMemoryBuffer> GetNewMemBuffer(
167 size_t size, std::string_view bufferName = "");
168
169 private:
170 // Hide these base class factory function so one can't write
171 // WritableMemoryBuffer::getXXX()
172 // and be surprised that they got a read-only Buffer.
178};
179
180/// This class is an extension of MemoryBuffer, which allows write access to
181/// the underlying contents and committing those changes to the original source.
182/// It only supports creation methods that are guaranteed to produce a writable
183/// buffer. For example, mapping a file read-only is not supported.
185 protected:
187
188 public:
190 using MemoryBuffer::end;
192 using MemoryBuffer::size;
193
194 // const_cast is well-defined here, because the underlying buffer is
195 // guaranteed to have been initialized with a mutable buffer.
196 uint8_t* begin() { return const_cast<uint8_t*>(MemoryBuffer::begin()); }
197 uint8_t* end() { return const_cast<uint8_t*>(MemoryBuffer::end()); }
198 std::span<uint8_t> GetBuffer() { return {begin(), end()}; }
199
200 static std::unique_ptr<WriteThroughMemoryBuffer> GetFile(
201 std::string_view filename, std::error_code& ec, int64_t fileSize = -1);
202
203 /// Map a subrange of the specified file as a ReadWriteMemoryBuffer.
204 static std::unique_ptr<WriteThroughMemoryBuffer> GetFileSlice(
205 std::string_view filename, std::error_code& ec, uint64_t mapSize,
206 uint64_t offset);
207
208 private:
209 // Hide these base class factory function so one can't write
210 // WritableMemoryBuffer::getXXX()
211 // and be surprised that they got a read-only Buffer.
217};
218
220 std::span<const uint8_t> m_buffer;
221 std::string_view m_id;
222
223 public:
224 MemoryBufferRef() = default;
226 : m_buffer(buffer.GetBuffer()), m_id(buffer.GetBufferIdentifier()) {}
227 MemoryBufferRef(std::span<const uint8_t> buffer, std::string_view id)
228 : m_buffer(buffer), m_id(id) {}
229
230 std::span<const uint8_t> GetBuffer() const { return m_buffer; }
231
232 std::string_view GetBufferIdentifier() const { return m_id; }
233
234 const uint8_t* begin() const { return &*m_buffer.begin(); }
235 const uint8_t* end() const { return &*m_buffer.end(); }
236 size_t size() const { return m_buffer.size(); }
237};
238
239} // namespace wpi
\rst A contiguous memory buffer with an optional growing ability.
Definition: core.h:862
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:45
static std::unique_ptr< MemoryBuffer > GetOpenFileSlice(fs::file_t f, std::string_view filename, std::error_code &ec, uint64_t mapSize, int64_t offset)
Given an already-open file descriptor, map some slice of it into a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > GetFile(std::string_view filename, std::error_code &ec, int64_t fileSize=-1)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
BufferKind
The kind of memory backing used to support the MemoryBuffer.
Definition: MemoryBuffer.h:120
@ MemoryBuffer_Malloc
Definition: MemoryBuffer.h:120
@ MemoryBuffer_MMap
Definition: MemoryBuffer.h:120
virtual std::string_view GetBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
Definition: MemoryBuffer.h:67
static std::unique_ptr< MemoryBuffer > GetOpenFile(fs::file_t f, std::string_view filename, std::error_code &ec, uint64_t fileSize)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > GetMemBuffer(std::span< const uint8_t > inputData, std::string_view bufferName="")
Open the specified memory range as a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > GetMemBufferCopy(std::span< const uint8_t > inputData, std::string_view bufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
const uint8_t * begin() const
Definition: MemoryBuffer.h:59
std::span< const uint8_t > GetBuffer() const
Definition: MemoryBuffer.h:63
virtual ~MemoryBuffer()
virtual BufferKind GetBufferKind() const =0
Return information on the memory mechanism used to support the MemoryBuffer.
static std::unique_ptr< MemoryBuffer > GetFileSlice(std::string_view filename, std::error_code &ec, uint64_t mapSize, uint64_t offset)
Map a subrange of the specified file as a MemoryBuffer.
static std::unique_ptr< MemoryBuffer > GetFileAsStream(std::string_view filename, std::error_code &ec)
Read all of the specified file into a MemoryBuffer as a stream (i.e.
MemoryBuffer(const MemoryBuffer &)=delete
MemoryBuffer()=default
MemoryBuffer & operator=(const MemoryBuffer &)=delete
const uint8_t * end() const
Definition: MemoryBuffer.h:60
void Init(const uint8_t *bufStart, const uint8_t *bufEnd)
MemoryBufferRef GetMemBufferRef() const
static std::unique_ptr< MemoryBuffer > GetMemBuffer(MemoryBufferRef ref)
size_t size() const
Definition: MemoryBuffer.h:61
Definition: MemoryBuffer.h:219
std::string_view GetBufferIdentifier() const
Definition: MemoryBuffer.h:232
MemoryBufferRef(MemoryBuffer &buffer)
Definition: MemoryBuffer.h:225
std::span< const uint8_t > GetBuffer() const
Definition: MemoryBuffer.h:230
MemoryBufferRef(std::span< const uint8_t > buffer, std::string_view id)
Definition: MemoryBuffer.h:227
const uint8_t * begin() const
Definition: MemoryBuffer.h:234
MemoryBufferRef()=default
size_t size() const
Definition: MemoryBuffer.h:236
const uint8_t * end() const
Definition: MemoryBuffer.h:235
This class is an extension of MemoryBuffer, which allows copy-on-write access to the underlying conte...
Definition: MemoryBuffer.h:133
static std::unique_ptr< WritableMemoryBuffer > GetFile(std::string_view filename, std::error_code &ec, int64_t fileSize=-1)
static std::unique_ptr< WritableMemoryBuffer > GetFileSlice(std::string_view filename, std::error_code &ec, uint64_t mapSize, uint64_t offset)
Map a subrange of the specified file as a WritableMemoryBuffer.
uint8_t * end()
Definition: MemoryBuffer.h:146
std::span< uint8_t > GetBuffer()
Definition: MemoryBuffer.h:147
uint8_t * begin()
Definition: MemoryBuffer.h:145
static std::unique_ptr< WritableMemoryBuffer > GetNewMemBuffer(size_t size, std::string_view bufferName="")
Allocate a new zero-initialized MemoryBuffer of the specified size.
static std::unique_ptr< WritableMemoryBuffer > GetNewUninitMemBuffer(size_t size, std::string_view bufferName="")
Allocate a new MemoryBuffer of the specified size that is not initialized.
size_t size() const
Definition: MemoryBuffer.h:61
This class is an extension of MemoryBuffer, which allows write access to the underlying contents and ...
Definition: MemoryBuffer.h:184
static std::unique_ptr< WriteThroughMemoryBuffer > GetFileSlice(std::string_view filename, std::error_code &ec, uint64_t mapSize, uint64_t offset)
Map a subrange of the specified file as a ReadWriteMemoryBuffer.
static std::unique_ptr< WriteThroughMemoryBuffer > GetFile(std::string_view filename, std::error_code &ec, int64_t fileSize=-1)
std::span< uint8_t > GetBuffer()
Definition: MemoryBuffer.h:198
uint8_t * begin()
Definition: MemoryBuffer.h:196
uint8_t * end()
Definition: MemoryBuffer.h:197
basic_string_view< char > string_view
Definition: core.h:520
::uint64_t uint64_t
Definition: Meta.h:58
::int64_t int64_t
Definition: Meta.h:59
::uint8_t uint8_t
Definition: Meta.h:52
Definition: MappedFileRegion.h:12
int file_t
Definition: MappedFileRegion.h:17
Definition: AprilTagFieldLayout.h:18