From 989f39ef325385301ed9f3e7d45e55573ee7ef7b Mon Sep 17 00:00:00 2001 From: Michael Stefaniuc Date: Tue, 2 Aug 2011 10:28:16 +0200 Subject: [PATCH] dmloader: Simplify the module refcount handling. --- dlls/dmloader/container.c | 9 +++------ dlls/dmloader/dmloader_main.c | 21 ++++++++------------- dlls/dmloader/dmloader_private.h | 5 +++-- dlls/dmloader/loader.c | 11 ++++------- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/dlls/dmloader/container.c b/dlls/dmloader/container.c index a267816cb62..3bee753d21f 100644 --- a/dlls/dmloader/container.c +++ b/dlls/dmloader/container.c @@ -68,9 +68,7 @@ static HRESULT DMUSIC_DestroyDirectMusicContainerImpl (LPDIRECTMUSICCONTAINER if IStream_Release (This->pStream); /* FIXME: release allocated entries */ - - /* decrease number of instances */ - InterlockedDecrement (&dwDirectMusicContainer); + unlock_module(); return S_OK; } @@ -924,8 +922,7 @@ HRESULT WINAPI DMUSIC_CreateDirectMusicContainerImpl (LPCGUID lpcGUID, LPVOID* p obj->pContainedObjects = HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, sizeof(struct list)); list_init (obj->pContainedObjects); - /* increase number of instances */ - InterlockedIncrement (&dwDirectMusicContainer); - + lock_module(); + return IDirectMusicContainerImpl_IDirectMusicContainer_QueryInterface ((LPDIRECTMUSICCONTAINER)&obj->ContainerVtbl, lpcGUID, ppobj); } diff --git a/dlls/dmloader/dmloader_main.c b/dlls/dmloader/dmloader_main.c index 80e115e7f66..95a3936adec 100644 --- a/dlls/dmloader/dmloader_main.c +++ b/dlls/dmloader/dmloader_main.c @@ -23,8 +23,7 @@ WINE_DEFAULT_DEBUG_CHANNEL(dmloader); static HINSTANCE instance; -LONG dwDirectMusicContainer = 0; -LONG dwDirectMusicLoader = 0; +LONG module_ref = 0; typedef struct { IClassFactory IClassFactory_iface; @@ -61,14 +60,14 @@ static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID r static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface) { - InterlockedIncrement(&dwDirectMusicLoader); + lock_module(); return 2; /* non-heap based object */ } static ULONG WINAPI ClassFactory_Release(IClassFactory *iface) { - InterlockedDecrement(&dwDirectMusicLoader); + unlock_module(); return 1; /* non-heap based object */ } @@ -88,9 +87,9 @@ static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock) TRACE("(%d)\n", dolock); if (dolock) - InterlockedIncrement(&dwDirectMusicLoader); + lock_module(); else - InterlockedDecrement(&dwDirectMusicLoader); + unlock_module(); return S_OK; } @@ -127,14 +126,10 @@ BOOL WINAPI DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { */ HRESULT WINAPI DllCanUnloadNow (void) { - TRACE("(void)\n"); - /* if there are no instances left, it's safe to release */ - if (!dwDirectMusicContainer && !dwDirectMusicLoader) - return S_OK; - else - return S_FALSE; -} + TRACE("() ref=%d\n", module_ref); + return module_ref ? S_FALSE : S_OK; +} /****************************************************************** * DllGetClassObject (DMLOADER.@) diff --git a/dlls/dmloader/dmloader_private.h b/dlls/dmloader/dmloader_private.h index d8e520b86c7..a6de708cdaa 100644 --- a/dlls/dmloader/dmloader_private.h +++ b/dlls/dmloader/dmloader_private.h @@ -45,8 +45,9 @@ #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field)) /* dmloader.dll global (for DllCanUnloadNow) */ -extern LONG dwDirectMusicLoader DECLSPEC_HIDDEN; /* number of DirectMusicLoader(CF) instances */ -extern LONG dwDirectMusicContainer DECLSPEC_HIDDEN; /* number of DirectMusicContainer(CF) instances */ +extern LONG module_ref DECLSPEC_HIDDEN; +static inline void lock_module(void) { InterlockedIncrement( &module_ref ); } +static inline void unlock_module(void) { InterlockedDecrement( &module_ref ); } /***************************************************************************** * Interfaces diff --git a/dlls/dmloader/loader.c b/dlls/dmloader/loader.c index 0e9748d9e58..e83444d93c3 100644 --- a/dlls/dmloader/loader.c +++ b/dlls/dmloader/loader.c @@ -104,11 +104,9 @@ static ULONG WINAPI IDirectMusicLoaderImpl_IDirectMusicLoader_Release (LPDIRECTM /*This->CritSect.DebugInfo->Spare[0] = 0; DeleteCriticalSection (&This->CritSect); */ HeapFree (GetProcessHeap(), 0, This); - - /* decrease number of instances */ - InterlockedDecrement (&dwDirectMusicLoader); + unlock_module(); } - + return dwRef; } @@ -895,9 +893,8 @@ HRESULT WINAPI DMUSIC_CreateDirectMusicLoaderImpl (LPCGUID lpcGUID, LPVOID *ppob pDefaultDLSEntry->bInvalidDefaultDLS = TRUE; } - /* increase number of instances */ - InterlockedIncrement (&dwDirectMusicLoader); - + lock_module(); + return IDirectMusicLoaderImpl_IDirectMusicLoader_QueryInterface ((LPDIRECTMUSICLOADER8)obj, lpcGUID, ppobj); } -- 2.11.4.GIT