Skip to content

Commit

Permalink
pw_allocator: Parameterize BlockAllocator on blocks
Browse files Browse the repository at this point in the history
This CL switches the template parameter type for BlockAllocator from
an offset type and poison interval to just a block type. The poison
interval is now controlled by the module configuration. A type alias for
the legacy type is provided.

Change-Id: Id0add6254caf56dfb9a65b7f0bc9ed44249c72a4
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/234812
Lint: Lint 🤖 <[email protected]>
Pigweed-Auto-Submit: Aaron Green <[email protected]>
Commit-Queue: Aaron Green <[email protected]>
Reviewed-by: Taylor Cramer <[email protected]>
  • Loading branch information
nopsledder authored and CQ Bot Account committed Nov 18, 2024
1 parent 8cc8495 commit f84afe1
Show file tree
Hide file tree
Showing 52 changed files with 483 additions and 391 deletions.
19 changes: 16 additions & 3 deletions pw_allocator/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ label_flag(

cc_library(
name = "test_config",
defines = ["PW_ALLOCATOR_STRICT_VALIDATION=1"],
defines = [
"PW_ALLOCATOR_STRICT_VALIDATION=1",
"PW_ALLOCATOR_BLOCK_POISON_INTERVAL=4",
],
)

# Libraries
Expand Down Expand Up @@ -80,6 +83,7 @@ cc_library(
deps = [
":block_allocator",
":config",
"//pw_allocator/block:detailed_block",
],
)

Expand All @@ -91,7 +95,11 @@ cc_library(
deps = [
":allocator",
":fragmentation",
"//pw_allocator/block:detailed_block",
"//pw_allocator/block:allocatable",
"//pw_allocator/block:basic",
"//pw_allocator/block:iterable",
"//pw_allocator/block:poisonable",
"//pw_allocator/block:with_layout",
"//pw_assert",
"//pw_bytes:alignment",
"//pw_result",
Expand Down Expand Up @@ -125,6 +133,7 @@ cc_library(
deps = [
":block_allocator",
":bucket",
"//pw_allocator/block:detailed_block",
"//pw_status",
],
)
Expand Down Expand Up @@ -241,6 +250,7 @@ cc_library(
deps = [
":block_allocator",
":config",
"//pw_allocator/block:detailed_block",
],
)

Expand Down Expand Up @@ -269,6 +279,7 @@ cc_library(
deps = [
":block_allocator",
":config",
"//pw_allocator/block:detailed_block",
],
)

Expand All @@ -287,7 +298,6 @@ cc_library(
strip_include_prefix = "public",
deps = [
":bucket_allocator",
"//pw_allocator/block:detailed_block",
"//pw_assert",
"//pw_bytes",
"//pw_preprocessor",
Expand All @@ -301,6 +311,7 @@ cc_library(
deps = [
":block_allocator",
":config",
"//pw_allocator/block:detailed_block",
],
)

Expand Down Expand Up @@ -402,6 +413,7 @@ cc_library(
deps = [
":block_allocator",
":config",
"//pw_allocator/block:detailed_block",
],
)

Expand Down Expand Up @@ -530,6 +542,7 @@ pw_cc_test(
deps = [
":block_allocator_testing",
":bucket_allocator",
":bucket_block_allocator",
"//pw_unit_test",
],
)
Expand Down
20 changes: 17 additions & 3 deletions pw_allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ pw_source_set("config") {
}

config("test_config") {
defines = [ "PW_ALLOCATOR_STRICT_VALIDATION=1" ]
defines = [
"PW_ALLOCATOR_STRICT_VALIDATION=1",
"PW_ALLOCATOR_BLOCK_POISON_INTERVAL=4",
]
}

# Libraries
Expand Down Expand Up @@ -82,6 +85,7 @@ pw_source_set("best_fit_block_allocator") {
public_deps = [
":block_allocator",
":config",
"block:detailed_block",
]
}

Expand All @@ -92,7 +96,11 @@ pw_source_set("block_allocator") {
":allocator",
":bucket",
":fragmentation",
"block:detailed_block",
"block:allocatable",
"block:basic",
"block:iterable",
"block:poisonable",
"block:with_layout",
dir_pw_bytes,
dir_pw_result,
dir_pw_status,
Expand All @@ -119,6 +127,7 @@ pw_source_set("bucket_allocator") {
public_deps = [
":block_allocator",
":bucket",
"block:detailed_block",
dir_pw_status,
]
}
Expand Down Expand Up @@ -214,6 +223,7 @@ pw_source_set("dual_first_fit_block_allocator") {
public_deps = [
":block_allocator",
":config",
"block:detailed_block",
]
}

Expand All @@ -236,6 +246,7 @@ pw_source_set("first_fit_block_allocator") {
public_deps = [
":block_allocator",
":config",
"block:detailed_block",
]
}

Expand All @@ -250,7 +261,6 @@ pw_source_set("freelist_heap") {
public = [ "public/pw_allocator/freelist_heap.h" ]
public_deps = [
":bucket_allocator",
"block:detailed_block",
dir_pw_assert,
dir_pw_bytes,
dir_pw_preprocessor,
Expand All @@ -263,6 +273,7 @@ pw_source_set("last_fit_block_allocator") {
public_deps = [
":block_allocator",
":config",
"block:detailed_block",
]
}

Expand Down Expand Up @@ -340,6 +351,7 @@ pw_source_set("worst_fit_block_allocator") {
public_deps = [
":block_allocator",
":config",
"block:detailed_block",
]
}

Expand Down Expand Up @@ -373,6 +385,7 @@ pw_source_set("block_allocator_testing") {
deps = [
"$dir_pw_bytes:alignment",
"$dir_pw_third_party/fuchsia:stdcompat",
"block:detailed_block",
dir_pw_assert,
dir_pw_status,
]
Expand Down Expand Up @@ -432,6 +445,7 @@ pw_test("bucket_allocator_test") {
deps = [
":block_allocator_testing",
":bucket_allocator",
":bucket_block_allocator",
]
sources = [ "bucket_allocator_test.cc" ]
}
Expand Down
16 changes: 14 additions & 2 deletions pw_allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pw_add_library(pw_allocator.config INTERFACE
pw_add_library(pw_allocator.test_config INTERFACE
PUBLIC_DEFINES
PW_ALLOCATOR_STRICT_VALIDATION=1
PW_ALLOCATOR_BLOCK_POISON_INTERVAL=4
)

# Libraries
Expand Down Expand Up @@ -67,6 +68,7 @@ pw_add_library(pw_allocator.best_fit_block_allocator INTERFACE
public
PUBLIC_DEPS
pw_allocator.block_allocator
pw_allocator.block.detailed_block
pw_allocator.config
)

Expand All @@ -84,7 +86,11 @@ pw_add_library(pw_allocator.block_allocator STATIC
public
PUBLIC_DEPS
pw_allocator.allocator
pw_allocator.block.detailed_block
pw_allocator.block.allocatable
pw_allocator.block.basic
pw_allocator.block.iterable
pw_allocator.block.poisonable
pw_allocator.block.with_layout
pw_allocator.fragmentation
pw_bytes.alignment
pw_result
Expand Down Expand Up @@ -117,6 +123,7 @@ pw_add_library(pw_allocator.bucket_allocator INTERFACE
public
PUBLIC_DEPS
pw_allocator.block_allocator
pw_allocator.block.detailed_block
pw_allocator.bucket
pw_status
)
Expand Down Expand Up @@ -214,6 +221,7 @@ pw_add_library(pw_allocator.dual_first_fit_block_allocator INTERFACE
public
PUBLIC_DEPS
pw_allocator.block_allocator
pw_allocator.block.detailed_block
pw_allocator.config
)

Expand All @@ -240,6 +248,7 @@ pw_add_library(pw_allocator.first_fit_block_allocator INTERFACE
public
PUBLIC_DEPS
pw_allocator.block_allocator
pw_allocator.block.detailed_block
pw_allocator.config
)

Expand All @@ -258,7 +267,6 @@ pw_add_library(pw_allocator.freelist_heap INTERFACE
PUBLIC_INCLUDES
public
PUBLIC_DEPS
pw_allocator.block.detailed_block
pw_allocator.bucket_allocator
pw_assert
pw_bytes
Expand All @@ -272,6 +280,7 @@ pw_add_library(pw_allocator.last_fit_block_allocator INTERFACE
public
PUBLIC_DEPS
pw_allocator.block_allocator
pw_allocator.block.detailed_block
pw_allocator.config
)

Expand Down Expand Up @@ -360,6 +369,7 @@ pw_add_library(pw_allocator.worst_fit_block_allocator INTERFACE
public
PUBLIC_DEPS
pw_allocator.block_allocator
pw_allocator.block.detailed_block
pw_allocator.config
)

Expand Down Expand Up @@ -394,6 +404,7 @@ pw_add_library(pw_allocator.block_allocator_testing STATIC
PUBLIC_DEPS
pw_allocator.block.testing
pw_allocator.block_allocator
pw_allocator.block.detailed_block
pw_unit_test
PRIVATE_DEPS
pw_assert
Expand Down Expand Up @@ -473,6 +484,7 @@ pw_add_test(pw_allocator.bucket_allocator_test
PRIVATE_DEPS
pw_allocator.block_allocator_testing
pw_allocator.bucket_allocator
pw_allocator.bucket_block_allocator
GROUPS
modules
pw_allocator
Expand Down
2 changes: 1 addition & 1 deletion pw_allocator/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ uses the mix-ins above.

Bucket
======
.. doxygenclass:: pw::allocator::internal::Bucket
.. doxygenclass:: pw::allocator::Bucket
:members:

.. _module-pw_allocator-api-metrics_adapter:
Expand Down
2 changes: 2 additions & 0 deletions pw_allocator/best_fit_block_allocator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,6 @@ TEST_F(BestFitBlockAllocatorTest, CanMeasureFragmentation) {
CanMeasureFragmentation();
}

TEST_F(BestFitBlockAllocatorTest, PoisonPeriodically) { PoisonPeriodically(); }

} // namespace
1 change: 1 addition & 0 deletions pw_allocator/block/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pw_add_library(pw_allocator.block.allocatable INTERFACE
PUBLIC_INCLUDES
public
PUBLIC_DEPS
pw_allocator.config
pw_allocator.block.contiguous
pw_allocator.block.result
pw_allocator.deallocator
Expand Down
4 changes: 2 additions & 2 deletions pw_allocator/block/public/pw_allocator/block/contiguous.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ContiguousBlock : public internal::ContiguousBase {
inline Derived* Next() const;

protected:
/// Split a block into two smaller blocks and allocates the leading one.
/// Split a block into two smaller blocks.
///
/// This method splits a block into a leading block of the given
/// `new_inner_size` and a trailing block, and returns the trailing space as a
Expand All @@ -97,7 +97,7 @@ class ContiguousBlock : public internal::ContiguousBase {
/// @pre The space remaining after a split can hold a new block.
Derived* DoSplitFirst(size_t new_inner_size);

/// Split a block into two smaller blocks and allocates the trailing one.
/// Split a block into two smaller blocks.
///
/// This method splits a block into a leading block and a trailing block of
/// the given `new_inner_size`, and returns the trailing space is returned as
Expand Down
Loading

0 comments on commit f84afe1

Please sign in to comment.