Synchronize with trunk r58606.
[reactos.git] / dll / win32 / mapi32 / imalloc.c
1 /*
2 * MAPI Default IMalloc implementation
3 *
4 * Copyright 2004 Jon Griffiths
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <stdarg.h>
26
27 #define COBJMACROS
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include <windef.h>
31 #include <winbase.h>
32 //#include "winreg.h"
33 //#include "winuser.h"
34 //#include "winerror.h"
35 //#include "winternl.h"
36 //#include "objbase.h"
37 //#include "shlwapi.h"
38 #include <mapiutil.h>
39 #include "util.h"
40 #include <wine/debug.h>
41
42 WINE_DEFAULT_DEBUG_CHANNEL(mapi);
43
44 static const IMallocVtbl MAPI_IMalloc_vt;
45
46 typedef struct
47 {
48 const IMallocVtbl *lpVtbl;
49 LONG lRef;
50 } MAPI_IMALLOC;
51
52 static MAPI_IMALLOC MAPI_IMalloc = { &MAPI_IMalloc_vt, 0u };
53
54 extern LONG MAPI_ObjectCount; /* In mapi32_main.c */
55
56 /*************************************************************************
57 * MAPIGetDefaultMalloc@0 (MAPI32.59)
58 *
59 * Get the default MAPI IMalloc interface.
60 *
61 * PARAMS
62 * None.
63 *
64 * RETURNS
65 * A pointer to the MAPI default allocator.
66 */
67 LPMALLOC WINAPI MAPIGetDefaultMalloc(void)
68 {
69 TRACE("()\n");
70
71 if (mapiFunctions.MAPIGetDefaultMalloc)
72 return mapiFunctions.MAPIGetDefaultMalloc();
73
74 IMalloc_AddRef((LPMALLOC)&MAPI_IMalloc);
75 return (LPMALLOC)&MAPI_IMalloc;
76 }
77
78 /**************************************************************************
79 * IMAPIMalloc_QueryInterface
80 */
81 static HRESULT WINAPI IMAPIMalloc_fnQueryInterface(LPMALLOC iface, REFIID refiid,
82 LPVOID *ppvObj)
83 {
84 TRACE("(%s,%p)\n", debugstr_guid(refiid), ppvObj);
85
86 if (IsEqualIID(refiid, &IID_IUnknown) ||
87 IsEqualIID(refiid, &IID_IMalloc))
88 {
89 *ppvObj = &MAPI_IMalloc;
90 TRACE("Returning IMalloc (%p)\n", *ppvObj);
91 return S_OK;
92 }
93 TRACE("Returning E_NOINTERFACE\n");
94 return E_NOINTERFACE;
95 }
96
97 /**************************************************************************
98 * IMAPIMalloc_AddRef
99 */
100 static ULONG WINAPI IMAPIMalloc_fnAddRef(LPMALLOC iface)
101 {
102 TRACE("(%p)\n", iface);
103 InterlockedIncrement(&MAPI_ObjectCount);
104 return 1u;
105 }
106
107 /**************************************************************************
108 * IMAPIMalloc_Release
109 */
110 static ULONG WINAPI IMAPIMalloc_fnRelease(LPMALLOC iface)
111 {
112 TRACE("(%p)\n", iface);
113 InterlockedDecrement(&MAPI_ObjectCount);
114 return 1u;
115 }
116
117 /**************************************************************************
118 * IMAPIMalloc_Alloc
119 */
120 static LPVOID WINAPI IMAPIMalloc_fnAlloc(LPMALLOC iface, DWORD cb)
121 {
122 TRACE("(%p)->(%d)\n", iface, cb);
123
124 return LocalAlloc(LMEM_FIXED, cb);
125 }
126
127 /**************************************************************************
128 * IMAPIMalloc_Realloc
129 */
130 static LPVOID WINAPI IMAPIMalloc_fnRealloc(LPMALLOC iface, LPVOID pv, DWORD cb)
131 {
132 TRACE("(%p)->(%p, %d)\n", iface, pv, cb);
133
134 if (!pv)
135 return LocalAlloc(LMEM_FIXED, cb);
136
137 if (cb)
138 return LocalReAlloc(pv, cb, LMEM_MOVEABLE);
139
140 LocalFree(pv);
141 return NULL;
142 }
143
144 /**************************************************************************
145 * IMAPIMalloc_Free
146 */
147 static void WINAPI IMAPIMalloc_fnFree(LPMALLOC iface, LPVOID pv)
148 {
149 TRACE("(%p)->(%p)\n", iface, pv);
150 LocalFree(pv);
151 }
152
153 /**************************************************************************
154 * IMAPIMalloc_GetSize
155 */
156 static DWORD WINAPI IMAPIMalloc_fnGetSize(LPMALLOC iface, LPVOID pv)
157 {
158 TRACE("(%p)->(%p)\n", iface, pv);
159 return LocalSize(pv);
160 }
161
162 /**************************************************************************
163 * IMAPIMalloc_DidAlloc
164 */
165 static INT WINAPI IMAPIMalloc_fnDidAlloc(LPMALLOC iface, LPVOID pv)
166 {
167 TRACE("(%p)->(%p)\n", iface, pv);
168 return -1;
169 }
170
171 /**************************************************************************
172 * IMAPIMalloc_HeapMinimize
173 */
174 static void WINAPI IMAPIMalloc_fnHeapMinimize(LPMALLOC iface)
175 {
176 TRACE("(%p)\n", iface);
177 }
178
179 static const IMallocVtbl MAPI_IMalloc_vt =
180 {
181 IMAPIMalloc_fnQueryInterface,
182 IMAPIMalloc_fnAddRef,
183 IMAPIMalloc_fnRelease,
184 IMAPIMalloc_fnAlloc,
185 IMAPIMalloc_fnRealloc,
186 IMAPIMalloc_fnFree,
187 IMAPIMalloc_fnGetSize,
188 IMAPIMalloc_fnDidAlloc,
189 IMAPIMalloc_fnHeapMinimize
190 };