From 1d266aa8347fadfb0db34ae27df7b728c0ab2171 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 19 Apr 2014 02:38:32 -0700 Subject: [PATCH] Add a GCC-specific STATIC_UPCAST macro that checks the object type The check is compile time, and is functionally identical to the old/alternate version. --- Alc/backends/base.h | 2 +- Alc/midi/base.h | 2 +- OpenAL32/Include/alAuxEffectSlot.h | 2 +- OpenAL32/Include/alMain.h | 9 ++++++++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Alc/backends/base.h b/Alc/backends/base.h index 905c3ea4..d18ca400 100644 --- a/Alc/backends/base.h +++ b/Alc/backends/base.h @@ -61,7 +61,7 @@ DECLARE_THUNK(T, ALCbackend, ALint64, getLatency) \ DECLARE_THUNK(T, ALCbackend, void, lock) \ DECLARE_THUNK(T, ALCbackend, void, unlock) \ static void T##_ALCbackend_Delete(void *ptr) \ -{ T##_Delete(STATIC_UPCAST(T, ALCbackend, ptr)); } \ +{ T##_Delete(STATIC_UPCAST(T, ALCbackend, (ALCbackend*)ptr)); } \ \ DECLARE_ALCBACKEND_VTABLE(T) = { \ T##_ALCbackend_Destruct, \ diff --git a/Alc/midi/base.h b/Alc/midi/base.h index 75332006..42a4c279 100644 --- a/Alc/midi/base.h +++ b/Alc/midi/base.h @@ -99,7 +99,7 @@ DECLARE_THUNK(T, MidiSynth, void, reset) \ DECLARE_THUNK1(T, MidiSynth, void, update, ALCdevice*) \ DECLARE_THUNK2(T, MidiSynth, void, process, ALuint, ALfloatBUFFERSIZE*restrict) \ static void T##_MidiSynth_Delete(void *ptr) \ -{ T##_Delete(STATIC_UPCAST(T, MidiSynth, ptr)); } \ +{ T##_Delete(STATIC_UPCAST(T, MidiSynth, (MidiSynth*)ptr)); } \ \ static const struct MidiSynthVtable T##_MidiSynth_vtable = { \ T##_MidiSynth_Destruct, \ diff --git a/OpenAL32/Include/alAuxEffectSlot.h b/OpenAL32/Include/alAuxEffectSlot.h index 75eb87ca..bc871111 100644 --- a/OpenAL32/Include/alAuxEffectSlot.h +++ b/OpenAL32/Include/alAuxEffectSlot.h @@ -33,7 +33,7 @@ DECLARE_THUNK1(T, ALeffectState, ALboolean, deviceUpdate, ALCdevice*) \ DECLARE_THUNK2(T, ALeffectState, void, update, ALCdevice*, const ALeffectslot*) \ DECLARE_THUNK3(T, ALeffectState, void, process, ALuint, const ALfloat*restrict, ALfloatBUFFERSIZE*restrict) \ static void T##_ALeffectState_Delete(void *ptr) \ -{ return T##_Delete(STATIC_UPCAST(T, ALeffectState, ptr)); } \ +{ return T##_Delete(STATIC_UPCAST(T, ALeffectState, (ALeffectState*)ptr)); } \ \ static const struct ALeffectStateVtable T##_ALeffectState_vtable = { \ T##_ALeffectState_Destruct, \ diff --git a/OpenAL32/Include/alMain.h b/OpenAL32/Include/alMain.h index ad305c4e..8b66e4e7 100644 --- a/OpenAL32/Include/alMain.h +++ b/OpenAL32/Include/alMain.h @@ -348,8 +348,15 @@ static const union { #define DERIVE_FROM_TYPE(t) t t##_parent #define STATIC_CAST(to, obj) (&(obj)->to##_parent) +#ifdef __GNUC__ +#define STATIC_UPCAST(to, from, obj) __extension__({ \ + static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \ + "Invalid upcast object from type"); \ + (to*)((char*)(obj) - offsetof(to, from##_parent)); \ +}) +#else #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent))) - +#endif #define DECLARE_FORWARD(T1, T2, rettype, func) \ rettype T1##_##func(T1 *obj) \