Pigweed
 
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/simple_allocator.h"
25
26namespace pw::multibuf::test {
27
29template <size_t kDataSizeBytes = 1024, size_t kMetaSizeBytes = kDataSizeBytes>
31 public:
33 static constexpr size_t data_size_bytes() { return kDataSizeBytes; }
34
36 : SimpleAllocator(data_area_, meta_alloc_), meta_alloc_(alloc_) {}
37
39 MultiBuf BufWith(std::initializer_list<std::byte> data) {
40 std::optional<MultiBuf> buffer = Allocate(data.size());
41 PW_ASSERT(buffer.has_value());
42 std::copy(data.begin(), data.end(), buffer->begin());
43 return *std::move(buffer);
44 }
45
46 private:
47 std::byte data_area_[kDataSizeBytes];
50};
51
52} // namespace pw::multibuf::test
Definition: synchronized_allocator.h:35
An AllocatorForTest that is automatically initialized on construction.
Definition: testing.h:69
std::optional< MultiBuf > Allocate(size_t size)
Definition: multibuf.h:245
A simple first-fit MultiBufAllocator.
Definition: simple_allocator.h:59
Simple, self-contained pw::multibuf::MultiBufAllocator for test use.
Definition: simple_allocator_for_test.h:30
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:39
static constexpr size_t data_size_bytes()
Size of the data area.
Definition: simple_allocator_for_test.h:33