1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "VTableBuilder.h"
11 static HRESULT STDMETHODCALLTYPE
QueryInterfaceThunk(IUnknown
* aThis
,
13 void** aOutInterface
) {
14 void** table
= (void**)aThis
;
15 IUnknown
* real
= (IUnknown
*)table
[1];
16 return real
->lpVtbl
->QueryInterface(real
, aIid
, aOutInterface
);
19 static ULONG STDMETHODCALLTYPE
AddRefThunk(IUnknown
* aThis
) {
20 void** table
= (void**)aThis
;
21 IUnknown
* real
= (IUnknown
*)table
[1];
22 return real
->lpVtbl
->AddRef(real
);
25 static ULONG STDMETHODCALLTYPE
ReleaseThunk(IUnknown
* aThis
) {
26 void** table
= (void**)aThis
;
27 IUnknown
* real
= (IUnknown
*)table
[1];
28 return real
->lpVtbl
->Release(real
);
31 IUnknown
* BuildNullVTable(IUnknown
* aUnk
, uint32_t aVtblSize
) {
38 // We need to allocate slots for two additional pointers: The |lpVtbl|
39 // pointer, as well as |aUnk| which is needed to get |this| right when
40 // something calls through one of the IUnknown thunks.
41 table
= calloc(aVtblSize
+ 2, sizeof(void*));
43 table
[0] = &table
[2]; // |lpVtbl|, points to the first entry of the vtable
44 table
[1] = aUnk
; // |this|
45 // Now the actual vtable entries for IUnknown
46 table
[2] = &QueryInterfaceThunk
;
47 table
[3] = &AddRefThunk
;
48 table
[4] = &ReleaseThunk
;
49 // Remaining entries are NULL thanks to calloc zero-initializing everything.
51 return (IUnknown
*)table
;
54 void DeleteNullVTable(IUnknown
* aUnk
) { free(aUnk
); }