Use Interlocked* functions in AddRef and Release.
[wine/multimedia.git] / dlls / mapi32 / imalloc.c
blob52ea5a72af354550b2899ced9114f17b9108f1f1
1 /*
2 * MAPI Default IMalloc implementation
4 * Copyright 2004 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winuser.h"
30 #include "winerror.h"
31 #include "winternl.h"
32 #include "objbase.h"
33 #include "shlwapi.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
38 static const IMallocVtbl MAPI_IMalloc_vt;
40 typedef struct
42 const IMallocVtbl *lpVtbl;
43 LONG lRef;
44 } MAPI_IMALLOC;
46 static MAPI_IMALLOC MAPI_IMalloc = { &MAPI_IMalloc_vt, 0u };
48 extern LONG MAPI_ObjectCount; /* In mapi32_main.c */
50 /*************************************************************************
51 * MAPIGetDefaultMalloc@0 (MAPI32.59)
53 * Get the default MAPI IMalloc interface.
55 * PARAMS
56 * None.
58 * RETURNS
59 * A pointer to the MAPI default allocator.
61 LPMALLOC WINAPI MAPIGetDefaultMalloc(void)
63 TRACE("()\n");
65 IMalloc_AddRef((LPMALLOC)&MAPI_IMalloc);
66 return (LPMALLOC)&MAPI_IMalloc;
69 /**************************************************************************
70 * IMAPIMalloc_QueryInterface
72 static HRESULT WINAPI IMAPIMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid,
73 LPVOID *ppvObj)
75 TRACE("(%s,%p)\n", debugstr_guid(refiid), ppvObj);
77 if (IsEqualIID(refiid, &IID_IUnknown) ||
78 IsEqualIID(refiid, &IID_IMalloc))
80 *ppvObj = (LPMALLOC) &MAPI_IMalloc;
81 TRACE("Returning IMalloc (%p)\n", *ppvObj);
82 return S_OK;
84 TRACE("Returning E_NOINTERFACE\n");
85 return E_NOINTERFACE;
88 /**************************************************************************
89 * IMAPIMalloc_AddRef
91 static ULONG WINAPI IMAPIMalloc_fnAddRef(LPMALLOC iface)
93 TRACE("(%p)\n", iface);
94 InterlockedIncrement(&MAPI_ObjectCount);
95 return 1u;
98 /**************************************************************************
99 * IMAPIMalloc_Release
101 static ULONG WINAPI IMAPIMalloc_fnRelease(LPMALLOC iface)
103 TRACE("(%p)\n", iface);
104 InterlockedDecrement(&MAPI_ObjectCount);
105 return 1u;
108 /**************************************************************************
109 * IMAPIMalloc_Alloc
111 static LPVOID WINAPI IMAPIMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
113 TRACE("(%p)->(%ld)\n", iface, cb);
115 return LocalAlloc(GMEM_FIXED, cb);
118 /**************************************************************************
119 * IMAPIMalloc_Realloc
121 static LPVOID WINAPI IMAPIMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
123 TRACE("(%p)->(%p, %ld)\n", iface, pv, cb);
125 if (!pv)
126 return LocalAlloc(GMEM_FIXED, cb);
128 if (cb)
129 return LocalReAlloc((HANDLE) pv, cb, GMEM_MOVEABLE);
131 LocalFree((HANDLE) pv);
132 return NULL;
135 /**************************************************************************
136 * IMAPIMalloc_Free
138 static void WINAPI IMAPIMalloc_fnFree(LPMALLOC iface, LPVOID pv)
140 TRACE("(%p)->(%p)\n", iface, pv);
141 LocalFree((HANDLE) pv);
144 /**************************************************************************
145 * IMAPIMalloc_GetSize
147 static DWORD WINAPI IMAPIMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
149 TRACE("(%p)->(%p)\n", iface, pv);
150 return LocalSize((HANDLE) pv);
153 /**************************************************************************
154 * IMAPIMalloc_DidAlloc
156 static INT WINAPI IMAPIMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
158 TRACE("(%p)->(%p)\n", iface, pv);
159 return -1;
162 /**************************************************************************
163 * IMAPIMalloc_HeapMinimize
165 static void WINAPI IMAPIMalloc_fnHeapMinimize(LPMALLOC iface)
167 TRACE("(%p)\n", iface);
170 static const IMallocVtbl MAPI_IMalloc_vt =
172 IMAPIMalloc_fnQueryInterface,
173 IMAPIMalloc_fnAddRef,
174 IMAPIMalloc_fnRelease,
175 IMAPIMalloc_fnAlloc,
176 IMAPIMalloc_fnRealloc,
177 IMAPIMalloc_fnFree,
178 IMAPIMalloc_fnGetSize,
179 IMAPIMalloc_fnDidAlloc,
180 IMAPIMalloc_fnHeapMinimize