Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
simple_allocator_for_test.h
1// Copyright 2024 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 <algorithm>
17#include <cstddef>
18#include <initializer_list>
19#include <optional>
20
21#include "pw_allocator/synchronized_allocator.h"
22#include "pw_allocator/testing.h"
23#include "pw_assert/assert.h"
24#include "pw_multibuf/config.h"
25#include "pw_multibuf/simple_allocator.h"
26
27namespace pw::multibuf::test {
28
30template <size_t kDataSizeBytes = 1024, size_t kMetaSizeBytes = kDataSizeBytes>
31class PW_MULTIBUF_DEPRECATED SimpleAllocatorForTest : public SimpleAllocator {
32 public:
34 static constexpr size_t data_size_bytes() { return kDataSizeBytes; }
35
37 : SimpleAllocator(data_area_, meta_alloc_), meta_alloc_(alloc_) {}
38
40 MultiBuf BufWith(std::initializer_list<std::byte> data) {
41 std::optional<MultiBuf> buffer = Allocate(data.size());
42 PW_ASSERT(buffer.has_value());
43 std::copy(data.begin(), data.end(), buffer->begin());
44 return *std::move(buffer);
45 }
46
47 private:
48 std::byte data_area_[kDataSizeBytes];
51};
52
53} // namespace pw::multibuf::test
Definition: synchronized_allocator.h:35
An AllocatorForTest that is automatically initialized on construction.
Definition: testing.h:69
Definition: multibuf_v1.h:246
A simple first-fit MultiBufAllocator.
Definition: simple_allocator.h:60
Simple, self-contained pw::multibuf::MultiBufAllocator for test use.
Definition: simple_allocator_for_test.h:31
MultiBuf BufWith(std::initializer_list< std::byte > data)
Allocates a MultiBuf and initializes its contents to the provided data.
Definition: simple_allocator_for_test.h:40
static constexpr size_t data_size_bytes()
Size of the data area.
Definition: simple_allocator_for_test.h:34