From b9ceb1018fe9cfc7c306720dfc4752e3759a28c0 Mon Sep 17 00:00:00 2001 From: Christopher James Halse Rogers Date: Fri, 6 Sep 2024 11:20:00 +1000 Subject: [PATCH] mir::SharedLibrary: Add a stable Handle for SharedLibrary It can be useful to know if two `SharedLibrary` instances refer to the same DSO. The new `SharedLibrary::Handle` implements such a comparable handle. This works because `dlopen` guarantees that loading an already-loaded DSO returns the same handle. --- include/common/mir/shared_library.h | 17 ++++++++++++++++- src/common/sharedlibrary/shared_library.cpp | 16 ++++++++++++++++ src/common/symbols.map | 6 +++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/common/mir/shared_library.h b/include/common/mir/shared_library.h index 13c5b05da67..dd783d669da 100644 --- a/include/common/mir/shared_library.h +++ b/include/common/mir/shared_library.h @@ -17,6 +17,7 @@ #ifndef MIR_SHARED_LIBRARY_H_ #define MIR_SHARED_LIBRARY_H_ +#include #include namespace mir @@ -49,6 +50,21 @@ class SharedLibrary (void*&)result = load_symbol(function_name.c_str(), version.c_str()); return result; } + + class Handle + { + public: + auto operator<=>(Handle const& rhs) const -> std::strong_ordering; + auto operator==(Handle const& rhs) const -> bool = default; + auto operator!=(Handle const& rhs) const -> bool = default; + private: + friend class SharedLibrary; + + Handle(void* handle); + void* handle; + }; + + auto get_handle() const -> Handle; private: void* const so; void* load_symbol(char const* function_name) const; @@ -58,5 +74,4 @@ class SharedLibrary }; } - #endif /* MIR_SHARED_LIBRARY_H_ */ diff --git a/src/common/sharedlibrary/shared_library.cpp b/src/common/sharedlibrary/shared_library.cpp index ec41047c023..a76e9b010e4 100644 --- a/src/common/sharedlibrary/shared_library.cpp +++ b/src/common/sharedlibrary/shared_library.cpp @@ -15,6 +15,7 @@ */ #include "mir/shared_library.h" +#include #ifdef MIR_DONT_USE_DLVSYM #include @@ -74,3 +75,18 @@ void* mir::SharedLibrary::load_symbol(char const* function_name, char const* ver } #endif } + +auto mir::SharedLibrary::get_handle() const -> Handle +{ + return Handle{so}; +} + +mir::SharedLibrary::Handle::Handle(void* handle) + : handle{handle} +{ +} + +auto mir::SharedLibrary::Handle::operator<=>(Handle const& rhs) const -> std::strong_ordering +{ + return handle <=> rhs.handle; +} diff --git a/src/common/symbols.map b/src/common/symbols.map index 3f73ccc844e..eb0b74ed64d 100644 --- a/src/common/symbols.map +++ b/src/common/symbols.map @@ -325,6 +325,9 @@ global: mir::SharedLibrary::?SharedLibrary*; mir::SharedLibrary::SharedLibrary*; mir::SharedLibrary::load_symbol*; + mir::SharedLibrary::get_handle*; + mir::SharedLibrary::Handle::?Handle*; + mir::SharedLibrary::Handle::operator*; mir::SharedLibraryProberReport::?SharedLibraryProberReport*; mir::SharedLibraryProberReport::SharedLibraryProberReport*; mir::SharedLibraryProberReport::operator*; @@ -500,6 +503,7 @@ global: typeinfo?for?mir::NonBlockingExecutor; typeinfo?for?mir::PosixRWMutex; typeinfo?for?mir::SharedLibrary; + typeinfo?for?mir::SharedLibrary::Handle; typeinfo?for?mir::SharedLibraryProberReport; typeinfo?for?mir::Signal; typeinfo?for?mir::ThreadPoolExecutor; @@ -695,4 +699,4 @@ MIR_COMMON_2.18 { MirTouchpadConfig::disable_with_external_mouse*; mir::event_type_to_c_str*; }; -} MIR_COMMON_2.17; \ No newline at end of file +} MIR_COMMON_2.17;