2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
// author: Max Kellermann <max.kellermann@gmail.com>
|
2013-01-04 14:54:49 +01:00
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
#pragma once
|
2013-01-04 14:54:49 +01:00
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
#include "SpanCast.hxx"
|
2013-01-04 14:54:49 +01:00
|
|
|
|
2020-03-13 00:46:28 +01:00
|
|
|
#include <cstddef>
|
2022-05-20 10:17:20 +02:00
|
|
|
#include <span>
|
2016-06-17 18:01:13 +02:00
|
|
|
#include <utility>
|
2016-06-17 17:44:45 +02:00
|
|
|
|
2013-01-04 14:54:49 +01:00
|
|
|
#ifdef __linux__
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocate a huge amount of memory. This will be done in a way that
|
|
|
|
* allows giving the memory back to the kernel as soon as we don't
|
|
|
|
* need it anymore. On the downside, this call is expensive.
|
2017-09-19 19:45:52 +02:00
|
|
|
*
|
|
|
|
* Throws std::bad_alloc on error
|
2017-09-21 20:31:03 +02:00
|
|
|
*
|
|
|
|
* @returns the allocated buffer with a size which may be rounded up
|
|
|
|
* (to the next page size), so callers can take advantage of this
|
|
|
|
* allocation overhead
|
2013-01-04 14:54:49 +01:00
|
|
|
*/
|
2022-05-20 10:17:20 +02:00
|
|
|
std::span<std::byte>
|
2017-01-04 10:32:21 +01:00
|
|
|
HugeAllocate(size_t size);
|
2013-01-04 14:54:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param p an allocation returned by HugeAllocate()
|
|
|
|
* @param size the allocation's size as passed to HugeAllocate()
|
|
|
|
*/
|
|
|
|
void
|
2016-06-17 17:51:27 +02:00
|
|
|
HugeFree(void *p, size_t size) noexcept;
|
2013-01-04 14:54:49 +01:00
|
|
|
|
2022-04-26 20:18:07 +02:00
|
|
|
/**
|
|
|
|
* Set a name for the specified virtual memory area.
|
|
|
|
*
|
|
|
|
* This feature requires Linux 5.17.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
HugeSetName(void *p, size_t size, const char *name) noexcept;
|
|
|
|
|
2017-09-19 19:52:02 +02:00
|
|
|
/**
|
|
|
|
* Control whether this allocation is copied to newly forked child
|
|
|
|
* processes. Disabling that makes forking a little bit cheaper.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
HugeForkCow(void *p, size_t size, bool enable) noexcept;
|
|
|
|
|
2013-01-04 14:54:49 +01:00
|
|
|
/**
|
|
|
|
* Discard any data stored in the allocation and give the memory back
|
|
|
|
* to the kernel. After returning, the allocation still exists and
|
|
|
|
* can be reused at any time, but its contents are undefined.
|
|
|
|
*
|
|
|
|
* @param p an allocation returned by HugeAllocate()
|
|
|
|
* @param size the allocation's size as passed to HugeAllocate()
|
|
|
|
*/
|
|
|
|
void
|
2016-06-17 17:51:27 +02:00
|
|
|
HugeDiscard(void *p, size_t size) noexcept;
|
2013-01-04 14:54:49 +01:00
|
|
|
|
2017-12-12 10:22:20 +01:00
|
|
|
#elif defined(_WIN32)
|
2021-05-19 17:24:02 +02:00
|
|
|
#include <memoryapi.h>
|
2014-08-11 23:06:45 +02:00
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
std::span<std::byte>
|
2017-01-04 10:32:21 +01:00
|
|
|
HugeAllocate(size_t size);
|
2014-08-11 23:06:45 +02:00
|
|
|
|
|
|
|
static inline void
|
2019-08-15 17:57:20 +02:00
|
|
|
HugeFree(void *p, size_t) noexcept
|
2014-08-11 23:06:45 +02:00
|
|
|
{
|
|
|
|
VirtualFree(p, 0, MEM_RELEASE);
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:18:07 +02:00
|
|
|
static inline void
|
|
|
|
HugeSetName(void *, size_t, const char *) noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-19 19:52:02 +02:00
|
|
|
static inline void
|
|
|
|
HugeForkCow(void *, size_t, bool) noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-11 23:06:45 +02:00
|
|
|
static inline void
|
2016-06-17 17:51:27 +02:00
|
|
|
HugeDiscard(void *p, size_t size) noexcept
|
2014-08-11 23:06:45 +02:00
|
|
|
{
|
|
|
|
VirtualAlloc(p, size, MEM_RESET, PAGE_NOACCESS);
|
|
|
|
}
|
|
|
|
|
2013-01-04 14:54:49 +01:00
|
|
|
#else
|
|
|
|
|
|
|
|
/* not Linux: fall back to standard C calls */
|
|
|
|
|
2020-03-13 01:08:53 +01:00
|
|
|
#include <cstdint>
|
2013-01-04 14:54:49 +01:00
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
static inline std::span<std::byte>
|
2017-01-04 10:32:21 +01:00
|
|
|
HugeAllocate(size_t size)
|
2013-01-04 14:54:49 +01:00
|
|
|
{
|
2022-05-20 10:17:20 +02:00
|
|
|
return {new std::byte[size], size};
|
2013-01-04 14:54:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2016-06-17 17:51:27 +02:00
|
|
|
HugeFree(void *_p, size_t) noexcept
|
2013-01-04 14:54:49 +01:00
|
|
|
{
|
2022-05-20 10:17:20 +02:00
|
|
|
auto *p = (std::byte *)_p;
|
2016-06-17 17:44:45 +02:00
|
|
|
delete[] p;
|
2013-01-04 14:54:49 +01:00
|
|
|
}
|
|
|
|
|
2022-04-26 20:18:07 +02:00
|
|
|
static inline void
|
|
|
|
HugeSetName(void *, size_t, const char *) noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-19 19:52:02 +02:00
|
|
|
static inline void
|
|
|
|
HugeForkCow(void *, size_t, bool) noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-04 14:54:49 +01:00
|
|
|
static inline void
|
2016-06-17 17:51:27 +02:00
|
|
|
HugeDiscard(void *, size_t) noexcept
|
2013-01-04 14:54:49 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-09-21 20:19:49 +02:00
|
|
|
/**
|
|
|
|
* Automatic memory management for a dynamic array in "huge" memory.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class HugeArray {
|
2022-05-20 10:17:20 +02:00
|
|
|
using Buffer = std::span<T>;
|
2017-09-21 20:19:49 +02:00
|
|
|
Buffer buffer{nullptr};
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef typename Buffer::size_type size_type;
|
|
|
|
typedef typename Buffer::value_type value_type;
|
2020-01-12 14:39:54 +01:00
|
|
|
typedef typename Buffer::reference reference;
|
|
|
|
typedef typename Buffer::const_reference const_reference;
|
2017-09-21 20:19:49 +02:00
|
|
|
typedef typename Buffer::iterator iterator;
|
|
|
|
|
|
|
|
constexpr HugeArray() = default;
|
|
|
|
|
|
|
|
explicit HugeArray(size_type _size)
|
2022-05-20 10:17:20 +02:00
|
|
|
:buffer(FromBytesFloor<value_type>(HugeAllocate(sizeof(value_type) * _size))) {}
|
2017-09-21 20:19:49 +02:00
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
constexpr HugeArray(HugeArray &&other) noexcept
|
2017-09-21 20:19:49 +02:00
|
|
|
:buffer(std::exchange(other.buffer, nullptr)) {}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
~HugeArray() noexcept {
|
2022-05-20 10:17:20 +02:00
|
|
|
if (!buffer.empty()) {
|
|
|
|
auto v = std::as_writable_bytes(buffer);
|
|
|
|
HugeFree(v.data(), v.size());
|
2017-09-21 20:19:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
HugeArray &operator=(HugeArray &&other) noexcept {
|
2019-05-08 22:33:41 +02:00
|
|
|
using std::swap;
|
|
|
|
swap(buffer, other.buffer);
|
2017-09-21 20:19:49 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-26 20:18:07 +02:00
|
|
|
void SetName(const char *name) noexcept {
|
2022-05-20 10:17:20 +02:00
|
|
|
const auto v = std::as_writable_bytes(buffer);
|
|
|
|
HugeSetName(v.data(), v.size(), name);
|
2022-04-26 20:18:07 +02:00
|
|
|
}
|
|
|
|
|
2017-09-21 20:19:49 +02:00
|
|
|
void ForkCow(bool enable) noexcept {
|
2022-05-20 10:17:20 +02:00
|
|
|
const auto v = std::as_writable_bytes(buffer);
|
|
|
|
HugeForkCow(v.data(), v.size(), enable);
|
2017-09-21 20:19:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Discard() noexcept {
|
2022-05-20 10:17:20 +02:00
|
|
|
const auto v = std::as_writable_bytes(buffer);
|
|
|
|
HugeDiscard(v.data(), v.size());
|
2017-09-21 20:19:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
constexpr bool operator==(std::nullptr_t) const noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer == nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
constexpr bool operator!=(std::nullptr_t) const noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of allocated elements.
|
|
|
|
*/
|
2019-05-08 22:32:50 +02:00
|
|
|
constexpr size_type size() const noexcept {
|
2022-05-20 10:17:20 +02:00
|
|
|
return buffer.size();
|
2017-09-21 20:19:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
reference front() noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer.front();
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
const_reference front() const noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer.front();
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
reference back() noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer.back();
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
const_reference back() const noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns one element. No bounds checking.
|
|
|
|
*/
|
2019-05-08 22:32:50 +02:00
|
|
|
reference operator[](size_type i) noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns one constant element. No bounds checking.
|
|
|
|
*/
|
2019-05-08 22:32:50 +02:00
|
|
|
const_reference operator[](size_type i) const noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer[i];
|
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
iterator begin() noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer.begin();
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
constexpr auto begin() const noexcept {
|
|
|
|
return buffer.begin();
|
2017-09-21 20:19:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 22:32:50 +02:00
|
|
|
iterator end() noexcept {
|
2017-09-21 20:19:49 +02:00
|
|
|
return buffer.end();
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:17:20 +02:00
|
|
|
constexpr auto end() const noexcept {
|
|
|
|
return buffer.end();
|
2017-09-21 20:19:49 +02:00
|
|
|
}
|
|
|
|
};
|