-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This CL adds NullAllocator, a memory allocator which always fails. This can be used as part of a larger memory framework to disallow memory allocations under specific circumstances. Change-Id: I2b005660f611afc975f98097b88f1fe0c4b1af27 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/172233 Reviewed-by: Taylor Cramer <[email protected]> Commit-Queue: Aaron Green <[email protected]>
- Loading branch information
1 parent
b9656ec
commit 5715233
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2023 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
#include "pw_allocator/null_allocator.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace pw::allocator { | ||
|
||
TEST(NullAllocatorTest, Allocate) { | ||
NullAllocator allocator; | ||
// Allocate should fail, regardless of size and alignment. | ||
for (size_t size = 1; size < 0x100; size <<= 1) { | ||
for (size_t alignment = 1; alignment < 0x100; alignment <<= 1) { | ||
EXPECT_EQ(allocator.AllocateUnchecked(size, alignment), nullptr); | ||
} | ||
} | ||
} | ||
|
||
TEST(NullAllocatorTest, Resize) { | ||
NullAllocator allocator; | ||
// It is not possible to get a valid pointer from Allocate. | ||
constexpr Layout layout = Layout::Of<uint8_t>(); | ||
EXPECT_FALSE(allocator.Resize(this, layout, 1)); | ||
} | ||
|
||
} // namespace pw::allocator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2023 The Pigweed Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
#pragma once | ||
|
||
#include "pw_allocator/allocator.h" | ||
|
||
namespace pw::allocator { | ||
|
||
/// A memory allocator that always fails to allocate memory. | ||
/// | ||
/// A null allocator may be useful as part of a larger framework if allocation | ||
/// should be disallowed under certain circumstances. For example, a function | ||
/// that returns different allocators based on an input parameter may return a | ||
/// null allocator when given an invalid or unsupported parameter value. | ||
class NullAllocator : public Allocator { | ||
public: | ||
constexpr NullAllocator() = default; | ||
|
||
private: | ||
/// @copydoc Allocator::Allocate | ||
void* DoAllocate(size_t, size_t) override { return nullptr; } | ||
|
||
/// @copydoc Allocator::Deallocate | ||
void DoDeallocate(void*, size_t, size_t) override {} | ||
|
||
/// @copydoc Allocator::Resize | ||
bool DoResize(void*, size_t, size_t, size_t) override { return false; } | ||
}; | ||
|
||
} // namespace pw::allocator |