st/nine: Fix leak after ctor failures
[mesa.git] / src / gallium / state_trackers / nine / iunknown.c
1 /*
2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "iunknown.h"
24 #include "util/u_atomic.h"
25 #include "nine_helpers.h"
26
27 #define DBG_CHANNEL DBG_UNKNOWN
28
29 HRESULT
30 NineUnknown_ctor( struct NineUnknown *This,
31 struct NineUnknownParams *pParams )
32 {
33 This->refs = pParams->container ? 0 : 1;
34 This->bind = 0;
35 This->forward = !This->refs;
36 This->container = pParams->container;
37 This->device = pParams->device;
38 if (This->refs && This->device)
39 NineUnknown_AddRef(NineUnknown(This->device));
40
41 This->vtable = pParams->vtable;
42 This->guids = pParams->guids;
43 This->dtor = pParams->dtor;
44
45 return D3D_OK;
46 }
47
48 void
49 NineUnknown_dtor( struct NineUnknown *This )
50 {
51 if (This->refs && This->device) /* Possible only if early exit after a ctor failed */
52 (void) NineUnknown_Release(NineUnknown(This->device));
53 FREE(This);
54 }
55
56 HRESULT NINE_WINAPI
57 NineUnknown_QueryInterface( struct NineUnknown *This,
58 REFIID riid,
59 void **ppvObject )
60 {
61 unsigned i = 0;
62
63 DBG("This=%p riid=%p ppvObject=%p\n", This, riid, ppvObject);
64
65 if (!ppvObject) return E_POINTER;
66
67 do {
68 if (GUID_equal(This->guids[i], riid)) {
69 *ppvObject = This;
70 assert(This->refs);
71 NineUnknown_AddRef(This);
72 return S_OK;
73 }
74 } while (This->guids[++i]);
75
76 *ppvObject = NULL;
77 return E_NOINTERFACE;
78 }
79
80 ULONG NINE_WINAPI
81 NineUnknown_AddRef( struct NineUnknown *This )
82 {
83 ULONG r;
84 if (This->forward)
85 return NineUnknown_AddRef(This->container);
86 else
87 r = p_atomic_inc_return(&This->refs);
88
89 if (r == 1) {
90 if (This->device)
91 NineUnknown_AddRef(NineUnknown(This->device));
92 /* This shouldn't be necessary:
93 if (This->container)
94 NineUnknown_Bind(NineUnknown(This->container)); */
95 }
96 return r;
97 }
98
99 ULONG NINE_WINAPI
100 NineUnknown_Release( struct NineUnknown *This )
101 {
102 if (This->forward)
103 return NineUnknown_Release(This->container);
104
105 ULONG r = p_atomic_dec_return(&This->refs);
106
107 if (r == 0) {
108 if (This->device) {
109 if (NineUnknown_Release(NineUnknown(This->device)) == 0)
110 return r; /* everything's gone */
111 }
112 if (This->container) {
113 /* NineUnknown_Unbind(NineUnknown(This->container)); */
114 } else
115 if (This->bind == 0) {
116 This->dtor(This);
117 }
118 }
119 return r;
120 }
121
122 HRESULT NINE_WINAPI
123 NineUnknown_GetDevice( struct NineUnknown *This,
124 IDirect3DDevice9 **ppDevice )
125 {
126 user_assert(ppDevice, E_POINTER);
127 NineUnknown_AddRef(NineUnknown(This->device));
128 *ppDevice = (IDirect3DDevice9 *)This->device;
129 return D3D_OK;
130 }