Skip to content

Commit

Permalink
mir::SharedLibrary: Add a stable Handle for SharedLibrary
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
RAOF committed Sep 6, 2024
1 parent 13c01c6 commit b9ceb10
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
17 changes: 16 additions & 1 deletion include/common/mir/shared_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef MIR_SHARED_LIBRARY_H_
#define MIR_SHARED_LIBRARY_H_

#include <compare>
#include <string>

namespace mir
Expand Down Expand Up @@ -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;
Expand All @@ -58,5 +74,4 @@ class SharedLibrary
};
}


#endif /* MIR_SHARED_LIBRARY_H_ */
16 changes: 16 additions & 0 deletions src/common/sharedlibrary/shared_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "mir/shared_library.h"
#include <compare>

#ifdef MIR_DONT_USE_DLVSYM
#include <mir/log.h>
Expand Down Expand Up @@ -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;
}
6 changes: 5 additions & 1 deletion src/common/symbols.map
Original file line number Diff line number Diff line change
Expand Up @@ -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*;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -695,4 +699,4 @@ MIR_COMMON_2.18 {
MirTouchpadConfig::disable_with_external_mouse*;
mir::event_type_to_c_str*;
};
} MIR_COMMON_2.17;
} MIR_COMMON_2.17;

0 comments on commit b9ceb10

Please sign in to comment.