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