From e8cafa57999a69b2967e7f30aed4ba492012944e Mon Sep 17 00:00:00 2001 From: James Hawkins Date: Wed, 26 Jan 2005 19:41:43 +0000 Subject: [PATCH] Properly implement DllCanUnloadNow ref counting. --- dlls/dmusic/buffer.c | 5 +++ dlls/dmusic/clock.c | 5 +++ dlls/dmusic/collection.c | 5 +++ dlls/dmusic/dmusic.c | 5 +++ dlls/dmusic/dmusic_main.c | 69 ++++++++++++++++++++++---------------- dlls/dmusic/dmusic_private.h | 6 ++++ dlls/dmusic/download.c | 5 +++ dlls/dmusic/downloadedinstrument.c | 5 +++ dlls/dmusic/instrument.c | 5 +++ dlls/dmusic/port.c | 5 +++ dlls/dmusic/portdownload.c | 5 +++ dlls/dmusic/thru.c | 5 +++ 12 files changed, 97 insertions(+), 28 deletions(-) diff --git a/dlls/dmusic/buffer.c b/dlls/dmusic/buffer.c index b60248435a8..a7164858f5e 100644 --- a/dlls/dmusic/buffer.c +++ b/dlls/dmusic/buffer.c @@ -41,6 +41,8 @@ ULONG WINAPI IDirectMusicBufferImpl_AddRef (LPDIRECTMUSICBUFFER iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -53,6 +55,9 @@ ULONG WINAPI IDirectMusicBufferImpl_Release (LPDIRECTMUSICBUFFER iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/clock.c b/dlls/dmusic/clock.c index ab7789f7370..fa44f100567 100644 --- a/dlls/dmusic/clock.c +++ b/dlls/dmusic/clock.c @@ -42,6 +42,8 @@ ULONG WINAPI IReferenceClockImpl_AddRef (IReferenceClock *iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -54,6 +56,9 @@ ULONG WINAPI IReferenceClockImpl_Release (IReferenceClock *iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/collection.c b/dlls/dmusic/collection.c index d4989a1319b..3b1bd9c7bd9 100644 --- a/dlls/dmusic/collection.c +++ b/dlls/dmusic/collection.c @@ -58,6 +58,8 @@ ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_AddRef (LPUNKNOWN iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -70,6 +72,9 @@ ULONG WINAPI IDirectMusicCollectionImpl_IUnknown_Release (LPUNKNOWN iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/dmusic.c b/dlls/dmusic/dmusic.c index 9cdde015426..b93f0bd20b4 100644 --- a/dlls/dmusic/dmusic.c +++ b/dlls/dmusic/dmusic.c @@ -44,6 +44,8 @@ ULONG WINAPI IDirectMusic8Impl_AddRef (LPDIRECTMUSIC8 iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -56,6 +58,9 @@ ULONG WINAPI IDirectMusic8Impl_Release (LPDIRECTMUSIC8 iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/dmusic_main.c b/dlls/dmusic/dmusic_main.c index b06f9608a07..dc1544ea56c 100644 --- a/dlls/dmusic/dmusic_main.c +++ b/dlls/dmusic/dmusic_main.c @@ -23,41 +23,48 @@ WINE_DEFAULT_DEBUG_CHANNEL(dmusic); +LONG DMUSIC_refCount = 0; + typedef struct { - /* IUnknown fields */ IClassFactoryVtbl *lpVtbl; - DWORD ref; } IClassFactoryImpl; /****************************************************************** * DirectMusic ClassFactory */ static HRESULT WINAPI DirectMusicCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - FIXME("(%p, %s, %p): stub\n",This,debugstr_dmguid(riid),ppobj); + FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid)); + + if (ppobj == NULL) return E_POINTER; + return E_NOINTERFACE; } static ULONG WINAPI DirectMusicCF_AddRef(LPCLASSFACTORY iface) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - return InterlockedIncrement(&This->ref); + DMUSIC_LockModule(); + + return 2; /* non-heap based object */ } static ULONG WINAPI DirectMusicCF_Release(LPCLASSFACTORY iface) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - /* static class, won't be freed */ - return InterlockedDecrement(&This->ref); + DMUSIC_UnlockModule(); + + return 1; /* non-heap based object */ } static HRESULT WINAPI DirectMusicCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - TRACE ("(%p, %p, %s, %p)\n", This, pOuter, debugstr_dmguid(riid), ppobj); + TRACE ("(%p, %s, %p)\n", pOuter, debugstr_dmguid(riid), ppobj); return DMUSIC_CreateDirectMusicImpl (riid, (LPVOID*) ppobj, pOuter); } static HRESULT WINAPI DirectMusicCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - FIXME("(%p, %d): stub\n", This, dolock); + TRACE("(%d)\n", dolock); + + if (dolock) + DMUSIC_LockModule(); + else + DMUSIC_UnlockModule(); + return S_OK; } @@ -69,37 +76,44 @@ static IClassFactoryVtbl DirectMusicCF_Vtbl = { DirectMusicCF_LockServer }; -static IClassFactoryImpl DirectMusic_CF = {&DirectMusicCF_Vtbl, 1 }; +static IClassFactoryImpl DirectMusic_CF = {&DirectMusicCF_Vtbl}; /****************************************************************** * DirectMusicCollection ClassFactory */ static HRESULT WINAPI CollectionCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - FIXME("(%p, %s, %p): stub\n",This,debugstr_dmguid(riid),ppobj); + FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid)); + + if (ppobj == NULL) return E_POINTER; + return E_NOINTERFACE; } static ULONG WINAPI CollectionCF_AddRef(LPCLASSFACTORY iface) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - return InterlockedIncrement(&This->ref); + DMUSIC_LockModule(); + + return 2; /* non-heap based object */ } static ULONG WINAPI CollectionCF_Release(LPCLASSFACTORY iface) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - /* static class, won't be freed */ - return InterlockedDecrement(&This->ref); + DMUSIC_UnlockModule(); + + return 1; /* non-heap based object */ } static HRESULT WINAPI CollectionCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter, REFIID riid, LPVOID *ppobj) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - TRACE ("(%p, %p, %s, %p)\n", This, pOuter, debugstr_dmguid(riid), ppobj); + TRACE ("(%p, %s, %p)\n", pOuter, debugstr_dmguid(riid), ppobj); return DMUSIC_CreateDirectMusicCollectionImpl (riid, ppobj, pOuter); } static HRESULT WINAPI CollectionCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) { - IClassFactoryImpl *This = (IClassFactoryImpl *)iface; - FIXME("(%p, %d): stub\n", This, dolock); + TRACE("(%d)\n", dolock); + + if (dolock) + DMUSIC_LockModule(); + else + DMUSIC_UnlockModule(); + return S_OK; } @@ -111,7 +125,7 @@ static IClassFactoryVtbl CollectionCF_Vtbl = { CollectionCF_LockServer }; -static IClassFactoryImpl Collection_CF = {&CollectionCF_Vtbl, 1 }; +static IClassFactoryImpl Collection_CF = {&CollectionCF_Vtbl}; /****************************************************************** * DllMain @@ -136,8 +150,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { * */ HRESULT WINAPI DMUSIC_DllCanUnloadNow(void) { - FIXME("(void): stub\n"); - return S_FALSE; + return DMUSIC_refCount != 0 ? S_FALSE : S_OK; } diff --git a/dlls/dmusic/dmusic_private.h b/dlls/dmusic/dmusic_private.h index dbf6498fbdf..c9ebd9df9fb 100644 --- a/dlls/dmusic/dmusic_private.h +++ b/dlls/dmusic/dmusic_private.h @@ -402,6 +402,12 @@ extern HRESULT WINAPI IDirectMusicInstrumentImpl_IDirectMusicInstrument_SetPatch /* custom :) */ extern HRESULT WINAPI IDirectMusicInstrumentImpl_Custom_Load (LPDIRECTMUSICINSTRUMENT iface, LPSTREAM pStm); +/********************************************************************** + * Dll lifetime tracking declaration for dmusic.dll + */ +extern LONG DMUSIC_refCount; +static inline void DMUSIC_LockModule() { InterlockedIncrement( &DMUSIC_refCount ); } +static inline void DMUSIC_UnlockModule() { InterlockedDecrement( &DMUSIC_refCount ); } /***************************************************************************** * Helper Functions diff --git a/dlls/dmusic/download.c b/dlls/dmusic/download.c index 3d67b778826..c6a0a5571fc 100644 --- a/dlls/dmusic/download.c +++ b/dlls/dmusic/download.c @@ -42,6 +42,8 @@ ULONG WINAPI IDirectMusicDownloadImpl_AddRef (LPDIRECTMUSICDOWNLOAD iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -54,6 +56,9 @@ ULONG WINAPI IDirectMusicDownloadImpl_Release (LPDIRECTMUSICDOWNLOAD iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/downloadedinstrument.c b/dlls/dmusic/downloadedinstrument.c index 0cfa0acac3a..7cb6c76cf2c 100644 --- a/dlls/dmusic/downloadedinstrument.c +++ b/dlls/dmusic/downloadedinstrument.c @@ -42,6 +42,8 @@ ULONG WINAPI IDirectMusicDownloadedInstrumentImpl_AddRef (LPDIRECTMUSICDOWNLOADE TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -54,6 +56,9 @@ ULONG WINAPI IDirectMusicDownloadedInstrumentImpl_Release (LPDIRECTMUSICDOWNLOAD if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/instrument.c b/dlls/dmusic/instrument.c index 37f4c7c54ef..e7df0236895 100644 --- a/dlls/dmusic/instrument.c +++ b/dlls/dmusic/instrument.c @@ -58,6 +58,8 @@ ULONG WINAPI IDirectMusicInstrumentImpl_IUnknown_AddRef (LPUNKNOWN iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -70,6 +72,9 @@ ULONG WINAPI IDirectMusicInstrumentImpl_IUnknown_Release (LPUNKNOWN iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/port.c b/dlls/dmusic/port.c index 8d0e503de33..d5c9ad65abb 100644 --- a/dlls/dmusic/port.c +++ b/dlls/dmusic/port.c @@ -41,6 +41,8 @@ ULONG WINAPI IDirectMusicPortImpl_AddRef (LPDIRECTMUSICPORT iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -53,6 +55,9 @@ ULONG WINAPI IDirectMusicPortImpl_Release (LPDIRECTMUSICPORT iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/portdownload.c b/dlls/dmusic/portdownload.c index 2778c397807..94592d10950 100644 --- a/dlls/dmusic/portdownload.c +++ b/dlls/dmusic/portdownload.c @@ -41,6 +41,8 @@ ULONG WINAPI IDirectMusicPortDownloadImpl_AddRef (LPDIRECTMUSICPORTDOWNLOAD ifac TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -53,6 +55,9 @@ ULONG WINAPI IDirectMusicPortDownloadImpl_Release (LPDIRECTMUSICPORTDOWNLOAD ifa if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } diff --git a/dlls/dmusic/thru.c b/dlls/dmusic/thru.c index a07403ebd27..e45ab8b29a6 100644 --- a/dlls/dmusic/thru.c +++ b/dlls/dmusic/thru.c @@ -42,6 +42,8 @@ ULONG WINAPI IDirectMusicThruImpl_AddRef (LPDIRECTMUSICTHRU iface) { TRACE("(%p)->(ref before=%lu)\n", This, refCount - 1); + DMUSIC_LockModule(); + return refCount; } @@ -54,6 +56,9 @@ ULONG WINAPI IDirectMusicThruImpl_Release (LPDIRECTMUSICTHRU iface) { if (!refCount) { HeapFree(GetProcessHeap(), 0, This); } + + DMUSIC_UnlockModule(); + return refCount; } -- 2.11.4.GIT