aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2023-05-04 09:16:59 -0700
committerChris Robinson <[email protected]>2023-05-04 09:27:19 -0700
commit6e0a0a2692a4303d6410c24bf83e09ca47ac6759 (patch)
tree906db65a3900d89b07a67d8b938ecc5977dcb9bb
parent3d2e586636f765eb2fccebb757305295d7b2c954 (diff)
Make and use a bit_cast function
Instead of reinterpret_casting between incompatible types
-rw-r--r--al/buffer.cpp2
-rw-r--r--al/state.cpp5
-rw-r--r--alc/alc.cpp2
-rw-r--r--alc/alconfig.cpp3
-rw-r--r--alc/backends/alsa.cpp3
-rw-r--r--alc/backends/dsound.cpp3
-rw-r--r--alc/backends/jack.cpp5
-rw-r--r--alc/backends/pipewire.cpp9
-rw-r--r--alc/backends/portaudio.cpp3
-rw-r--r--alc/backends/pulseaudio.cpp3
-rw-r--r--alc/backends/wasapi.cpp4
-rw-r--r--common/albit.h11
-rw-r--r--common/dynload.cpp3
-rw-r--r--core/dbus_wrap.cpp3
14 files changed, 40 insertions, 19 deletions
diff --git a/al/buffer.cpp b/al/buffer.cpp
index 4f0bcf8c..ad74a7f7 100644
--- a/al/buffer.cpp
+++ b/al/buffer.cpp
@@ -1463,7 +1463,7 @@ START_API_FUNC
else switch(param)
{
case AL_BUFFER_CALLBACK_FUNCTION_SOFT:
- *value = reinterpret_cast<void*>(albuf->mCallback);
+ *value = al::bit_cast<void*>(albuf->mCallback);
break;
case AL_BUFFER_CALLBACK_USER_PARAM_SOFT:
*value = albuf->mUserData;
diff --git a/al/state.cpp b/al/state.cpp
index 77b104a4..fb3186c7 100644
--- a/al/state.cpp
+++ b/al/state.cpp
@@ -35,6 +35,7 @@
#include "AL/alext.h"
#include "al/debug.h"
+#include "albit.h"
#include "alc/alu.h"
#include "alc/context.h"
#include "alc/inprogext.h"
@@ -516,7 +517,7 @@ START_API_FUNC
switch(pname)
{
case AL_EVENT_CALLBACK_FUNCTION_SOFT:
- *values = reinterpret_cast<void*>(context->mEventCb);
+ *values = al::bit_cast<void*>(context->mEventCb);
break;
case AL_EVENT_CALLBACK_USER_PARAM_SOFT:
@@ -524,7 +525,7 @@ START_API_FUNC
break;
case AL_DEBUG_CALLBACK_FUNCTION_EXT:
- *values = reinterpret_cast<void*>(context->mDebugCb);
+ *values = al::bit_cast<void*>(context->mDebugCb);
break;
case AL_DEBUG_CALLBACK_USER_PARAM_EXT:
diff --git a/alc/alc.cpp b/alc/alc.cpp
index 2da5c5db..149223f4 100644
--- a/alc/alc.cpp
+++ b/alc/alc.cpp
@@ -180,7 +180,7 @@ BOOL APIENTRY DllMain(HINSTANCE module, DWORD reason, LPVOID /*reserved*/)
case DLL_PROCESS_ATTACH:
/* Pin the DLL so we won't get unloaded until the process terminates */
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
- reinterpret_cast<WCHAR*>(module), &module);
+ al::bit_cast<WCHAR*>(module), &module);
break;
}
return TRUE;
diff --git a/alc/alconfig.cpp b/alc/alconfig.cpp
index 56cad9e0..ceb7d97b 100644
--- a/alc/alconfig.cpp
+++ b/alc/alconfig.cpp
@@ -38,6 +38,7 @@
#include <string>
#include <utility>
+#include "albit.h"
#include "alfstream.h"
#include "alstring.h"
#include "core/helpers.h"
@@ -418,7 +419,7 @@ void ReadALConfig()
if((configURL=CFBundleCopyResourceURL(mainBundle, CFSTR(".alsoftrc"), CFSTR(""), nullptr)) &&
CFURLGetFileSystemRepresentation(configURL, true, fileName, sizeof(fileName)))
{
- f = al::ifstream{reinterpret_cast<char*>(fileName)};
+ f = al::ifstream{al::bit_cast<char*>(fileName)};
if(f.is_open())
LoadConfigFromFile(f);
}
diff --git a/alc/backends/alsa.cpp b/alc/backends/alsa.cpp
index 01021868..b162f84e 100644
--- a/alc/backends/alsa.cpp
+++ b/alc/backends/alsa.cpp
@@ -35,6 +35,7 @@
#include <thread>
#include <utility>
+#include "albit.h"
#include "albyte.h"
#include "alc/alconfig.h"
#include "almalloc.h"
@@ -1204,7 +1205,7 @@ bool AlsaBackendFactory::init()
error = false;
#define LOAD_FUNC(f) do { \
- p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(alsa_handle, #f)); \
+ p##f = al::bit_cast<decltype(p##f)>(GetSymbol(alsa_handle, #f)); \
if(p##f == nullptr) { \
error = true; \
missing_funcs += "\n" #f; \
diff --git a/alc/backends/dsound.cpp b/alc/backends/dsound.cpp
index 51dc36f6..8b967c95 100644
--- a/alc/backends/dsound.cpp
+++ b/alc/backends/dsound.cpp
@@ -44,6 +44,7 @@
#include <algorithm>
#include <functional>
+#include "albit.h"
#include "alnumeric.h"
#include "comptr.h"
#include "core/device.h"
@@ -778,7 +779,7 @@ bool DSoundBackendFactory::init()
}
#define LOAD_FUNC(f) do { \
- p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(ds_handle, #f)); \
+ p##f = al::bit_cast<decltype(p##f)>(GetSymbol(ds_handle, #f)); \
if(!p##f) \
{ \
CloseLib(ds_handle); \
diff --git a/alc/backends/jack.cpp b/alc/backends/jack.cpp
index 791002ca..dbc2b038 100644
--- a/alc/backends/jack.cpp
+++ b/alc/backends/jack.cpp
@@ -31,6 +31,7 @@
#include <thread>
#include <functional>
+#include "albit.h"
#include "alc/alconfig.h"
#include "alnumeric.h"
#include "core/device.h"
@@ -126,7 +127,7 @@ bool jack_load()
error = false;
#define LOAD_FUNC(f) do { \
- p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(jack_handle, #f)); \
+ p##f = al::bit_cast<decltype(p##f)>(GetSymbol(jack_handle, #f)); \
if(p##f == nullptr) { \
error = true; \
missing_funcs += "\n" #f; \
@@ -135,7 +136,7 @@ bool jack_load()
JACK_FUNCS(LOAD_FUNC);
#undef LOAD_FUNC
/* Optional symbols. These don't exist in all versions of JACK. */
-#define LOAD_SYM(f) p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(jack_handle, #f))
+#define LOAD_SYM(f) p##f = al::bit_cast<decltype(p##f)>(GetSymbol(jack_handle, #f))
LOAD_SYM(jack_error_callback);
#undef LOAD_SYM
diff --git a/alc/backends/pipewire.cpp b/alc/backends/pipewire.cpp
index d3c8e77e..5b1596aa 100644
--- a/alc/backends/pipewire.cpp
+++ b/alc/backends/pipewire.cpp
@@ -37,6 +37,7 @@
#include <type_traits>
#include <utility>
+#include "albit.h"
#include "albyte.h"
#include "alc/alconfig.h"
#include "almalloc.h"
@@ -210,7 +211,7 @@ bool pwire_load()
}
#define LOAD_FUNC(f) do { \
- p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(pwire_handle, #f)); \
+ p##f = al::bit_cast<decltype(p##f)>(GetSymbol(pwire_handle, #f)); \
if(p##f == nullptr) missing_funcs += "\n" #f; \
} while(0);
PWIRE_FUNCS(LOAD_FUNC)
@@ -328,11 +329,11 @@ To as(From) noexcept = delete;
* - pw_metadata
*/
template<>
-pw_proxy* as(pw_registry *reg) noexcept { return reinterpret_cast<pw_proxy*>(reg); }
+pw_proxy* as(pw_registry *reg) noexcept { return al::bit_cast<pw_proxy*>(reg); }
template<>
-pw_proxy* as(pw_node *node) noexcept { return reinterpret_cast<pw_proxy*>(node); }
+pw_proxy* as(pw_node *node) noexcept { return al::bit_cast<pw_proxy*>(node); }
template<>
-pw_proxy* as(pw_metadata *mdata) noexcept { return reinterpret_cast<pw_proxy*>(mdata); }
+pw_proxy* as(pw_metadata *mdata) noexcept { return al::bit_cast<pw_proxy*>(mdata); }
struct PwContextDeleter {
diff --git a/alc/backends/portaudio.cpp b/alc/backends/portaudio.cpp
index 9c94587d..d652d4cd 100644
--- a/alc/backends/portaudio.cpp
+++ b/alc/backends/portaudio.cpp
@@ -26,6 +26,7 @@
#include <cstdlib>
#include <cstring>
+#include "albit.h"
#include "alc/alconfig.h"
#include "alnumeric.h"
#include "core/device.h"
@@ -376,7 +377,7 @@ bool PortBackendFactory::init()
return false;
#define LOAD_FUNC(f) do { \
- p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(pa_handle, #f)); \
+ p##f = al::bit_cast<decltype(p##f)>(GetSymbol(pa_handle, #f)); \
if(p##f == nullptr) \
{ \
CloseLib(pa_handle); \
diff --git a/alc/backends/pulseaudio.cpp b/alc/backends/pulseaudio.cpp
index 6f706c7f..6e8bfe1b 100644
--- a/alc/backends/pulseaudio.cpp
+++ b/alc/backends/pulseaudio.cpp
@@ -38,6 +38,7 @@
#include <sys/types.h>
#include <utility>
+#include "albit.h"
#include "albyte.h"
#include "alc/alconfig.h"
#include "almalloc.h"
@@ -1381,7 +1382,7 @@ bool PulseBackendFactory::init()
}
#define LOAD_FUNC(x) do { \
- p##x = reinterpret_cast<decltype(p##x)>(GetSymbol(pulse_handle, #x)); \
+ p##x = al::bit_cast<decltype(p##x)>(GetSymbol(pulse_handle, #x)); \
if(!(p##x)) { \
ret = false; \
missing_funcs += "\n" #x; \
diff --git a/alc/backends/wasapi.cpp b/alc/backends/wasapi.cpp
index e66656ce..ea6ecbe0 100644
--- a/alc/backends/wasapi.cpp
+++ b/alc/backends/wasapi.cpp
@@ -207,7 +207,7 @@ NameGUIDPair get_device_name_and_guid(IMMDevice *device)
}
PropVariant pvprop;
- hr = ps->GetValue(reinterpret_cast<const PROPERTYKEY&>(DEVPKEY_Device_FriendlyName), pvprop.get());
+ hr = ps->GetValue(al::bit_cast<PROPERTYKEY>(DEVPKEY_Device_FriendlyName), pvprop.get());
if(FAILED(hr))
{
WARN("GetValue Device_FriendlyName failed: 0x%08lx\n", hr);
@@ -222,7 +222,7 @@ NameGUIDPair get_device_name_and_guid(IMMDevice *device)
}
pvprop.clear();
- hr = ps->GetValue(reinterpret_cast<const PROPERTYKEY&>(PKEY_AudioEndpoint_GUID), pvprop.get());
+ hr = ps->GetValue(al::bit_cast<PROPERTYKEY>(PKEY_AudioEndpoint_GUID), pvprop.get());
if(FAILED(hr))
{
WARN("GetValue AudioEndpoint_GUID failed: 0x%08lx\n", hr);
diff --git a/common/albit.h b/common/albit.h
index ad596208..a563a4e7 100644
--- a/common/albit.h
+++ b/common/albit.h
@@ -2,6 +2,7 @@
#define AL_BIT_H
#include <cstdint>
+#include <cstring>
#include <limits>
#include <type_traits>
#if !defined(__GNUC__) && (defined(_WIN32) || defined(_WIN64))
@@ -10,6 +11,16 @@
namespace al {
+template<typename To, typename From>
+std::enable_if_t<sizeof(To) == sizeof(From) && std::is_trivially_copyable_v<From>
+ && std::is_trivially_copyable_v<To>,
+To> bit_cast(const From &src) noexcept
+{
+ union { char c; To dst; } u;
+ std::memcpy(&u.dst, &src, sizeof(To));
+ return u.dst;
+}
+
#ifdef __BYTE_ORDER__
enum class endian {
little = __ORDER_LITTLE_ENDIAN__,
diff --git a/common/dynload.cpp b/common/dynload.cpp
index f1c2a7eb..86c36e00 100644
--- a/common/dynload.cpp
+++ b/common/dynload.cpp
@@ -3,6 +3,7 @@
#include "dynload.h"
+#include "albit.h"
#include "strutils.h"
#ifdef _WIN32
@@ -17,7 +18,7 @@ void *LoadLib(const char *name)
void CloseLib(void *handle)
{ FreeLibrary(static_cast<HMODULE>(handle)); }
void *GetSymbol(void *handle, const char *name)
-{ return reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name)); }
+{ return al::bit_cast<void*>(GetProcAddress(static_cast<HMODULE>(handle), name)); }
#elif defined(HAVE_DLFCN_H)
diff --git a/core/dbus_wrap.cpp b/core/dbus_wrap.cpp
index 7f221706..eaddce9f 100644
--- a/core/dbus_wrap.cpp
+++ b/core/dbus_wrap.cpp
@@ -8,6 +8,7 @@
#include <mutex>
#include <type_traits>
+#include "albit.h"
#include "logging.h"
@@ -21,7 +22,7 @@ void PrepareDBus()
static constexpr char libname[] = "libdbus-1.so.3";
auto load_func = [](auto &f, const char *name) -> void
- { f = reinterpret_cast<std::remove_reference_t<decltype(f)>>(GetSymbol(dbus_handle, name)); };
+ { f = al::bit_cast<std::remove_reference_t<decltype(f)>>(GetSymbol(dbus_handle, name)); };
#define LOAD_FUNC(x) do { \
load_func(p##x, #x); \
if(!p##x) \