pw_span: Switch from pw::span to std::span
Replace pw::span with std::span and "pw_span/span.h" with <span>
throughout the codebase.
Change-Id: Ib1fa873168b6093794e861611d750fcad6285d6c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/12801
Commit-Queue: Wyatt Hepler <hepler@google.com>
Reviewed-by: Rob Mohr <mohrr@google.com>
Reviewed-by: Keir Mierle <keir@google.com>
diff --git a/pw_allocator/block.cc b/pw_allocator/block.cc
index 8f93ff4..43fb209 100644
--- a/pw_allocator/block.cc
+++ b/pw_allocator/block.cc
@@ -16,7 +16,7 @@
namespace pw::allocator {
-Status Block::Init(const span<std::byte> region, Block** block) {
+Status Block::Init(const std::span<std::byte> region, Block** block) {
// Ensure the region we're given is aligned and sized accordingly
if (reinterpret_cast<uintptr_t>(region.data()) % alignof(Block) != 0) {
return Status::INVALID_ARGUMENT;
diff --git a/pw_allocator/block_test.cc b/pw_allocator/block_test.cc
index 7979bdc..9fa5678 100644
--- a/pw_allocator/block_test.cc
+++ b/pw_allocator/block_test.cc
@@ -14,8 +14,9 @@
#include "pw_allocator/block.h"
+#include <span>
+
#include "gtest/gtest.h"
-#include "pw_span/span.h"
using std::byte;
@@ -26,7 +27,7 @@
alignas(Block*) byte bytes[kN];
Block* block = nullptr;
- auto status = Block::Init(span(bytes, kN), &block);
+ auto status = Block::Init(std::span(bytes, kN), &block);
ASSERT_EQ(status, Status::OK);
EXPECT_EQ(block->OuterSize(), kN);
@@ -45,7 +46,7 @@
byte* byte_ptr = bytes;
Block* block = nullptr;
- auto status = Block::Init(span(byte_ptr + 1, kN - 1), &block);
+ auto status = Block::Init(std::span(byte_ptr + 1, kN - 1), &block);
EXPECT_EQ(status, Status::INVALID_ARGUMENT);
}
@@ -54,7 +55,7 @@
constexpr size_t kN = 2;
alignas(Block*) byte bytes[kN];
Block* block = nullptr;
- auto status = Block::Init(span(bytes, kN), &block);
+ auto status = Block::Init(std::span(bytes, kN), &block);
EXPECT_EQ(status, Status::INVALID_ARGUMENT);
}
@@ -65,7 +66,7 @@
alignas(Block*) byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
auto status = block->Split(kSplitN, &next_block);
@@ -96,7 +97,7 @@
uintptr_t split_len = split_addr - (uintptr_t)&bytes;
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
auto status = block->Split(kSplitN, &next_block);
@@ -126,7 +127,7 @@
byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* block2 = nullptr;
block->Split(kSplit1, &block2);
@@ -146,7 +147,7 @@
byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
auto status = block->Split(kSplitN, &next_block);
@@ -161,7 +162,7 @@
byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
auto status = block->Split(kSplitN, nullptr);
EXPECT_EQ(status, Status::INVALID_ARGUMENT);
@@ -173,7 +174,7 @@
byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
auto status = block->Split(block->InnerSize() + 1, &next_block);
@@ -187,7 +188,7 @@
byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
auto status = block->Split(0, &next_block);
@@ -202,7 +203,7 @@
byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
auto status = block->Split(block->InnerSize() - sizeof(Block), &next_block);
@@ -216,7 +217,7 @@
alignas(Block*) byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
block->MarkUsed();
EXPECT_EQ(block->Used(), true);
@@ -234,7 +235,7 @@
alignas(Block*) byte bytes[kN];
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
block->MarkUsed();
@@ -252,7 +253,7 @@
byte bytes[kN];
Block* block = nullptr;
- Block::Init(pw::span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* block2 = nullptr;
block->Split(kSplit1, &block2);
@@ -278,7 +279,7 @@
// Do a split, just to sanity check that the checks on Next/Prev are
// different...
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
block->Split(512, &next_block);
@@ -294,7 +295,7 @@
// Do a split, just to sanity check that the checks on Next/Prev are
// different...
Block* block = nullptr;
- Block::Init(span(bytes, kN), &block);
+ Block::Init(std::span(bytes, kN), &block);
Block* next_block = nullptr;
block->Split(512, &next_block);
diff --git a/pw_allocator/freelist.cc b/pw_allocator/freelist.cc
index 34bcbb7..4629a4f 100644
--- a/pw_allocator/freelist.cc
+++ b/pw_allocator/freelist.cc
@@ -16,7 +16,7 @@
namespace pw::allocator {
-Status FreeList::AddChunk(span<std::byte> chunk) {
+Status FreeList::AddChunk(std::span<std::byte> chunk) {
// Check that the size is enough to actually store what we need
if (chunk.size() < sizeof(FreeListNode)) {
return Status::OUT_OF_RANGE;
@@ -39,9 +39,9 @@
return Status::OK;
}
-span<std::byte> FreeList::FindChunk(size_t size) const {
+std::span<std::byte> FreeList::FindChunk(size_t size) const {
if (size == 0) {
- return span<std::byte>();
+ return std::span<std::byte>();
}
size_t chunk_ptr = FindChunkPtrForSize(size, true);
@@ -49,7 +49,7 @@
// Check that there's data. This catches the case where we run off the
// end of the array
if (chunks_[chunk_ptr] == nullptr) {
- return span<std::byte>();
+ return std::span<std::byte>();
}
// Now iterate up the buckets, walking each list to find a good candidate
@@ -62,7 +62,7 @@
while (aliased.node != nullptr) {
if (aliased.node->size >= size) {
- return span<std::byte>(aliased.data, aliased.node->size);
+ return std::span<std::byte>(aliased.data, aliased.node->size);
}
aliased.node = aliased.node->next;
@@ -71,10 +71,10 @@
// If we get here, we've checked every block in every bucket. There's
// nothing that can support this allocation.
- return span<std::byte>();
+ return std::span<std::byte>();
}
-Status FreeList::RemoveChunk(span<std::byte> chunk) {
+Status FreeList::RemoveChunk(std::span<std::byte> chunk) {
size_t chunk_ptr = FindChunkPtrForSize(chunk.size(), true);
// Walk that list, finding the chunk.
diff --git a/pw_allocator/freelist_heap.cc b/pw_allocator/freelist_heap.cc
index 7966bb8..dfaba56 100644
--- a/pw_allocator/freelist_heap.cc
+++ b/pw_allocator/freelist_heap.cc
@@ -18,7 +18,7 @@
namespace pw::allocator {
-FreeListHeap::FreeListHeap(span<std::byte> region, FreeList& freelist)
+FreeListHeap::FreeListHeap(std::span<std::byte> region, FreeList& freelist)
: freelist_(freelist) {
Block* block;
Block::Init(region, &block);
diff --git a/pw_allocator/freelist_heap_test.cc b/pw_allocator/freelist_heap_test.cc
index 5356c43..a28b31d 100644
--- a/pw_allocator/freelist_heap_test.cc
+++ b/pw_allocator/freelist_heap_test.cc
@@ -14,8 +14,9 @@
#include "pw_allocator/freelist_heap.h"
+#include <span>
+
#include "gtest/gtest.h"
-#include "pw_span/span.h"
namespace pw::allocator {
diff --git a/pw_allocator/freelist_test.cc b/pw_allocator/freelist_test.cc
index 9a1c5a3..0484a7a 100644
--- a/pw_allocator/freelist_test.cc
+++ b/pw_allocator/freelist_test.cc
@@ -14,8 +14,9 @@
#include "pw_allocator/freelist.h"
+#include <span>
+
#include "gtest/gtest.h"
-#include "pw_span/span.h"
#include "pw_status/status.h"
using std::byte;
@@ -41,7 +42,7 @@
byte data[kN] = {std::byte(0)};
- auto status = list.AddChunk(pw::span(data, kN));
+ auto status = list.AddChunk(std::span(data, kN));
EXPECT_EQ(status, Status::OK);
auto item = list.FindChunk(kN);
@@ -55,7 +56,7 @@
byte data[kN] = {std::byte(0)};
- list.AddChunk(pw::span(data, kN));
+ list.AddChunk(std::span(data, kN));
auto item = list.FindChunk(kN / 2);
EXPECT_EQ(item.size(), kN);
EXPECT_EQ(item.data(), data);
@@ -67,8 +68,8 @@
byte data[kN] = {std::byte(0)};
- list.AddChunk(pw::span(data, kN));
- auto status = list.RemoveChunk(pw::span(data, kN));
+ list.AddChunk(std::span(data, kN));
+ auto status = list.RemoveChunk(std::span(data, kN));
EXPECT_EQ(status, Status::OK);
auto item = list.FindChunk(kN);
@@ -83,8 +84,8 @@
byte data1[kN1] = {std::byte(0)};
byte data2[kN2] = {std::byte(0)};
- list.AddChunk(pw::span(data1, kN1));
- list.AddChunk(pw::span(data2, kN2));
+ list.AddChunk(std::span(data1, kN1));
+ list.AddChunk(std::span(data2, kN2));
auto chunk = list.FindChunk(kN1 / 2);
EXPECT_EQ(chunk.size(), kN1);
@@ -110,8 +111,8 @@
byte data2[kN2] = {std::byte(0)};
// List should now be 257 -> 512 -> NULL
- list.AddChunk(pw::span(data1, kN1));
- list.AddChunk(pw::span(data2, kN2));
+ list.AddChunk(std::span(data1, kN1));
+ list.AddChunk(std::span(data2, kN2));
auto chunk = list.FindChunk(kN2 + 1);
EXPECT_EQ(chunk.size(), kN1);
@@ -130,8 +131,8 @@
// List should now be:
// bkt[3] (257 bytes up to 512 bytes) -> 257 -> NULL
// bkt[4] (513 bytes up to 1024 bytes) -> 513 -> NULL
- list.AddChunk(pw::span(data1, kN1));
- list.AddChunk(pw::span(data2, kN2));
+ list.AddChunk(std::span(data1, kN1));
+ list.AddChunk(std::span(data2, kN2));
// Request a 300 byte chunk. This should return the 513 byte one
auto chunk = list.FindChunk(kN1 + 1);
@@ -145,8 +146,8 @@
byte data[kN] = {std::byte(0)};
byte data2[kN] = {std::byte(0)};
- list.AddChunk(pw::span(data, kN));
- auto status = list.RemoveChunk(pw::span(data2, kN));
+ list.AddChunk(std::span(data, kN));
+ auto status = list.RemoveChunk(std::span(data2, kN));
EXPECT_EQ(status, Status::NOT_FOUND);
}
@@ -157,8 +158,8 @@
byte data1[kN] = {std::byte(0)};
byte data2[kN] = {std::byte(0)};
- list.AddChunk(pw::span(data1, kN));
- list.AddChunk(pw::span(data2, kN));
+ list.AddChunk(std::span(data1, kN));
+ list.AddChunk(std::span(data2, kN));
auto chunk1 = list.FindChunk(kN);
list.RemoveChunk(chunk1);
diff --git a/pw_allocator/public/pw_allocator/block.h b/pw_allocator/public/pw_allocator/block.h
index 2fa4052..e12349a 100644
--- a/pw_allocator/public/pw_allocator/block.h
+++ b/pw_allocator/public/pw_allocator/block.h
@@ -16,7 +16,8 @@
// usable.
#pragma once
-#include "pw_span/span.h"
+#include <span>
+
#include "pw_status/status.h"
namespace pw::allocator {
@@ -80,7 +81,7 @@
// Returns:
// INVALID_ARGUMENT if the given region is unaligned to too small, or
// OK otherwise.
- static Status Init(const span<std::byte> region, Block** block);
+ static Status Init(const std::span<std::byte> region, Block** block);
// Returns a pointer to a Block, given a pointer to the start of the usable
// space inside the block (i.e. the opposite operation to UsableSpace()). In
diff --git a/pw_allocator/public/pw_allocator/freelist.h b/pw_allocator/public/pw_allocator/freelist.h
index ac4444a..207edba 100644
--- a/pw_allocator/public/pw_allocator/freelist.h
+++ b/pw_allocator/public/pw_allocator/freelist.h
@@ -14,9 +14,9 @@
#pragma once
#include <array>
+#include <span>
#include "pw_containers/vector.h"
-#include "pw_span/span.h"
#include "pw_status/status.h"
namespace pw::allocator {
@@ -64,19 +64,19 @@
// OK: The chunk was added successfully
// OUT_OF_RANGE: The chunk could not be added for size reasons (e.g. if
// the chunk is too small to store the FreeListNode).
- Status AddChunk(span<std::byte> chunk);
+ Status AddChunk(std::span<std::byte> chunk);
// Finds an eligible chunk for an allocation of size `size`. Note that this
// will return the first allocation possible within a given bucket, it does
- // not currently optimize for finding the smallest chunk. Returns a pw::span
+ // not currently optimize for finding the smallest chunk. Returns a std::span
// representing the chunk. This will be "valid" on success, and will have size
// = 0 on failure (if there were no chunks available for that allocation).
- span<std::byte> FindChunk(size_t size) const;
+ std::span<std::byte> FindChunk(size_t size) const;
// Remove a chunk from this freelist. Returns:
// OK: The chunk was removed successfully
// NOT_FOUND: The chunk could not be found in this freelist.
- Status RemoveChunk(span<std::byte> chunk);
+ Status RemoveChunk(std::span<std::byte> chunk);
private:
// For a given size, find which index into chunks_ the node should be written
diff --git a/pw_allocator/public/pw_allocator/freelist_heap.h b/pw_allocator/public/pw_allocator/freelist_heap.h
index c3c015e..cf44ffd 100644
--- a/pw_allocator/public/pw_allocator/freelist_heap.h
+++ b/pw_allocator/public/pw_allocator/freelist_heap.h
@@ -15,16 +15,16 @@
#pragma once
#include <cstddef>
+#include <span>
#include "pw_allocator/block.h"
#include "pw_allocator/freelist.h"
-#include "pw_span/span.h"
namespace pw::allocator {
class FreeListHeap {
public:
- FreeListHeap(span<std::byte> region, FreeList& freelist);
+ FreeListHeap(std::span<std::byte> region, FreeList& freelist);
void* Allocate(size_t size);
void Free(void* ptr);
@@ -32,11 +32,11 @@
void* Calloc(size_t num, size_t size);
private:
- span<std::byte> BlockToSpan(Block* block) {
- return span<std::byte>(block->UsableSpace(), block->InnerSize());
+ std::span<std::byte> BlockToSpan(Block* block) {
+ return std::span<std::byte>(block->UsableSpace(), block->InnerSize());
}
- span<std::byte> region_;
+ std::span<std::byte> region_;
FreeList& freelist_;
};
@@ -46,7 +46,7 @@
static constexpr std::array<size_t, N> defaultBuckets{
16, 32, 64, 128, 256, 512};
- FreeListHeapBuffer(span<std::byte> region)
+ FreeListHeapBuffer(std::span<std::byte> region)
: freelist_(defaultBuckets), heap_(region, freelist_) {}
void* Allocate(size_t size) { return heap_.Allocate(size); }