From 6ac955a0b441d762a2ebc0ce96bc41954879c0fc Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 10 Sep 2018 21:20:52 -0400 Subject: hle/service: Default constructors and destructors in the cpp file where applicable When a destructor isn't defaulted into a cpp file, it can cause the use of forward declarations to seemingly fail to compile for non-obvious reasons. It also allows inlining of the construction/destruction logic all over the place where a constructor or destructor is invoked, which can lead to code bloat. This isn't so much a worry here, given the services won't be created and destroyed frequently. The cause of the above mentioned non-obvious errors can be demonstrated as follows: ------- Demonstrative example, if you know how the described error happens, skip forwards ------- Assume we have the following in the header, which we'll call "thing.h": \#include // Forward declaration. For example purposes, assume the definition // of Object is in some header named "object.h" class Object; class Thing { public: // assume no constructors or destructors are specified here, // or the constructors/destructors are defined as: // // Thing() = default; // ~Thing() = default; // // ... Some interface member functions would be defined here private: std::shared_ptr obj; }; If this header is included in a cpp file, (which we'll call "main.cpp"), this will result in a compilation error, because even though no destructor is specified, the destructor will still need to be generated by the compiler because std::shared_ptr's destructor is *not* trivial (in other words, it does something other than nothing), as std::shared_ptr's destructor needs to do two things: 1. Decrement the shared reference count of the object being pointed to, and if the reference count decrements to zero, 2. Free the Object instance's memory (aka deallocate the memory it's pointing to). And so the compiler generates the code for the destructor doing this inside main.cpp. Now, keep in mind, the Object forward declaration is not a complete type. All it does is tell the compiler "a type named Object exists" and allows us to use the name in certain situations to avoid a header dependency. So the compiler needs to generate destruction code for Object, but the compiler doesn't know *how* to destruct it. A forward declaration doesn't tell the compiler anything about Object's constructor or destructor. So, the compiler will issue an error in this case because it's undefined behavior to try and deallocate (or construct) an incomplete type and std::shared_ptr and std::unique_ptr make sure this isn't the case internally. Now, if we had defaulted the destructor in "thing.cpp", where we also include "object.h", this would never be an issue, as the destructor would only have its code generated in one place, and it would be in a place where the full class definition of Object would be visible to the compiler. ---------------------- End example ---------------------------- Given these service classes are more than certainly going to change in the future, this defaults the constructors and destructors into the relevant cpp files to make the construction and destruction of all of the services consistent and unlikely to run into cases where forward declarations are indirectly causing compilation errors. It also has the plus of avoiding the need to rebuild several services if destruction logic changes, since it would only be necessary to recompile the single cpp file. --- src/core/hle/service/audio/audctl.cpp | 2 ++ src/core/hle/service/audio/audctl.h | 1 + src/core/hle/service/audio/auddbg.cpp | 2 ++ src/core/hle/service/audio/auddbg.h | 1 + src/core/hle/service/audio/audin_a.cpp | 2 ++ src/core/hle/service/audio/audin_a.h | 1 + src/core/hle/service/audio/audin_u.cpp | 2 ++ src/core/hle/service/audio/audin_u.h | 2 +- src/core/hle/service/audio/audout_a.cpp | 2 ++ src/core/hle/service/audio/audout_a.h | 1 + src/core/hle/service/audio/audout_u.cpp | 2 ++ src/core/hle/service/audio/audout_u.h | 2 +- src/core/hle/service/audio/audrec_a.cpp | 2 ++ src/core/hle/service/audio/audrec_a.h | 1 + src/core/hle/service/audio/audrec_u.cpp | 2 ++ src/core/hle/service/audio/audrec_u.h | 2 +- src/core/hle/service/audio/audren_a.cpp | 2 ++ src/core/hle/service/audio/audren_a.h | 1 + src/core/hle/service/audio/audren_u.cpp | 2 ++ src/core/hle/service/audio/audren_u.h | 2 +- src/core/hle/service/audio/codecctl.cpp | 2 ++ src/core/hle/service/audio/codecctl.h | 2 +- src/core/hle/service/audio/hwopus.cpp | 2 ++ src/core/hle/service/audio/hwopus.h | 2 +- 24 files changed, 36 insertions(+), 6 deletions(-) (limited to 'src/core/hle/service/audio') diff --git a/src/core/hle/service/audio/audctl.cpp b/src/core/hle/service/audio/audctl.cpp index 37c3fdcac..b6b71f966 100644 --- a/src/core/hle/service/audio/audctl.cpp +++ b/src/core/hle/service/audio/audctl.cpp @@ -42,4 +42,6 @@ AudCtl::AudCtl() : ServiceFramework{"audctl"} { RegisterHandlers(functions); } +AudCtl::~AudCtl() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audctl.h b/src/core/hle/service/audio/audctl.h index ed837bdf2..9d2d9e83b 100644 --- a/src/core/hle/service/audio/audctl.h +++ b/src/core/hle/service/audio/audctl.h @@ -11,6 +11,7 @@ namespace Service::Audio { class AudCtl final : public ServiceFramework { public: explicit AudCtl(); + ~AudCtl() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/auddbg.cpp b/src/core/hle/service/audio/auddbg.cpp index b08c21a20..8fff3e4b4 100644 --- a/src/core/hle/service/audio/auddbg.cpp +++ b/src/core/hle/service/audio/auddbg.cpp @@ -17,4 +17,6 @@ AudDbg::AudDbg(const char* name) : ServiceFramework{name} { RegisterHandlers(functions); } +AudDbg::~AudDbg() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/auddbg.h b/src/core/hle/service/audio/auddbg.h index a2f540b75..6689f4759 100644 --- a/src/core/hle/service/audio/auddbg.h +++ b/src/core/hle/service/audio/auddbg.h @@ -11,6 +11,7 @@ namespace Service::Audio { class AudDbg final : public ServiceFramework { public: explicit AudDbg(const char* name); + ~AudDbg() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audin_a.cpp b/src/core/hle/service/audio/audin_a.cpp index a70d5bca4..ddd12f35e 100644 --- a/src/core/hle/service/audio/audin_a.cpp +++ b/src/core/hle/service/audio/audin_a.cpp @@ -19,4 +19,6 @@ AudInA::AudInA() : ServiceFramework{"audin:a"} { RegisterHandlers(functions); } +AudInA::~AudInA() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audin_a.h b/src/core/hle/service/audio/audin_a.h index e4c75510f..e7623bc29 100644 --- a/src/core/hle/service/audio/audin_a.h +++ b/src/core/hle/service/audio/audin_a.h @@ -11,6 +11,7 @@ namespace Service::Audio { class AudInA final : public ServiceFramework { public: explicit AudInA(); + ~AudInA() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audin_u.cpp b/src/core/hle/service/audio/audin_u.cpp index cbc49e55e..657010312 100644 --- a/src/core/hle/service/audio/audin_u.cpp +++ b/src/core/hle/service/audio/audin_u.cpp @@ -41,4 +41,6 @@ AudInU::AudInU() : ServiceFramework("audin:u") { RegisterHandlers(functions); } +AudInU::~AudInU() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audin_u.h b/src/core/hle/service/audio/audin_u.h index 2e65efb5b..0538b9560 100644 --- a/src/core/hle/service/audio/audin_u.h +++ b/src/core/hle/service/audio/audin_u.h @@ -15,7 +15,7 @@ namespace Service::Audio { class AudInU final : public ServiceFramework { public: explicit AudInU(); - ~AudInU() = default; + ~AudInU() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audout_a.cpp b/src/core/hle/service/audio/audout_a.cpp index bf8d40157..85febbca3 100644 --- a/src/core/hle/service/audio/audout_a.cpp +++ b/src/core/hle/service/audio/audout_a.cpp @@ -21,4 +21,6 @@ AudOutA::AudOutA() : ServiceFramework{"audout:a"} { RegisterHandlers(functions); } +AudOutA::~AudOutA() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audout_a.h b/src/core/hle/service/audio/audout_a.h index 91a069152..d65b66e8e 100644 --- a/src/core/hle/service/audio/audout_a.h +++ b/src/core/hle/service/audio/audout_a.h @@ -11,6 +11,7 @@ namespace Service::Audio { class AudOutA final : public ServiceFramework { public: explicit AudOutA(); + ~AudOutA() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audout_u.cpp b/src/core/hle/service/audio/audout_u.cpp index 5f370bbdf..05100ca8f 100644 --- a/src/core/hle/service/audio/audout_u.cpp +++ b/src/core/hle/service/audio/audout_u.cpp @@ -218,4 +218,6 @@ AudOutU::AudOutU() : ServiceFramework("audout:u") { audio_core = std::make_unique(); } +AudOutU::~AudOutU() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audout_u.h b/src/core/hle/service/audio/audout_u.h index fd491f65d..aa52d3855 100644 --- a/src/core/hle/service/audio/audout_u.h +++ b/src/core/hle/service/audio/audout_u.h @@ -30,7 +30,7 @@ class IAudioOut; class AudOutU final : public ServiceFramework { public: AudOutU(); - ~AudOutU() = default; + ~AudOutU() override; private: std::shared_ptr audio_out_interface; diff --git a/src/core/hle/service/audio/audrec_a.cpp b/src/core/hle/service/audio/audrec_a.cpp index 016eabf53..ce1bfb48d 100644 --- a/src/core/hle/service/audio/audrec_a.cpp +++ b/src/core/hle/service/audio/audrec_a.cpp @@ -17,4 +17,6 @@ AudRecA::AudRecA() : ServiceFramework{"audrec:a"} { RegisterHandlers(functions); } +AudRecA::~AudRecA() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audrec_a.h b/src/core/hle/service/audio/audrec_a.h index 9685047f2..384d24c69 100644 --- a/src/core/hle/service/audio/audrec_a.h +++ b/src/core/hle/service/audio/audrec_a.h @@ -11,6 +11,7 @@ namespace Service::Audio { class AudRecA final : public ServiceFramework { public: explicit AudRecA(); + ~AudRecA() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audrec_u.cpp b/src/core/hle/service/audio/audrec_u.cpp index 74909415c..34974afa9 100644 --- a/src/core/hle/service/audio/audrec_u.cpp +++ b/src/core/hle/service/audio/audrec_u.cpp @@ -36,4 +36,6 @@ AudRecU::AudRecU() : ServiceFramework("audrec:u") { RegisterHandlers(functions); } +AudRecU::~AudRecU() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audrec_u.h b/src/core/hle/service/audio/audrec_u.h index 46daa33a4..ca3d638e8 100644 --- a/src/core/hle/service/audio/audrec_u.h +++ b/src/core/hle/service/audio/audrec_u.h @@ -15,7 +15,7 @@ namespace Service::Audio { class AudRecU final : public ServiceFramework { public: explicit AudRecU(); - ~AudRecU() = default; + ~AudRecU() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audren_a.cpp b/src/core/hle/service/audio/audren_a.cpp index 616ff3dc4..edb66d985 100644 --- a/src/core/hle/service/audio/audren_a.cpp +++ b/src/core/hle/service/audio/audren_a.cpp @@ -23,4 +23,6 @@ AudRenA::AudRenA() : ServiceFramework{"audren:a"} { RegisterHandlers(functions); } +AudRenA::~AudRenA() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audren_a.h b/src/core/hle/service/audio/audren_a.h index 5ecf2e184..81fef0ffe 100644 --- a/src/core/hle/service/audio/audren_a.h +++ b/src/core/hle/service/audio/audren_a.h @@ -11,6 +11,7 @@ namespace Service::Audio { class AudRenA final : public ServiceFramework { public: explicit AudRenA(); + ~AudRenA() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/audren_u.cpp b/src/core/hle/service/audio/audren_u.cpp index 016db7c82..3870bec65 100644 --- a/src/core/hle/service/audio/audren_u.cpp +++ b/src/core/hle/service/audio/audren_u.cpp @@ -198,6 +198,8 @@ AudRenU::AudRenU() : ServiceFramework("audren:u") { RegisterHandlers(functions); } +AudRenU::~AudRenU() = default; + void AudRenU::OpenAudioRenderer(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; auto params = rp.PopRaw(); diff --git a/src/core/hle/service/audio/audren_u.h b/src/core/hle/service/audio/audren_u.h index 8600ac6e4..85a995a2f 100644 --- a/src/core/hle/service/audio/audren_u.h +++ b/src/core/hle/service/audio/audren_u.h @@ -16,7 +16,7 @@ namespace Service::Audio { class AudRenU final : public ServiceFramework { public: explicit AudRenU(); - ~AudRenU() = default; + ~AudRenU() override; private: void OpenAudioRenderer(Kernel::HLERequestContext& ctx); diff --git a/src/core/hle/service/audio/codecctl.cpp b/src/core/hle/service/audio/codecctl.cpp index 212c8d448..c6864146d 100644 --- a/src/core/hle/service/audio/codecctl.cpp +++ b/src/core/hle/service/audio/codecctl.cpp @@ -28,4 +28,6 @@ CodecCtl::CodecCtl() : ServiceFramework("codecctl") { RegisterHandlers(functions); } +CodecCtl::~CodecCtl() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/codecctl.h b/src/core/hle/service/audio/codecctl.h index d9ac29b67..2fe75b6e2 100644 --- a/src/core/hle/service/audio/codecctl.h +++ b/src/core/hle/service/audio/codecctl.h @@ -15,7 +15,7 @@ namespace Service::Audio { class CodecCtl final : public ServiceFramework { public: explicit CodecCtl(); - ~CodecCtl() = default; + ~CodecCtl() override; }; } // namespace Service::Audio diff --git a/src/core/hle/service/audio/hwopus.cpp b/src/core/hle/service/audio/hwopus.cpp index 371cd4997..341bfda42 100644 --- a/src/core/hle/service/audio/hwopus.cpp +++ b/src/core/hle/service/audio/hwopus.cpp @@ -151,4 +151,6 @@ HwOpus::HwOpus() : ServiceFramework("hwopus") { RegisterHandlers(functions); } +HwOpus::~HwOpus() = default; + } // namespace Service::Audio diff --git a/src/core/hle/service/audio/hwopus.h b/src/core/hle/service/audio/hwopus.h index 5258d59f3..602ede8ba 100644 --- a/src/core/hle/service/audio/hwopus.h +++ b/src/core/hle/service/audio/hwopus.h @@ -11,7 +11,7 @@ namespace Service::Audio { class HwOpus final : public ServiceFramework { public: explicit HwOpus(); - ~HwOpus() = default; + ~HwOpus() override; private: void OpenOpusDecoder(Kernel::HLERequestContext& ctx); -- cgit v1.2.3