ams: prefer construct_at/destroy_at over placement new/explicit destructor
This commit is contained in:
@@ -29,7 +29,7 @@ namespace ams::util {
|
||||
private:
|
||||
ALWAYS_INLINE void FreeEntry(size_t i) {
|
||||
this->keys[i].reset();
|
||||
GetReference(this->values[i]).~Value();
|
||||
DestroyAt(this->values[i]);
|
||||
}
|
||||
public:
|
||||
constexpr BoundedMap() : keys(), values() { /* ... */ }
|
||||
@@ -78,7 +78,7 @@ namespace ams::util {
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
if (!this->keys[i]) {
|
||||
this->keys[i] = key;
|
||||
new (GetPointer(this->values[i])) Value(std::move(value));
|
||||
ConstructAt(this->values[i], std::forward<Value>(value));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -90,7 +90,7 @@ namespace ams::util {
|
||||
/* Try to find and assign an existing value. */
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
if (this->keys[i] && this->keys[i].value() == key) {
|
||||
GetReference(this->values[i]) = std::move(value);
|
||||
GetReference(this->values[i]) = std::forward<Value>(value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -99,7 +99,7 @@ namespace ams::util {
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
if (!this->keys[i]) {
|
||||
this->keys[i] = key;
|
||||
new (GetPointer(this->values[i])) Value(std::move(value));
|
||||
ConstructAt(this->values[i], std::move(value));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -118,7 +118,7 @@ namespace ams::util {
|
||||
for (size_t i = 0; i < N; i++) {
|
||||
if (!this->keys[i]) {
|
||||
this->keys[i] = key;
|
||||
new (GetPointer(this->values[i])) Value(std::forward<Args>(args)...);
|
||||
ConstructAt(this->values[i], std::forward<Args>(args)...);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace ams::util {
|
||||
ALWAYS_INLINE ScopeGuard(ScopeGuard&& rhs) : f(std::move(rhs.f)), active(rhs.active) {
|
||||
rhs.Cancel();
|
||||
}
|
||||
|
||||
ScopeGuard &operator=(ScopeGuard&& rhs) = delete;
|
||||
};
|
||||
|
||||
template<class F>
|
||||
|
||||
@@ -45,4 +45,46 @@ namespace ams::util {
|
||||
return *GetPointer(ts);
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
static constexpr ALWAYS_INLINE T *ConstructAt(TypedStorage<T> &ts, Args &&... args) {
|
||||
return std::construct_at(GetPointer(ts), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static constexpr ALWAYS_INLINE void DestroyAt(TypedStorage<T> &ts) {
|
||||
return std::destroy_at(GetPointer(ts));
|
||||
}
|
||||
|
||||
namespace impl {
|
||||
|
||||
template<typename T>
|
||||
class TypedStorageGuard {
|
||||
NON_COPYABLE(TypedStorageGuard);
|
||||
private:
|
||||
TypedStorage<T> &m_ts;
|
||||
bool m_active;
|
||||
public:
|
||||
template<typename... Args>
|
||||
constexpr ALWAYS_INLINE TypedStorageGuard(TypedStorage<T> &ts, Args &&... args) : m_ts(ts), m_active(true) {
|
||||
ConstructAt(m_ts, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
ALWAYS_INLINE ~TypedStorageGuard() { if (m_active) { DestroyAt(m_ts); } }
|
||||
|
||||
ALWAYS_INLINE void Cancel() { m_active = false; }
|
||||
|
||||
ALWAYS_INLINE TypedStorageGuard(TypedStorageGuard&& rhs) : m_ts(rhs.m_ts), m_active(rhs.m_active) {
|
||||
rhs.Cancel();
|
||||
}
|
||||
|
||||
TypedStorageGuard &operator=(TypedStorageGuard&& rhs) = delete;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
static constexpr ALWAYS_INLINE impl::TypedStorageGuard<T> ConstructAtGuarded(TypedStorage<T> &ts, Args &&... args) {
|
||||
return impl::TypedStorageGuard<T>(ts, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1085,7 +1085,7 @@ namespace ams::sdmmc::impl {
|
||||
/* This initializes a lot of globals in pcv, most of which we don't care about. */
|
||||
/* However, we do care about the Sdmmc1PowerController. */
|
||||
AMS_ABORT_UNLESS(this->power_controller == nullptr);
|
||||
this->power_controller = new (GetPointer(this->power_controller_storage)) PowerController;
|
||||
this->power_controller = util::ConstructAt(this->power_controller_storage);
|
||||
|
||||
/* Perform base initialization. */
|
||||
SdmmcController::Initialize();
|
||||
@@ -1099,8 +1099,8 @@ namespace ams::sdmmc::impl {
|
||||
/* As with initialize, we mostly don't care about the globals this touches. */
|
||||
/* However, we do want to finalize the Sdmmc1PowerController. */
|
||||
AMS_ABORT_UNLESS(this->power_controller != nullptr);
|
||||
this->power_controller->~PowerController();
|
||||
this->power_controller = nullptr;
|
||||
util::DestroyAt(this->power_controller_storage);
|
||||
|
||||
/* pinmux::CloseSession(std::addressof(this->pinmux_session)); */
|
||||
/* This does nothing. */
|
||||
|
||||
Reference in New Issue
Block a user