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