-
Notifications
You must be signed in to change notification settings - Fork 204
android: Replace env->NewGlobalRef with NoDestructor wrapper #10783
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c159a30
starboard/android: Replace env->NewGlobalRef with NoDestructor wrapper
kjyoun 35e6bce
Address comments
kjyoun faf4f2f
Address comments
kjyoun 90f13a7
Copy base/no_destructor.h
kjyoun dabb923
Merge branch 'main' into handle-g_j_video_surface
kjyoun 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright 2026 The Cobalt Authors. All Rights Reserved. | ||
| // | ||
| // 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 | ||
| // | ||
| // http://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. | ||
|
|
||
| #ifndef STARBOARD_COMMON_NO_DESTRUCTOR_H_ | ||
| #define STARBOARD_COMMON_NO_DESTRUCTOR_H_ | ||
|
|
||
| #include <new> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| namespace starboard { | ||
|
|
||
| // Helper type to create a function-local static variable of type `T` when `T` | ||
| // has a non-trivial destructor. Storing a `T` in a `starboard::NoDestructor<T>` | ||
| // will prevent `~T()` from running, even when the variable goes out of scope. | ||
| // | ||
| // Useful when a variable has static storage duration but its type has a | ||
| // non-trivial destructor. Global constructors and destructors are discouraged: | ||
| // using a function-local static variable prevents the former, while using | ||
| // `starboard::NoDestructor<T>` prevents the latter. | ||
| // | ||
| // See base::NoDestructor for more details and caveats. | ||
| template <typename T> | ||
| class NoDestructor { | ||
| public: | ||
| static_assert(!(std::is_trivially_constructible_v<T> && | ||
| std::is_trivially_destructible_v<T>), | ||
| "T is trivially constructible and destructible; please use a " | ||
| "constinit object of type T directly instead"); | ||
|
|
||
| static_assert( | ||
| !std::is_trivially_destructible_v<T>, | ||
| "T is trivially destructible; please use a function-local static " | ||
| "of type T directly instead"); | ||
|
|
||
| template <typename... Args> | ||
| explicit NoDestructor(Args&&... args) { | ||
| new (storage_) T(std::forward<Args>(args)...); | ||
| } | ||
|
|
||
| explicit NoDestructor(const T& x) { new (storage_) T(x); } | ||
| explicit NoDestructor(T&& x) { new (storage_) T(std::move(x)); } | ||
|
|
||
| NoDestructor(const NoDestructor&) = delete; | ||
| NoDestructor& operator=(const NoDestructor&) = delete; | ||
|
|
||
| ~NoDestructor() = default; | ||
|
|
||
| const T& operator*() const { return *get(); } | ||
| T& operator*() { return *get(); } | ||
|
|
||
| const T* operator->() const { return get(); } | ||
| T* operator->() { return get(); } | ||
|
|
||
| const T* get() const { return reinterpret_cast<const T*>(storage_); } | ||
| T* get() { return reinterpret_cast<T*>(storage_); } | ||
|
kjyoun marked this conversation as resolved.
|
||
|
|
||
| private: | ||
| alignas(T) char storage_[sizeof(T)]; | ||
|
|
||
| #if defined(LEAK_SANITIZER) | ||
| T* storage_ptr_ = reinterpret_cast<T*>(storage_); | ||
| #endif // defined(LEAK_SANITIZER) | ||
| }; | ||
|
|
||
| } // namespace starboard | ||
|
|
||
| #endif // STARBOARD_COMMON_NO_DESTRUCTOR_H_ | ||
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,97 @@ | ||
| // Copyright 2026 The Cobalt Authors. All Rights Reserved. | ||
| // | ||
| // 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 | ||
| // | ||
| // http://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 "starboard/common/no_destructor.h" | ||
|
|
||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "testing/gtest/include/gtest/gtest.h" | ||
|
|
||
| namespace starboard { | ||
| namespace { | ||
|
|
||
| struct NotreachedOnDestroy { | ||
| ~NotreachedOnDestroy() { | ||
| ADD_FAILURE() << "~NotreachedOnDestroy should not be called"; | ||
| } | ||
| }; | ||
|
|
||
| TEST(NoDestructorTest, SkipsDestructors) { | ||
| NoDestructor<NotreachedOnDestroy> destructor_should_not_run; | ||
| } | ||
|
|
||
| struct UncopyableUnmovable { | ||
| UncopyableUnmovable() = default; | ||
| explicit UncopyableUnmovable(int value) : value(value) {} | ||
|
|
||
| UncopyableUnmovable(const UncopyableUnmovable&) = delete; | ||
| UncopyableUnmovable& operator=(const UncopyableUnmovable&) = delete; | ||
|
|
||
| int value = 1; | ||
| std::string dummy_to_make_nontrivial_destructor; | ||
| }; | ||
|
|
||
| struct CopyOnly { | ||
| CopyOnly() = default; | ||
|
|
||
| CopyOnly(const CopyOnly&) = default; | ||
| CopyOnly& operator=(const CopyOnly&) = default; | ||
|
|
||
| CopyOnly(CopyOnly&&) = delete; | ||
| CopyOnly& operator=(CopyOnly&&) = delete; | ||
| }; | ||
|
|
||
| struct MoveOnly { | ||
| MoveOnly() = default; | ||
|
|
||
| MoveOnly(const MoveOnly&) = delete; | ||
| MoveOnly& operator=(const MoveOnly&) = delete; | ||
|
|
||
| MoveOnly(MoveOnly&&) = default; | ||
| MoveOnly& operator=(MoveOnly&&) = default; | ||
| }; | ||
|
|
||
| struct ForwardingTestStruct { | ||
| ForwardingTestStruct(const CopyOnly&, MoveOnly&&) {} | ||
| std::string dummy_to_make_nontrivial_destructor; | ||
| }; | ||
|
|
||
| TEST(NoDestructorTest, UncopyableUnmovable) { | ||
| static NoDestructor<UncopyableUnmovable> default_constructed; | ||
| EXPECT_EQ(1, default_constructed->value); | ||
|
|
||
| static NoDestructor<UncopyableUnmovable> constructed_with_arg(-1); | ||
| EXPECT_EQ(-1, constructed_with_arg->value); | ||
| } | ||
|
|
||
| TEST(NoDestructorTest, ForwardsArguments) { | ||
| CopyOnly copy_only; | ||
| MoveOnly move_only; | ||
|
|
||
| static NoDestructor<ForwardingTestStruct> test_forwarding( | ||
| copy_only, std::move(move_only)); | ||
| } | ||
|
|
||
| TEST(NoDestructorTest, Accessors) { | ||
| static NoDestructor<std::string> awesome("awesome"); | ||
|
|
||
| EXPECT_EQ("awesome", *awesome); | ||
| EXPECT_EQ(0, awesome->compare("awesome")); | ||
| EXPECT_EQ(0, awesome.get()->compare("awesome")); | ||
| } | ||
|
|
||
| } // namespace | ||
| } // namespace starboard |
Oops, something went wrong.
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.