user32: Remove some misleading TODOs.
[wine.git] / dlls / webservices / heap.c
blob5e1a2733292b797f1c4d0435ad2d4a6ff170da76
1 /*
2 * Copyright 2015, 2016 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "winnls.h"
24 #include "webservices.h"
26 #include "wine/debug.h"
27 #include "wine/heap.h"
28 #include "wine/list.h"
29 #include "webservices_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
33 static const struct prop_desc heap_props[] =
35 { sizeof(SIZE_T), FALSE }, /* WS_HEAP_PROPERTY_MAX_SIZE */
36 { sizeof(SIZE_T), FALSE }, /* WS_HEAP_PROPERTY_TRIM_SIZE */
37 { sizeof(SIZE_T), TRUE }, /* WS_HEAP_PROPERTY_REQUESTED_SIZE */
38 { sizeof(SIZE_T), TRUE } /* WS_HEAP_PROPERTY_ACTUAL_SIZE */
41 struct heap
43 ULONG magic;
44 CRITICAL_SECTION cs;
45 HANDLE handle;
46 SIZE_T max_size;
47 SIZE_T allocated;
48 ULONG prop_count;
49 struct prop prop[sizeof(heap_props)/sizeof(heap_props[0])];
52 #define HEAP_MAGIC (('H' << 24) | ('E' << 16) | ('A' << 8) | 'P')
54 static BOOL ensure_heap( struct heap *heap )
56 SIZE_T size;
57 if (heap->handle) return TRUE;
58 prop_get( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_MAX_SIZE, &size, sizeof(size) );
59 if (!(heap->handle = HeapCreate( 0, 0, 0 ))) return FALSE;
60 heap->max_size = size;
61 heap->allocated = 0;
62 return TRUE;
65 void *ws_alloc( WS_HEAP *handle, SIZE_T size )
67 struct heap *heap = (struct heap *)handle;
68 void *ret = NULL;
70 EnterCriticalSection( &heap->cs );
72 if (heap->magic != HEAP_MAGIC) goto done;
73 if (!ensure_heap( heap ) || size > heap->max_size - heap->allocated) goto done;
74 if ((ret = HeapAlloc( heap->handle, 0, size ))) heap->allocated += size;
76 done:
77 LeaveCriticalSection( &heap->cs );
78 return ret;
81 void *ws_alloc_zero( WS_HEAP *handle, SIZE_T size )
83 struct heap *heap = (struct heap *)handle;
84 void *ret = NULL;
86 EnterCriticalSection( &heap->cs );
88 if (heap->magic != HEAP_MAGIC) goto done;
89 if (!ensure_heap( heap ) || size > heap->max_size - heap->allocated) goto done;
90 if ((ret = HeapAlloc( heap->handle, HEAP_ZERO_MEMORY, size ))) heap->allocated += size;
92 done:
93 LeaveCriticalSection( &heap->cs );
94 return ret;
97 void *ws_realloc( WS_HEAP *handle, void *ptr, SIZE_T old_size, SIZE_T new_size )
99 struct heap *heap = (struct heap *)handle;
100 void *ret = NULL;
102 EnterCriticalSection( &heap->cs );
104 if (heap->magic != HEAP_MAGIC || !ensure_heap( heap )) goto done;
105 if (new_size >= old_size)
107 SIZE_T size = new_size - old_size;
108 if (size > heap->max_size - heap->allocated) goto done;
109 if ((ret = HeapReAlloc( heap->handle, 0, ptr, new_size ))) heap->allocated += size;
111 else
113 SIZE_T size = old_size - new_size;
114 if ((ret = HeapReAlloc( heap->handle, 0, ptr, new_size ))) heap->allocated -= size;
117 done:
118 LeaveCriticalSection( &heap->cs );
119 return ret;
122 void *ws_realloc_zero( WS_HEAP *handle, void *ptr, SIZE_T old_size, SIZE_T new_size )
124 struct heap *heap = (struct heap *)handle;
125 void *ret = NULL;
127 EnterCriticalSection( &heap->cs );
129 if (heap->magic != HEAP_MAGIC || !ensure_heap( heap )) goto done;
130 if (new_size >= old_size)
132 SIZE_T size = new_size - old_size;
133 if (size > heap->max_size - heap->allocated) goto done;
134 if ((ret = HeapReAlloc( heap->handle, HEAP_ZERO_MEMORY, ptr, new_size ))) heap->allocated += size;
136 else
138 SIZE_T size = old_size - new_size;
139 if ((ret = HeapReAlloc( heap->handle, HEAP_ZERO_MEMORY, ptr, new_size ))) heap->allocated -= size;
142 done:
143 LeaveCriticalSection( &heap->cs );
144 return ret;
147 void ws_free( WS_HEAP *handle, void *ptr, SIZE_T size )
149 struct heap *heap = (struct heap *)handle;
151 EnterCriticalSection( &heap->cs );
153 if (heap->magic == HEAP_MAGIC)
155 HeapFree( heap->handle, 0, ptr );
156 heap->allocated -= size;
159 LeaveCriticalSection( &heap->cs );
162 /**************************************************************************
163 * WsAlloc [webservices.@]
165 HRESULT WINAPI WsAlloc( WS_HEAP *handle, SIZE_T size, void **ptr, WS_ERROR *error )
167 void *mem;
169 TRACE( "%p %u %p %p\n", handle, (ULONG)size, ptr, error );
170 if (error) FIXME( "ignoring error parameter\n" );
172 if (!handle || !ptr) return E_INVALIDARG;
173 if (!(mem = ws_alloc( handle, size ))) return WS_E_QUOTA_EXCEEDED;
174 *ptr = mem;
175 return S_OK;
178 static struct heap *alloc_heap(void)
180 static const ULONG count = sizeof(heap_props)/sizeof(heap_props[0]);
181 struct heap *ret;
182 ULONG size = sizeof(*ret) + prop_size( heap_props, count );
184 if (!(ret = heap_alloc_zero( size ))) return NULL;
186 ret->magic = HEAP_MAGIC;
187 InitializeCriticalSection( &ret->cs );
188 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": heap.cs");
190 prop_init( heap_props, count, ret->prop, &ret[1] );
191 ret->prop_count = count;
192 return ret;
195 /**************************************************************************
196 * WsCreateHeap [webservices.@]
198 HRESULT WINAPI WsCreateHeap( SIZE_T max_size, SIZE_T trim_size, const WS_HEAP_PROPERTY *properties,
199 ULONG count, WS_HEAP **handle, WS_ERROR *error )
201 struct heap *heap;
203 TRACE( "%u %u %p %u %p %p\n", (ULONG)max_size, (ULONG)trim_size, properties, count, handle, error );
204 if (error) FIXME( "ignoring error parameter\n" );
206 if (!handle || count) return E_INVALIDARG;
207 if (!(heap = alloc_heap())) return E_OUTOFMEMORY;
209 prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_MAX_SIZE, &max_size, sizeof(max_size) );
210 prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_TRIM_SIZE, &trim_size, sizeof(trim_size) );
212 TRACE( "created %p\n", heap );
213 *handle = (WS_HEAP *)heap;
214 return S_OK;
217 static void reset_heap( struct heap *heap )
219 HeapDestroy( heap->handle );
220 heap->handle = NULL;
221 heap->max_size = heap->allocated = 0;
224 /**************************************************************************
225 * WsFreeHeap [webservices.@]
227 void WINAPI WsFreeHeap( WS_HEAP *handle )
229 struct heap *heap = (struct heap *)handle;
231 TRACE( "%p\n", handle );
233 if (!heap) return;
235 EnterCriticalSection( &heap->cs );
237 if (heap->magic != HEAP_MAGIC)
239 LeaveCriticalSection( &heap->cs );
240 return;
243 reset_heap( heap );
244 heap->magic = 0;
246 LeaveCriticalSection( &heap->cs );
248 heap->cs.DebugInfo->Spare[0] = 0;
249 DeleteCriticalSection( &heap->cs );
250 heap_free( heap );
253 /**************************************************************************
254 * WsResetHeap [webservices.@]
256 HRESULT WINAPI WsResetHeap( WS_HEAP *handle, WS_ERROR *error )
258 struct heap *heap = (struct heap *)handle;
260 TRACE( "%p %p\n", handle, error );
261 if (error) FIXME( "ignoring error parameter\n" );
263 if (!heap) return E_INVALIDARG;
265 EnterCriticalSection( &heap->cs );
267 if (heap->magic != HEAP_MAGIC)
269 LeaveCriticalSection( &heap->cs );
270 return E_INVALIDARG;
273 reset_heap( heap );
275 LeaveCriticalSection( &heap->cs );
276 return S_OK;
279 /**************************************************************************
280 * WsGetHeapProperty [webservices.@]
282 HRESULT WINAPI WsGetHeapProperty( WS_HEAP *handle, WS_HEAP_PROPERTY_ID id, void *buf,
283 ULONG size, WS_ERROR *error )
285 struct heap *heap = (struct heap *)handle;
286 HRESULT hr = S_OK;
288 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
289 if (error) FIXME( "ignoring error parameter\n" );
291 if (!heap) return E_INVALIDARG;
293 EnterCriticalSection( &heap->cs );
295 if (heap->magic != HEAP_MAGIC)
297 LeaveCriticalSection( &heap->cs );
298 return E_INVALIDARG;
301 switch (id)
303 case WS_HEAP_PROPERTY_REQUESTED_SIZE:
304 case WS_HEAP_PROPERTY_ACTUAL_SIZE:
306 SIZE_T *heap_size = buf;
307 if (!buf || size != sizeof(heap_size)) hr = E_INVALIDARG;
308 else *heap_size = heap->allocated;
309 break;
311 default:
312 hr = prop_get( heap->prop, heap->prop_count, id, buf, size );
315 LeaveCriticalSection( &heap->cs );
316 return hr;
319 #define XML_BUFFER_INITIAL_ALLOCATED_SIZE 256
320 struct xmlbuf *alloc_xmlbuf( WS_HEAP *heap, SIZE_T size, WS_XML_WRITER_ENCODING_TYPE encoding, WS_CHARSET charset,
321 const WS_XML_DICTIONARY *dict_static, WS_XML_DICTIONARY *dict )
323 struct xmlbuf *ret;
325 if (!size) size = XML_BUFFER_INITIAL_ALLOCATED_SIZE;
326 if (!(ret = ws_alloc( heap, sizeof(*ret) ))) return NULL;
327 if (!(ret->bytes.bytes = ws_alloc( heap, size )))
329 ws_free( heap, ret, sizeof(*ret) );
330 return NULL;
332 ret->heap = heap;
333 ret->size = size;
334 ret->bytes.length = 0;
335 ret->encoding = encoding;
336 ret->charset = charset;
337 ret->dict_static = dict_static;
338 ret->dict = dict;
339 return ret;
342 void free_xmlbuf( struct xmlbuf *xmlbuf )
344 if (!xmlbuf) return;
345 ws_free( xmlbuf->heap, xmlbuf->bytes.bytes, xmlbuf->size );
346 ws_free( xmlbuf->heap, xmlbuf, sizeof(*xmlbuf) );
349 /**************************************************************************
350 * WsCreateXmlBuffer [webservices.@]
352 HRESULT WINAPI WsCreateXmlBuffer( WS_HEAP *heap, const WS_XML_BUFFER_PROPERTY *properties,
353 ULONG count, WS_XML_BUFFER **handle, WS_ERROR *error )
355 struct xmlbuf *xmlbuf;
357 TRACE( "%p %p %u %p %p\n", heap, properties, count, handle, error );
358 if (error) FIXME( "ignoring error parameter\n" );
360 if (!heap || !handle) return E_INVALIDARG;
361 if (count) FIXME( "properties not implemented\n" );
363 if (!(xmlbuf = alloc_xmlbuf( heap, 0, WS_XML_WRITER_ENCODING_TYPE_TEXT, WS_CHARSET_UTF8, NULL, NULL )))
365 return WS_E_QUOTA_EXCEEDED;
368 TRACE( "created %p\n", xmlbuf );
369 *handle = (WS_XML_BUFFER *)xmlbuf;
370 return S_OK;