usp10: Fall back to 'dflt' language if shaping language tag isn't found.
[wine.git] / dlls / webservices / heap.c
bloba135cffed6586fc48ba7015dd715fa95adaf50a0
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/list.h"
28 #include "webservices_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(webservices);
32 static const struct prop_desc heap_props[] =
34 { sizeof(SIZE_T), FALSE }, /* WS_HEAP_PROPERTY_MAX_SIZE */
35 { sizeof(SIZE_T), FALSE }, /* WS_HEAP_PROPERTY_TRIM_SIZE */
36 { sizeof(SIZE_T), TRUE }, /* WS_HEAP_PROPERTY_REQUESTED_SIZE */
37 { sizeof(SIZE_T), TRUE } /* WS_HEAP_PROPERTY_ACTUAL_SIZE */
40 struct heap
42 ULONG magic;
43 CRITICAL_SECTION cs;
44 HANDLE handle;
45 SIZE_T max_size;
46 SIZE_T allocated;
47 ULONG prop_count;
48 struct prop prop[sizeof(heap_props)/sizeof(heap_props[0])];
51 #define HEAP_MAGIC (('H' << 24) | ('E' << 16) | ('A' << 8) | 'P')
53 static BOOL ensure_heap( struct heap *heap )
55 SIZE_T size;
56 if (heap->handle) return TRUE;
57 prop_get( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_MAX_SIZE, &size, sizeof(size) );
58 if (!(heap->handle = HeapCreate( 0, 0, 0 ))) return FALSE;
59 heap->max_size = size;
60 heap->allocated = 0;
61 return TRUE;
64 void *ws_alloc( WS_HEAP *handle, SIZE_T size )
66 struct heap *heap = (struct heap *)handle;
67 void *ret = NULL;
69 EnterCriticalSection( &heap->cs );
71 if (heap->magic != HEAP_MAGIC) goto done;
72 if (!ensure_heap( heap ) || size > heap->max_size - heap->allocated) goto done;
73 if ((ret = HeapAlloc( heap->handle, 0, size ))) heap->allocated += size;
75 done:
76 LeaveCriticalSection( &heap->cs );
77 return ret;
80 void *ws_alloc_zero( WS_HEAP *handle, SIZE_T size )
82 struct heap *heap = (struct heap *)handle;
83 void *ret = NULL;
85 EnterCriticalSection( &heap->cs );
87 if (heap->magic != HEAP_MAGIC) goto done;
88 if (!ensure_heap( heap ) || size > heap->max_size - heap->allocated) goto done;
89 if ((ret = HeapAlloc( heap->handle, HEAP_ZERO_MEMORY, size ))) heap->allocated += size;
91 done:
92 LeaveCriticalSection( &heap->cs );
93 return ret;
96 void *ws_realloc( WS_HEAP *handle, void *ptr, SIZE_T old_size, SIZE_T new_size )
98 struct heap *heap = (struct heap *)handle;
99 void *ret = NULL;
101 EnterCriticalSection( &heap->cs );
103 if (heap->magic != HEAP_MAGIC || !ensure_heap( heap )) goto done;
104 if (new_size >= old_size)
106 SIZE_T size = new_size - old_size;
107 if (size > heap->max_size - heap->allocated) goto done;
108 if ((ret = HeapReAlloc( heap->handle, 0, ptr, new_size ))) heap->allocated += size;
110 else
112 SIZE_T size = old_size - new_size;
113 if ((ret = HeapReAlloc( heap->handle, 0, ptr, new_size ))) heap->allocated -= size;
116 done:
117 LeaveCriticalSection( &heap->cs );
118 return ret;
121 void *ws_realloc_zero( WS_HEAP *handle, void *ptr, SIZE_T old_size, SIZE_T new_size )
123 struct heap *heap = (struct heap *)handle;
124 void *ret = NULL;
126 EnterCriticalSection( &heap->cs );
128 if (heap->magic != HEAP_MAGIC || !ensure_heap( heap )) goto done;
129 if (new_size >= old_size)
131 SIZE_T size = new_size - old_size;
132 if (size > heap->max_size - heap->allocated) goto done;
133 if ((ret = HeapReAlloc( heap->handle, HEAP_ZERO_MEMORY, ptr, new_size ))) heap->allocated += size;
135 else
137 SIZE_T size = old_size - new_size;
138 if ((ret = HeapReAlloc( heap->handle, HEAP_ZERO_MEMORY, ptr, new_size ))) heap->allocated -= size;
141 done:
142 LeaveCriticalSection( &heap->cs );
143 return ret;
146 void ws_free( WS_HEAP *handle, void *ptr, SIZE_T size )
148 struct heap *heap = (struct heap *)handle;
150 EnterCriticalSection( &heap->cs );
152 if (heap->magic == HEAP_MAGIC)
154 HeapFree( heap->handle, 0, ptr );
155 heap->allocated -= size;
158 LeaveCriticalSection( &heap->cs );
161 /**************************************************************************
162 * WsAlloc [webservices.@]
164 HRESULT WINAPI WsAlloc( WS_HEAP *handle, SIZE_T size, void **ptr, WS_ERROR *error )
166 void *mem;
168 TRACE( "%p %u %p %p\n", handle, (ULONG)size, ptr, error );
169 if (error) FIXME( "ignoring error parameter\n" );
171 if (!handle || !ptr) return E_INVALIDARG;
172 if (!(mem = ws_alloc( handle, size ))) return WS_E_QUOTA_EXCEEDED;
173 *ptr = mem;
174 return S_OK;
177 static struct heap *alloc_heap(void)
179 static const ULONG count = sizeof(heap_props)/sizeof(heap_props[0]);
180 struct heap *ret;
181 ULONG size = sizeof(*ret) + prop_size( heap_props, count );
183 if (!(ret = heap_alloc_zero( size ))) return NULL;
185 ret->magic = HEAP_MAGIC;
186 InitializeCriticalSection( &ret->cs );
187 ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": heap.cs");
189 prop_init( heap_props, count, ret->prop, &ret[1] );
190 ret->prop_count = count;
191 return ret;
194 /**************************************************************************
195 * WsCreateHeap [webservices.@]
197 HRESULT WINAPI WsCreateHeap( SIZE_T max_size, SIZE_T trim_size, const WS_HEAP_PROPERTY *properties,
198 ULONG count, WS_HEAP **handle, WS_ERROR *error )
200 struct heap *heap;
202 TRACE( "%u %u %p %u %p %p\n", (ULONG)max_size, (ULONG)trim_size, properties, count, handle, error );
203 if (error) FIXME( "ignoring error parameter\n" );
205 if (!handle || count) return E_INVALIDARG;
206 if (!(heap = alloc_heap())) return E_OUTOFMEMORY;
208 prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_MAX_SIZE, &max_size, sizeof(max_size) );
209 prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_TRIM_SIZE, &trim_size, sizeof(trim_size) );
211 *handle = (WS_HEAP *)heap;
212 return S_OK;
215 static void reset_heap( struct heap *heap )
217 HeapDestroy( heap->handle );
218 heap->handle = NULL;
219 heap->max_size = heap->allocated = 0;
222 /**************************************************************************
223 * WsFreeHeap [webservices.@]
225 void WINAPI WsFreeHeap( WS_HEAP *handle )
227 struct heap *heap = (struct heap *)handle;
229 TRACE( "%p\n", handle );
231 if (!heap) return;
233 EnterCriticalSection( &heap->cs );
235 if (heap->magic != HEAP_MAGIC)
237 LeaveCriticalSection( &heap->cs );
238 return;
241 reset_heap( heap );
242 heap->magic = 0;
244 LeaveCriticalSection( &heap->cs );
246 heap->cs.DebugInfo->Spare[0] = 0;
247 DeleteCriticalSection( &heap->cs );
248 heap_free( heap );
251 /**************************************************************************
252 * WsResetHeap [webservices.@]
254 HRESULT WINAPI WsResetHeap( WS_HEAP *handle, WS_ERROR *error )
256 struct heap *heap = (struct heap *)handle;
258 TRACE( "%p %p\n", handle, error );
259 if (error) FIXME( "ignoring error parameter\n" );
261 if (!heap) return E_INVALIDARG;
263 EnterCriticalSection( &heap->cs );
265 if (heap->magic != HEAP_MAGIC)
267 LeaveCriticalSection( &heap->cs );
268 return E_INVALIDARG;
271 reset_heap( heap );
273 LeaveCriticalSection( &heap->cs );
274 return S_OK;
277 /**************************************************************************
278 * WsGetHeapProperty [webservices.@]
280 HRESULT WINAPI WsGetHeapProperty( WS_HEAP *handle, WS_HEAP_PROPERTY_ID id, void *buf,
281 ULONG size, WS_ERROR *error )
283 struct heap *heap = (struct heap *)handle;
284 HRESULT hr = S_OK;
286 TRACE( "%p %u %p %u %p\n", handle, id, buf, size, error );
287 if (error) FIXME( "ignoring error parameter\n" );
289 if (!heap) return E_INVALIDARG;
291 EnterCriticalSection( &heap->cs );
293 if (heap->magic != HEAP_MAGIC)
295 LeaveCriticalSection( &heap->cs );
296 return E_INVALIDARG;
299 switch (id)
301 case WS_HEAP_PROPERTY_REQUESTED_SIZE:
302 case WS_HEAP_PROPERTY_ACTUAL_SIZE:
304 SIZE_T *heap_size = buf;
305 if (!buf || size != sizeof(heap_size)) hr = E_INVALIDARG;
306 else *heap_size = heap->allocated;
307 break;
309 default:
310 hr = prop_get( heap->prop, heap->prop_count, id, buf, size );
313 LeaveCriticalSection( &heap->cs );
314 return hr;
317 #define XML_BUFFER_INITIAL_ALLOCATED_SIZE 256
318 struct xmlbuf *alloc_xmlbuf( WS_HEAP *heap, WS_XML_WRITER_ENCODING_TYPE encoding, WS_CHARSET charset )
320 struct xmlbuf *ret;
322 if (!(ret = ws_alloc( heap, sizeof(*ret) ))) return NULL;
323 if (!(ret->bytes.bytes = ws_alloc( heap, XML_BUFFER_INITIAL_ALLOCATED_SIZE )))
325 ws_free( heap, ret, sizeof(*ret) );
326 return NULL;
328 ret->heap = heap;
329 ret->size = XML_BUFFER_INITIAL_ALLOCATED_SIZE;
330 ret->bytes.length = 0;
331 ret->encoding = encoding;
332 ret->charset = charset;
333 return ret;
336 void free_xmlbuf( struct xmlbuf *xmlbuf )
338 if (!xmlbuf) return;
339 ws_free( xmlbuf->heap, xmlbuf->bytes.bytes, xmlbuf->size );
340 ws_free( xmlbuf->heap, xmlbuf, sizeof(*xmlbuf) );
343 /**************************************************************************
344 * WsCreateXmlBuffer [webservices.@]
346 HRESULT WINAPI WsCreateXmlBuffer( WS_HEAP *heap, const WS_XML_BUFFER_PROPERTY *properties,
347 ULONG count, WS_XML_BUFFER **handle, WS_ERROR *error )
349 struct xmlbuf *xmlbuf;
351 if (!heap || !handle) return E_INVALIDARG;
352 if (count) FIXME( "properties not implemented\n" );
354 if (!(xmlbuf = alloc_xmlbuf( heap, WS_XML_WRITER_ENCODING_TYPE_TEXT, WS_CHARSET_UTF8 )))
356 return WS_E_QUOTA_EXCEEDED;
359 *handle = (WS_XML_BUFFER *)xmlbuf;
360 return S_OK;