-
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 LibCAllocator, a memory allocator that uses `malloc` and `free`. Change-Id: Idf3f1ce4e4e72ff3c8bff20f3c62ca373f45592e Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/172232 Commit-Queue: Aaron Green <[email protected]> Reviewed-by: Taylor Cramer <[email protected]>
- Loading branch information
1 parent
3cfe5d0
commit 2d88135
Showing
7 changed files
with
249 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,52 @@ | ||
// 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/libc_allocator.h" | ||
|
||
#include <algorithm> | ||
#include <cstddef> | ||
#include <cstdlib> | ||
#include <cstring> | ||
|
||
#include "pw_assert/check.h" | ||
#include "pw_bytes/alignment.h" | ||
|
||
namespace pw::allocator { | ||
|
||
void* LibCAllocator::DoAllocate(size_t size, size_t alignment) { | ||
// TODO: b/301930507 - `aligned_alloc` is not portable. Return null for larger | ||
// allocations for now. | ||
return alignment <= alignof(std::max_align_t) ? std::malloc(size) : nullptr; | ||
} | ||
|
||
void LibCAllocator::DoDeallocate(void* ptr, size_t, size_t) { std::free(ptr); } | ||
|
||
bool LibCAllocator::DoResize(void*, size_t old_size, size_t, size_t new_size) { | ||
// `realloc` may move memory, even when shrinking. Only return true if no | ||
// change is needed. | ||
return old_size == new_size; | ||
} | ||
|
||
void* LibCAllocator::DoReallocate(void* ptr, | ||
size_t, | ||
size_t old_alignment, | ||
size_t new_size) { | ||
// TODO: b/301930507 - `aligned_alloc` is not portable. Return null for larger | ||
// allocations for now. | ||
return old_alignment <= alignof(std::max_align_t) | ||
? std::realloc(ptr, new_size) | ||
: nullptr; | ||
} | ||
|
||
} // 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,76 @@ | ||
// 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/libc_allocator.h" | ||
|
||
#include <cstring> | ||
|
||
#include "gtest/gtest.h" | ||
|
||
namespace pw::allocator { | ||
|
||
// Test fixtures. | ||
|
||
struct LibCAllocatorTest : ::testing::Test { | ||
LibCAllocator allocator; | ||
}; | ||
|
||
// Unit tests. | ||
|
||
TEST_F(LibCAllocatorTest, AllocateDeallocate) { | ||
constexpr Layout layout = Layout::Of<std::byte[64]>(); | ||
void* ptr = allocator.Allocate(layout); | ||
ASSERT_NE(ptr, nullptr); | ||
// Check that the pointer can be dereferenced. | ||
memset(ptr, 0xAB, layout.size()); | ||
allocator.Deallocate(ptr, layout); | ||
} | ||
|
||
TEST_F(LibCAllocatorTest, AllocateLargeAlignment) { | ||
/// TODO: b/301930507 - `aligned_alloc` is not portable. As a result, this | ||
/// allocator has a maximum alignment of `std::align_max_t`. | ||
size_t size = 16; | ||
size_t alignment = alignof(std::max_align_t) * 2; | ||
void* ptr = allocator.AllocateUnchecked(size, alignment); | ||
EXPECT_EQ(ptr, nullptr); | ||
} | ||
|
||
TEST_F(LibCAllocatorTest, Resize) { | ||
constexpr Layout old_layout = Layout::Of<uint32_t[4]>(); | ||
void* ptr = allocator.Allocate(old_layout); | ||
ASSERT_NE(ptr, nullptr); | ||
constexpr Layout new_layout = Layout::Of<uint32_t[3]>(); | ||
EXPECT_FALSE(allocator.Resize(ptr, old_layout, new_layout.size())); | ||
allocator.Deallocate(ptr, old_layout); | ||
} | ||
|
||
TEST_F(LibCAllocatorTest, ResizeSame) { | ||
constexpr Layout layout = Layout::Of<uint32_t[4]>(); | ||
void* ptr = allocator.Allocate(layout); | ||
ASSERT_NE(ptr, nullptr); | ||
EXPECT_TRUE(allocator.Resize(ptr, layout, layout.size())); | ||
allocator.Deallocate(ptr, layout); | ||
} | ||
|
||
TEST_F(LibCAllocatorTest, Reallocate) { | ||
constexpr Layout old_layout = Layout::Of<uint32_t[4]>(); | ||
void* ptr = allocator.Allocate(old_layout); | ||
ASSERT_NE(ptr, nullptr); | ||
constexpr Layout new_layout = Layout::Of<uint32_t[3]>(); | ||
void* new_ptr = allocator.Reallocate(ptr, old_layout, new_layout.size()); | ||
ASSERT_NE(new_ptr, nullptr); | ||
allocator.Deallocate(new_ptr, new_layout); | ||
} | ||
|
||
} // 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,48 @@ | ||
// 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 { | ||
|
||
/// Memory allocator that uses `malloc` and `free`. | ||
/// | ||
/// TODO: b/301930507 - `aligned_alloc` is not portable. As a result, this | ||
/// allocator has a maximum alignment of `std::align_max_t`. | ||
class LibCAllocator : public Allocator { | ||
public: | ||
constexpr LibCAllocator() = default; | ||
|
||
private: | ||
/// @copydoc Allocator::Allocate | ||
void* DoAllocate(size_t size, size_t alignment) override; | ||
|
||
/// @copydoc Allocator::Deallocate | ||
void DoDeallocate(void* ptr, size_t size, size_t alignment) override; | ||
|
||
/// @copydoc Allocator::Resize | ||
bool DoResize(void* ptr, | ||
size_t old_size, | ||
size_t old_alignment, | ||
size_t new_size) override; | ||
|
||
/// @copydoc Allocator::Reallocate | ||
void* DoReallocate(void* ptr, | ||
size_t old_size, | ||
size_t old_alignment, | ||
size_t new_size) override; | ||
}; | ||
|
||
} // namespace pw::allocator |