Skip to content

Commit

Permalink
pw_allocator: Use move-semantics for Block API
Browse files Browse the repository at this point in the history
Several Block methods consume a block pointer and return a replacement.
Previously, this was accomplished by having static methods that took an
l-value reference to a Block pointer that was modified in place.

It was noted in pwrev/235312 that this API is potentially confusing, and
recommended consuming the pointers as rvalue references and returning
new pointers. This makes it clearer when a pointer is being consumed,
and allows the compiler to catch bugs like using a moved-from pointer.

Change-Id: I7b8d51da58b7a7195e43d840871b31d5f965d84c
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/242633
Docs-Not-Needed: Aaron Green <aarongreen@google.com>
Commit-Queue: Aaron Green <aarongreen@google.com>
Reviewed-by: Taylor Cramer <cramertj@google.com>
Pigweed-Auto-Submit: Aaron Green <aarongreen@google.com>
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
  • Loading branch information
nopsledder authored and CQ Bot Account committed Nov 18, 2024
1 parent 6417a52 commit f43ca4f
Show file tree
Hide file tree
Showing 30 changed files with 916 additions and 508 deletions.
1 change: 1 addition & 0 deletions pw_allocator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ cc_library(
],
)

# TODO(b/376730645): Remove deprecated interfaces.
cc_library(
name = "bucket_block_allocator",
hdrs = ["public/pw_allocator/bucket_block_allocator.h"],
Expand Down
1 change: 1 addition & 0 deletions pw_allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pw_source_set("bucket_allocator") {
]
}

# TODO(b/376730645): Remove deprecated interfaces.
pw_source_set("bucket_block_allocator") {
public_configs = [ ":public_include_path" ]
public = [ "public/pw_allocator/bucket_block_allocator.h" ]
Expand Down
1 change: 1 addition & 0 deletions pw_allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pw_add_library(pw_allocator.bucket_allocator INTERFACE
pw_status
)

# TODO(b/376730645): Remove deprecated interfaces.
pw_add_library(pw_allocator.bucket_block_allocator INTERFACE
HEADERS
public/pw_allocator/bucket_block_allocator.h
Expand Down
27 changes: 23 additions & 4 deletions pw_allocator/block/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ package(default_visibility = ["//visibility:public"])

licenses(["notice"])

cc_library(
name = "result",
hdrs = ["public/pw_allocator/block/result.h"],
includes = ["public"],
deps = [
"//pw_allocator:config",
"//pw_assert",
"//pw_status",
],
)

cc_library(
name = "alignable",
hdrs = ["public/pw_allocator/block/alignable.h"],
Expand All @@ -34,13 +45,11 @@ cc_library(

cc_library(
name = "allocatable",
hdrs = [
"public/pw_allocator/block/allocatable.h",
"public/pw_allocator/block/result.h",
],
hdrs = ["public/pw_allocator/block/allocatable.h"],
includes = ["public"],
deps = [
":contiguous",
":result",
"//pw_allocator:deallocator",
"//pw_assert",
"//pw_bytes:alignment",
Expand Down Expand Up @@ -143,6 +152,16 @@ cc_library(
],
)

pw_cc_test(
name = "result_test",
srcs = ["result_test.cc"],
deps = [
":basic",
":result",
"//pw_unit_test",
],
)

pw_cc_test(
name = "detailed_block_test",
srcs = ["detailed_block_test.cc"],
Expand Down
29 changes: 24 additions & 5 deletions pw_allocator/block/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ config("public_include_path") {
visibility = [ ":*" ]
}

pw_source_set("result") {
public_configs = [ ":public_include_path" ]
public = [ "public/pw_allocator/block/result.h" ]
public_deps = [
"$dir_pw_allocator:config",
"$dir_pw_assert",
"$dir_pw_status",
]
}

pw_source_set("alignable") {
public_configs = [ ":public_include_path" ]
public = [ "public/pw_allocator/block/alignable.h" ]
Expand All @@ -37,12 +47,10 @@ pw_source_set("alignable") {

pw_source_set("allocatable") {
public_configs = [ ":public_include_path" ]
public = [
"public/pw_allocator/block/allocatable.h",
"public/pw_allocator/block/result.h",
]
public = [ "public/pw_allocator/block/allocatable.h" ]
public_deps = [
":contiguous",
":result",
"$dir_pw_allocator:deallocator",
"$dir_pw_assert",
"$dir_pw_bytes:alignment",
Expand Down Expand Up @@ -134,6 +142,14 @@ pw_source_set("testing") {
]
}

pw_test("result_test") {
deps = [
":basic",
":result",
]
sources = [ "result_test.cc" ]
}

pw_test("detailed_block_test") {
deps = [
":testing",
Expand All @@ -144,5 +160,8 @@ pw_test("detailed_block_test") {
}

pw_test_group("tests") {
tests = [ ":detailed_block_test" ]
tests = [
":detailed_block_test",
":result_test",
]
}
24 changes: 23 additions & 1 deletion pw_allocator/block/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@

include($ENV{PW_ROOT}/pw_build/pigweed.cmake)

pw_add_library(pw_allocator.block.result INTERFACE
HEADERS
public/pw_allocator/block/result.h
PUBLIC_INCLUDES
public
PUBLIC_DEPS
pw_allocator.config
pw_assert
pw_status
)

pw_add_library(pw_allocator.block.alignable INTERFACE
HEADERS
public/pw_allocator/block/alignable.h
Expand All @@ -31,11 +42,11 @@ pw_add_library(pw_allocator.block.alignable INTERFACE
pw_add_library(pw_allocator.block.allocatable INTERFACE
HEADERS
public/pw_allocator/block/allocatable.h
public/pw_allocator/block/result.h
PUBLIC_INCLUDES
public
PUBLIC_DEPS
pw_allocator.block.contiguous
pw_allocator.block.result
pw_allocator.deallocator
pw_assert
pw_bytes.alignment
Expand Down Expand Up @@ -140,6 +151,17 @@ pw_add_library(pw_allocator.block.testing INTERFACE
pw_third_party.fuchsia.stdcompat
)

pw_add_test(pw_allocator.block.result_test
SOURCES
result_test.cc
PRIVATE_DEPS
pw_allocator.block.basic
pw_allocator.block.result
GROUPS
modules
pw_allocator
)

pw_add_test(pw_allocator.block.detailed_block_test
SOURCES
detailed_block_test.cc
Expand Down
Loading

0 comments on commit f43ca4f

Please sign in to comment.