Pixie
Loading...
Searching...
No Matches
aligned.h
1#pragma once
2
3#include <pixie/storage.h>
4#include <pixie/storage/read_only_view.h>
5
6#include <array>
7#include <cstddef>
8#include <cstdint>
9#include <span>
10#include <vector>
11
12namespace pixie {
13
14inline constexpr std::size_t kAlignedStorageLineBytes = 64;
15inline constexpr std::size_t kAlignedStorageLineBits =
16 kAlignedStorageLineBytes * 8;
17inline constexpr std::size_t kAlignedStorageLineWords64 =
18 kAlignedStorageLineBytes / sizeof(std::uint64_t);
19inline constexpr std::size_t kAlignedStorageLineWords16 =
20 kAlignedStorageLineBytes / sizeof(std::uint16_t);
21
23struct alignas(kAlignedStorageLineBytes) CacheLine {
24 std::array<std::byte, kAlignedStorageLineBytes> data{};
25};
26
27static_assert(alignof(CacheLine) == kAlignedStorageLineBytes);
28static_assert(sizeof(CacheLine) == kAlignedStorageLineBytes);
29
37class AlignedStorage : public StorageBase<AlignedStorage> {
38 public:
39 AlignedStorage() = default;
40
42 explicit AlignedStorage(std::size_t size_bits)
43 : data_(lines_for_bits(size_bits)) {}
44
46 std::size_t size_bytes_impl() const {
47 return data_.size() * kAlignedStorageLineBytes;
48 }
49
51 std::span<const std::byte> as_bytes_impl() const {
52 return std::as_bytes(std::span<const CacheLine>(data_));
53 }
54
56 ReadOnlyStorageView view_impl(std::size_t offset_bytes,
57 std::size_t count_bytes) const {
58 if (offset_bytes > size_bytes_impl() ||
59 count_bytes > size_bytes_impl() - offset_bytes) {
60 throw std::out_of_range("Storage view is outside the allocation");
61 }
63 as_bytes_impl().subspan(offset_bytes, count_bytes));
64 }
65
67 void resize_impl(std::size_t size_bits) {
68 data_.resize(lines_for_bits(size_bits));
69 }
70
72 std::span<std::byte> writable_bytes_impl() {
73 return std::as_writable_bytes(std::span<CacheLine>(data_));
74 }
75
77 std::span<std::uint16_t> writable_words16_impl() {
78 return {reinterpret_cast<std::uint16_t*>(data_.data()),
79 data_.size() * kAlignedStorageLineWords16};
80 }
81
83 std::span<std::uint64_t> writable_words64_impl() {
84 return {reinterpret_cast<std::uint64_t*>(data_.data()),
85 data_.size() * kAlignedStorageLineWords64};
86 }
87
89 std::size_t allocated_bytes_impl() const {
90 return data_.capacity() * kAlignedStorageLineBytes;
91 }
92
94 void shrink_to_fit_impl() { data_.shrink_to_fit(); }
95
97 std::span<CacheLine> as_lines() { return data_; }
98
100 std::span<const CacheLine> as_lines() const { return data_; }
101
102 private:
103 static constexpr std::size_t lines_for_bits(std::size_t size_bits) {
104 return size_bits / kAlignedStorageLineBits +
105 (size_bits % kAlignedStorageLineBits != 0);
106 }
107
108 std::vector<CacheLine> data_;
109};
110
111} // namespace pixie
std::span< CacheLine > as_lines()
Return mutable cache-line blocks.
Definition aligned.h:97
void shrink_to_fit_impl()
Request release of unused vector capacity.
Definition aligned.h:94
AlignedStorage(std::size_t size_bits)
Construct storage for at least size_bits bits.
Definition aligned.h:42
std::span< const CacheLine > as_lines() const
Return read-only cache-line blocks.
Definition aligned.h:100
std::size_t size_bytes_impl() const
Return the padded allocation size in bytes.
Definition aligned.h:46
void resize_impl(std::size_t size_bits)
Resize to hold at least size_bits bits.
Definition aligned.h:67
std::size_t allocated_bytes_impl() const
Return bytes reserved by the underlying vector.
Definition aligned.h:89
std::span< const std::byte > as_bytes_impl() const
Return the padded allocation as read-only bytes.
Definition aligned.h:51
std::span< std::byte > writable_bytes_impl()
Return writable allocation bytes.
Definition aligned.h:72
ReadOnlyStorageView view_impl(std::size_t offset_bytes, std::size_t count_bytes) const
Return a checked read-only byte subrange.
Definition aligned.h:56
std::span< std::uint64_t > writable_words64_impl()
Return writable allocation as 64-bit words.
Definition aligned.h:83
std::span< std::uint16_t > writable_words16_impl()
Return writable allocation as 16-bit words.
Definition aligned.h:77
A non-owning, read-only view of a byte sequence.
Definition read_only_view.h:17
CRTP facade for byte-addressable storage.
Definition storage.h:27
std::size_t size_bits() const
Definition storage.h:33
Common interface for byte-addressable storage.
A 64-byte aligned storage block.
Definition aligned.h:23