-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement polymorphic_allocator<> #1311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
ffdf0d6
Implemanet polymorphic_allocator<> (#10)
onihusube 4530e2d
fix format
onihusube 652ccb3
add newline
onihusube 12e682f
Merge branch 'master' of https://github.com/onihusube/STL
onihusube 69eafeb
Update stl/inc/xpolymorphic_allocator.h
onihusube c75ae36
Update stl/inc/xpolymorphic_allocator.h
onihusube 6f2b314
Update stl/inc/yvals_core.h
onihusube fd6a17d
add noexcept
onihusube 7c10115
use _TRY_BEGIN
onihusube a7703be
guard std::byte
onihusube 8dd7eb5
fix test order
onihusube 608dd43
fix and add test
onihusube d78ef03
fix format
onihusube fa0a388
Update include directives.
StephanTLavavej 9dc8b18
Use `std::uintptr_t` for modulo.
StephanTLavavej fc74297
Update stl/inc/xpolymorphic_allocator.h
onihusube 8bcff9a
Update stl/inc/xpolymorphic_allocator.h
onihusube 60e695e
remove this
onihusube 4bde969
fix declaration order
onihusube eda3b41
add const
onihusube a046c79
use _Destroy_in_place
onihusube 0e7fe2c
use std::destroy
onihusube File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\usual_latest_matrix.lst |
This file contains hidden or 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,108 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <cassert> | ||
| #include <cstddef> | ||
| #include <memory> | ||
| #include <memory_resource> | ||
| #include <numeric> | ||
|
|
||
| using std::pmr::polymorphic_allocator; | ||
|
|
||
| void allocate_bytes_test() { | ||
| constexpr int N = 5; | ||
|
|
||
| polymorphic_allocator<> alloc{}; | ||
|
|
||
| void* vp = alloc.allocate_bytes(sizeof(int) * N, alignof(int)); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| int* arr = static_cast<int*>(vp); | ||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| alloc.construct(arr + i, i); | ||
| } | ||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| assert(arr[i] == i); | ||
| } | ||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| std::destroy_at(arr + i); | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
StephanTLavavej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| alloc.deallocate_bytes(vp, sizeof(int) * N, alignof(int)); | ||
|
|
||
| void* vp2 = alloc.allocate_bytes(sizeof(int)); | ||
| assert(reinterpret_cast<std::intptr_t>(vp2) % alignof(std::max_align_t) == 0); | ||
| alloc.deallocate_bytes(vp2, sizeof(int)); | ||
| } | ||
|
|
||
| void allocate_object_test() { | ||
| constexpr int N = 10; | ||
|
|
||
| polymorphic_allocator<> alloc{}; | ||
|
|
||
| int* arr = alloc.allocate_object<int>(N); | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| alloc.construct(arr + i, i); | ||
| } | ||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| assert(arr[i] == i); | ||
| } | ||
|
|
||
| for (int i = 0; i < N; ++i) { | ||
| std::destroy_at(arr + i); | ||
| } | ||
|
|
||
| alloc.deallocate_object(arr, N); | ||
|
|
||
| // N = 1 | ||
| int* p = alloc.allocate_object<int>(); | ||
| alloc.construct(p, 20); | ||
| assert(*p == 20); | ||
| std::destroy_at(p); | ||
| alloc.deallocate_object(p); | ||
| } | ||
|
|
||
| void allocate_object_overflow_test() { | ||
| constexpr auto threshold = std::numeric_limits<std::size_t>::max() / sizeof(int); | ||
|
|
||
| polymorphic_allocator<> alloc{}; | ||
|
|
||
| try { | ||
| int* vp = alloc.allocate_object<int>(threshold); | ||
| alloc.deallocate_object(vp, threshold); | ||
| } catch (const std::bad_alloc&) { | ||
| } catch (...) { | ||
| assert(false); | ||
| } | ||
|
|
||
| try { | ||
| [[maybe_unused]] int* vp = alloc.allocate_object<int>(threshold + 1); | ||
| } catch (const std::bad_array_new_length&) { | ||
| return; | ||
| } catch (...) { | ||
| assert(false); | ||
| } | ||
| assert(false); | ||
| } | ||
StephanTLavavej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| void new_object_test() { | ||
| polymorphic_allocator<> alloc{}; | ||
|
|
||
| int* p = alloc.new_object<int>(20); | ||
|
|
||
| assert(*p == 20); | ||
|
|
||
| alloc.delete_object(p); | ||
| } | ||
|
|
||
|
|
||
| int main() { | ||
| allocate_bytes_test(); | ||
| allocate_object_test(); | ||
| allocate_object_overflow_test(); | ||
| new_object_test(); | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.