C/C++ API Reference
Loading...
Searching...
No Matches
entry.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17#include <cstdint>
18#include <limits>
19
20#include "pw_allocator/deallocator.h"
21#include "pw_allocator/internal/control_block.h"
22
23namespace pw::multibuf::internal {
24
37union Entry {
40 using size_type = uint16_t;
41
43 static constexpr size_t kMaxSize = ~(1U << 15);
44
46 static constexpr size_type kMemoryContextIndex = 0;
47
49 static constexpr size_type kDataIndex = 1;
50
52 static constexpr size_type kBaseViewIndex = 2;
53
55 static constexpr size_type kMinEntriesPerChunk = 3;
56
59 size_type entries_per_chunk) {
60 return chunk * entries_per_chunk + kMemoryContextIndex;
61 }
62
64 static constexpr size_type data_index(size_type chunk,
65 size_type entries_per_chunk) {
66 return chunk * entries_per_chunk + kDataIndex;
67 }
68
70 static constexpr size_type base_view_index(size_type chunk,
71 size_type entries_per_chunk) {
72 return chunk * entries_per_chunk + kBaseViewIndex;
73 }
74
76 static constexpr size_type view_index(size_type chunk,
77 size_type entries_per_chunk,
78 size_type layer) {
79 return chunk * entries_per_chunk + kBaseViewIndex + layer - 1;
80 }
81
83 static constexpr size_type top_view_index(size_type chunk,
84 size_type entries_per_chunk) {
85 return (chunk + 1) * entries_per_chunk - 1;
86 }
87
90
92 allocator::internal::ControlBlock* control_block;
93
95 std::byte* data;
96
98 struct BaseView {
101
105
108
112 } base_view;
113
116 struct View {
119
125
128
133 } view;
134};
135
136} // namespace pw::multibuf::internal
Abstract interface for releasing memory.
Definition: deallocator.h:29
Describes the entire memory region.
Definition: entry.h:98
size_type shared
Definition: entry.h:111
size_type owned
Definition: entry.h:104
size_type length
Amount of data from the buffer to present.
Definition: entry.h:107
size_type offset
Starting offset within the buffer of the data to present.
Definition: entry.h:100
Definition: entry.h:116
size_type length
Amount of data from the buffer to present.
Definition: entry.h:127
size_type boundary
Definition: entry.h:132
size_type offset
Starting offset within the buffer of the data to present.
Definition: entry.h:118
size_type sealed
Definition: entry.h:124
Definition: entry.h:37
static constexpr size_type kMinEntriesPerChunk
Minimum number of entries per chunk.
Definition: entry.h:55
allocator::internal::ControlBlock * control_block
Optional control block involved in freeing shared memory.
Definition: entry.h:92
static constexpr size_type base_view_index(size_type chunk, size_type entries_per_chunk)
Returns the index to the base view entry of a given chunk.
Definition: entry.h:70
static constexpr size_type view_index(size_type chunk, size_type entries_per_chunk, size_type layer)
Returns the index to a view entry of a given chunk.
Definition: entry.h:76
static constexpr size_type memory_context_index(size_type chunk, size_type entries_per_chunk)
Returns the index to the data entry of a given chunk.
Definition: entry.h:58
static constexpr size_type data_index(size_type chunk, size_type entries_per_chunk)
Returns the index to the data entry of a given chunk.
Definition: entry.h:64
static constexpr size_t kMaxSize
Offset and length must fit in 15 bits.
Definition: entry.h:43
static constexpr size_type kBaseViewIndex
Per-chunk index entry that holds the base view of the data.
Definition: entry.h:52
std::byte * data
Pointer to memory.
Definition: entry.h:95
static constexpr size_type top_view_index(size_type chunk, size_type entries_per_chunk)
Returns the index to the top view entry of a given chunk.
Definition: entry.h:83
uint16_t size_type
Definition: entry.h:40
Deallocator * deallocator
Optional deallocator involved in freeing owned memory.
Definition: entry.h:89
static constexpr size_type kDataIndex
Per-chunk index entry that holds the data pointer.
Definition: entry.h:49
static constexpr size_type kMemoryContextIndex
Per-chunk index entry that holds the deallocator or control block.
Definition: entry.h:46