winemenubuilder: Fix encoder method argument.
[wine.git] / dlls / webservices / heap.c
blob1e22a24b3441e55a390c96bb216c87c2c915f9fc
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[ARRAY_SIZE( heap_props )];
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 = ARRAY_SIZE( heap_props );
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 #ifndef __MINGW32__
189 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": heap.cs");
190 #endif
192 prop_init( heap_props, count, ret->prop, &ret[1] );
193 ret->prop_count = count;
194 return ret;
197 /**************************************************************************
198 * WsCreateHeap [webservices.@]
200 HRESULT WINAPI WsCreateHeap( SIZE_T max_size, SIZE_T trim_size, const WS_HEAP_PROPERTY *properties,
201 ULONG count, WS_HEAP **handle, WS_ERROR *error )
203 struct heap *heap;
205 TRACE( "%u %u %p %u %p %p\n", (ULONG)max_size, (ULONG)trim_size, properties, count, handle, error );
206 if (error) FIXME( "ignoring error parameter\n" );
208 if (!handle || count) return E_INVALIDARG;
209 if (!(heap = alloc_heap())) return E_OUTOFMEMORY;
211 prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_MAX_SIZE, &max_size, sizeof(max_size) );
212 prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_TRIM_SIZE, &trim_size, sizeof(trim_size) );
214 TRACE( "created %p\n", heap );
215 *handle = (WS_HEAP *)heap;
216 return S_OK;
219 static void reset_heap( struct heap *heap )
221 if (heap->handle) HeapDestroy( heap->handle );
222 heap->handle = NULL;
223 heap->max_size = heap->allocated = 0;
226 /**************************************************************************
227 * WsFreeHeap [webservices.@]
229 void WINAPI WsFreeHeap( WS_HEAP *handle )
231 struct heap *heap = (struct heap *)handle;
233 TRACE( "%p\n", handle );
235 if (!heap) return;
237 EnterCriticalSection( &heap->cs );
239 if (heap->magic != HEAP_MAGIC)
241 LeaveCriticalSection( &heap->cs );
242 return;
245 reset_heap( heap );
246 heap->magic = 0;
248 LeaveCriticalSection( &heap->cs );
250 #ifndef __MINGW32__
251 heap->cs.DebugInfo->Spare[0] = 0;
252 #endif
253 DeleteCriticalSection( &heap->cs );
254 heap_free( heap );
257 /**************************************************************************
258 * WsResetHeap [webservices.@]
260 HRESULT WINAPI WsResetHeap( WS_HEAP *handle, WS_ERROR *error )
262 struct heap *heap = (struct heap *)handle;
263 HRESULT hr = S_OK;
265 TRACE( "%p %p\n", handle, error );
266 if (error) FIXME( "ignoring error parameter\n" );
268 if (!heap) return E_INVALIDARG;
270 EnterCriticalSection( &heap->cs );
272 if (heap->magic != HEAP_MAGIC)
274 LeaveCriticalSection( &heap->cs );
275 return E_INVALIDARG;
278 reset_heap( heap );
280 LeaveCriticalSection( &heap->cs );
281 TRACE( "returning %08x\n", hr );
282 return hr;
285 /**************************************************************************
286 * WsGetHeapProperty [webservices.@]
288 HRESULT WINAPI WsGetHeapProperty( WS_HEAP *handle, WS_HEAP_PROPERTY_ID id, void *buf,
289 ULONG size, WS_ERROR *error )
291 struct heap *heap = (struct heap *)handle;
292 HRESULT hr = S_OK;
294 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
295 if (error) FIXME( "ignoring error parameter\n" );
297 if (!heap) return E_INVALIDARG;
299 EnterCriticalSection( &heap->cs );
301 if (heap->magic != HEAP_MAGIC)
303 LeaveCriticalSection( &heap->cs );
304 return E_INVALIDARG;
307 switch (id)
309 case WS_HEAP_PROPERTY_REQUESTED_SIZE:
310 case WS_HEAP_PROPERTY_ACTUAL_SIZE:
312 SIZE_T *heap_size = buf;
313 if (!buf || size != sizeof(heap_size)) hr = E_INVALIDARG;
314 else *heap_size = heap->allocated;
315 break;
317 default:
318 hr = prop_get( heap->prop, heap->prop_count, id, buf, size );
321 LeaveCriticalSection( &heap->cs );
322 TRACE( "returning %08x\n", hr );
323 return hr;
326 #define XML_BUFFER_INITIAL_ALLOCATED_SIZE 256
327 struct xmlbuf *alloc_xmlbuf( WS_HEAP *heap, SIZE_T size, WS_XML_WRITER_ENCODING_TYPE encoding, WS_CHARSET charset,
328 const WS_XML_DICTIONARY *dict_static, WS_XML_DICTIONARY *dict )
330 struct xmlbuf *ret;
332 if (!size) size = XML_BUFFER_INITIAL_ALLOCATED_SIZE;
333 if (!(ret = ws_alloc( heap, sizeof(*ret) ))) return NULL;
334 if (!(ret->bytes.bytes = ws_alloc( heap, size )))
336 ws_free( heap, ret, sizeof(*ret) );
337 return NULL;
339 ret->heap = heap;
340 ret->size = size;
341 ret->bytes.length = 0;
342 ret->encoding = encoding;
343 ret->charset = charset;
344 ret->dict_static = dict_static;
345 ret->dict = dict;
346 return ret;
349 void free_xmlbuf( struct xmlbuf *xmlbuf )
351 if (!xmlbuf) return;
352 ws_free( xmlbuf->heap, xmlbuf->bytes.bytes, xmlbuf->size );
353 ws_free( xmlbuf->heap, xmlbuf, sizeof(*xmlbuf) );
356 /**************************************************************************
357 * WsCreateXmlBuffer [webservices.@]
359 HRESULT WINAPI WsCreateXmlBuffer( WS_HEAP *heap, const WS_XML_BUFFER_PROPERTY *properties,
360 ULONG count, WS_XML_BUFFER **handle, WS_ERROR *error )
362 struct xmlbuf *xmlbuf;
364 TRACE( "%p %p %u %p %p\n", heap, properties, count, handle, error );
365 if (error) FIXME( "ignoring error parameter\n" );
367 if (!heap || !handle) return E_INVALIDARG;
368 if (count) FIXME( "properties not implemented\n" );
370 if (!(xmlbuf = alloc_xmlbuf( heap, 0, WS_XML_WRITER_ENCODING_TYPE_TEXT, WS_CHARSET_UTF8, NULL, NULL )))
372 return WS_E_QUOTA_EXCEEDED;
375 TRACE( "created %p\n", xmlbuf );
376 *handle = (WS_XML_BUFFER *)xmlbuf;
377 return S_OK;