heap.c 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2015, 2016 Hans Leidekker for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <stdarg.h>
20
#include <stdlib.h>
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include "windef.h"
#include "winbase.h"
#include "winnls.h"
#include "webservices.h"

#include "wine/debug.h"
#include "wine/list.h"
#include "webservices_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(webservices);

static const struct prop_desc heap_props[] =
{
    { sizeof(SIZE_T), FALSE }, /* WS_HEAP_PROPERTY_MAX_SIZE */
    { sizeof(SIZE_T), FALSE }, /* WS_HEAP_PROPERTY_TRIM_SIZE */
    { sizeof(SIZE_T), TRUE },  /* WS_HEAP_PROPERTY_REQUESTED_SIZE */
    { sizeof(SIZE_T), TRUE }   /* WS_HEAP_PROPERTY_ACTUAL_SIZE */
};

struct heap
{
    ULONG            magic;
    CRITICAL_SECTION cs;
    HANDLE           handle;
    SIZE_T           max_size;
    SIZE_T           allocated;
    ULONG            prop_count;
49
    struct prop      prop[ARRAY_SIZE( heap_props )];
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
};

#define HEAP_MAGIC (('H' << 24) | ('E' << 16) | ('A' << 8) | 'P')

static BOOL ensure_heap( struct heap *heap )
{
    SIZE_T size;
    if (heap->handle) return TRUE;
    prop_get( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_MAX_SIZE, &size, sizeof(size) );
    if (!(heap->handle = HeapCreate( 0, 0, 0 ))) return FALSE;
    heap->max_size  = size;
    heap->allocated = 0;
    return TRUE;
}

void *ws_alloc( WS_HEAP *handle, SIZE_T size )
{
    struct heap *heap = (struct heap *)handle;
    void *ret = NULL;

    EnterCriticalSection( &heap->cs );

    if (heap->magic != HEAP_MAGIC) goto done;
    if (!ensure_heap( heap ) || size > heap->max_size - heap->allocated) goto done;
    if ((ret = HeapAlloc( heap->handle, 0, size ))) heap->allocated += size;

done:
    LeaveCriticalSection( &heap->cs );
    return ret;
}

void *ws_alloc_zero( WS_HEAP *handle, SIZE_T size )
{
    struct heap *heap = (struct heap *)handle;
    void *ret = NULL;

    EnterCriticalSection( &heap->cs );

    if (heap->magic != HEAP_MAGIC) goto done;
    if (!ensure_heap( heap ) || size > heap->max_size - heap->allocated) goto done;
    if ((ret = HeapAlloc( heap->handle, HEAP_ZERO_MEMORY, size ))) heap->allocated += size;

done:
    LeaveCriticalSection( &heap->cs );
    return ret;
}

void *ws_realloc( WS_HEAP *handle, void *ptr, SIZE_T old_size, SIZE_T new_size )
{
    struct heap *heap = (struct heap *)handle;
    void *ret = NULL;

    EnterCriticalSection( &heap->cs );

    if (heap->magic != HEAP_MAGIC || !ensure_heap( heap )) goto done;
    if (new_size >= old_size)
    {
        SIZE_T size = new_size - old_size;
        if (size > heap->max_size - heap->allocated) goto done;
        if ((ret = HeapReAlloc( heap->handle, 0, ptr, new_size ))) heap->allocated += size;
    }
    else
    {
        SIZE_T size = old_size - new_size;
        if ((ret = HeapReAlloc( heap->handle, 0, ptr, new_size ))) heap->allocated -= size;
    }

done:
    LeaveCriticalSection( &heap->cs );
    return ret;
}

void *ws_realloc_zero( WS_HEAP *handle, void *ptr, SIZE_T old_size, SIZE_T new_size )
{
    struct heap *heap = (struct heap *)handle;
    void *ret = NULL;

    EnterCriticalSection( &heap->cs );

    if (heap->magic != HEAP_MAGIC || !ensure_heap( heap )) goto done;
    if (new_size >= old_size)
    {
        SIZE_T size = new_size - old_size;
        if (size > heap->max_size - heap->allocated) goto done;
        if ((ret = HeapReAlloc( heap->handle, HEAP_ZERO_MEMORY, ptr, new_size ))) heap->allocated += size;
    }
    else
    {
        SIZE_T size = old_size - new_size;
        if ((ret = HeapReAlloc( heap->handle, HEAP_ZERO_MEMORY, ptr, new_size ))) heap->allocated -= size;
    }

done:
    LeaveCriticalSection( &heap->cs );
    return ret;
}

void ws_free( WS_HEAP *handle, void *ptr, SIZE_T size )
{
    struct heap *heap = (struct heap *)handle;

    EnterCriticalSection( &heap->cs );

    if (heap->magic == HEAP_MAGIC)
    {
        HeapFree( heap->handle, 0, ptr );
        heap->allocated -= size;
    }

    LeaveCriticalSection( &heap->cs );
}

/**************************************************************************
 *          WsAlloc		[webservices.@]
 */
HRESULT WINAPI WsAlloc( WS_HEAP *handle, SIZE_T size, void **ptr, WS_ERROR *error )
{
    void *mem;

169
    TRACE( "%p %Iu %p %p\n", handle, size, ptr, error );
170 171 172 173 174 175 176 177 178 179
    if (error) FIXME( "ignoring error parameter\n" );

    if (!handle || !ptr) return E_INVALIDARG;
    if (!(mem = ws_alloc( handle, size ))) return WS_E_QUOTA_EXCEEDED;
    *ptr = mem;
    return S_OK;
}

static struct heap *alloc_heap(void)
{
180
    static const ULONG count = ARRAY_SIZE( heap_props );
181 182 183
    struct heap *ret;
    ULONG size = sizeof(*ret) + prop_size( heap_props, count );

184
    if (!(ret = calloc( 1, size ))) return NULL;
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202

    ret->magic      = HEAP_MAGIC;
    InitializeCriticalSection( &ret->cs );
    ret->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": heap.cs");

    prop_init( heap_props, count, ret->prop, &ret[1] );
    ret->prop_count = count;
    return ret;
}

/**************************************************************************
 *          WsCreateHeap		[webservices.@]
 */
HRESULT WINAPI WsCreateHeap( SIZE_T max_size, SIZE_T trim_size, const WS_HEAP_PROPERTY *properties,
                             ULONG count, WS_HEAP **handle, WS_ERROR *error )
{
    struct heap *heap;

203
    TRACE( "%Iu %Iu %p %lu %p %p\n", max_size, trim_size, properties, count, handle, error );
204 205 206 207 208 209 210 211
    if (error) FIXME( "ignoring error parameter\n" );

    if (!handle || count) return E_INVALIDARG;
    if (!(heap = alloc_heap())) return E_OUTOFMEMORY;

    prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_MAX_SIZE, &max_size, sizeof(max_size) );
    prop_set( heap->prop, heap->prop_count, WS_HEAP_PROPERTY_TRIM_SIZE, &trim_size, sizeof(trim_size) );

212
    TRACE( "created %p\n", heap );
213 214 215 216 217 218
    *handle = (WS_HEAP *)heap;
    return S_OK;
}

static void reset_heap( struct heap *heap )
{
219
    if (heap->handle) HeapDestroy( heap->handle );
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    heap->handle   = NULL;
    heap->max_size = heap->allocated = 0;
}

/**************************************************************************
 *          WsFreeHeap		[webservices.@]
 */
void WINAPI WsFreeHeap( WS_HEAP *handle )
{
    struct heap *heap = (struct heap *)handle;

    TRACE( "%p\n", handle );

    if (!heap) return;

    EnterCriticalSection( &heap->cs );

    if (heap->magic != HEAP_MAGIC)
    {
        LeaveCriticalSection( &heap->cs );
        return;
    }

    reset_heap( heap );
    heap->magic = 0;

    LeaveCriticalSection( &heap->cs );

    heap->cs.DebugInfo->Spare[0] = 0;
    DeleteCriticalSection( &heap->cs );
250
    free( heap );
251 252 253 254 255 256 257 258
}

/**************************************************************************
 *          WsResetHeap		[webservices.@]
 */
HRESULT WINAPI WsResetHeap( WS_HEAP *handle, WS_ERROR *error )
{
    struct heap *heap = (struct heap *)handle;
259
    HRESULT hr = S_OK;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

    TRACE( "%p %p\n", handle, error );
    if (error) FIXME( "ignoring error parameter\n" );

    if (!heap) return E_INVALIDARG;

    EnterCriticalSection( &heap->cs );

    if (heap->magic != HEAP_MAGIC)
    {
        LeaveCriticalSection( &heap->cs );
        return E_INVALIDARG;
    }

    reset_heap( heap );

    LeaveCriticalSection( &heap->cs );
277
    TRACE( "returning %#lx\n", hr );
278
    return hr;
279 280 281 282 283 284 285 286 287 288 289
}

/**************************************************************************
 *          WsGetHeapProperty		[webservices.@]
 */
HRESULT WINAPI WsGetHeapProperty( WS_HEAP *handle, WS_HEAP_PROPERTY_ID id, void *buf,
                                  ULONG size, WS_ERROR *error )
{
    struct heap *heap = (struct heap *)handle;
    HRESULT hr = S_OK;

290
    TRACE( "%p %u %p %lu %p\n", handle, id, buf, size, error );
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    if (error) FIXME( "ignoring error parameter\n" );

    if (!heap) return E_INVALIDARG;

    EnterCriticalSection( &heap->cs );

    if (heap->magic != HEAP_MAGIC)
    {
        LeaveCriticalSection( &heap->cs );
        return E_INVALIDARG;
    }

    switch (id)
    {
    case WS_HEAP_PROPERTY_REQUESTED_SIZE:
    case WS_HEAP_PROPERTY_ACTUAL_SIZE:
    {
        SIZE_T *heap_size = buf;
        if (!buf || size != sizeof(heap_size)) hr = E_INVALIDARG;
        else *heap_size = heap->allocated;
        break;
    }
    default:
        hr = prop_get( heap->prop, heap->prop_count, id, buf, size );
    }

    LeaveCriticalSection( &heap->cs );
318
    TRACE( "returning %#lx\n", hr );
319 320 321 322
    return hr;
}

#define XML_BUFFER_INITIAL_ALLOCATED_SIZE 256
323 324
struct xmlbuf *alloc_xmlbuf( WS_HEAP *heap, SIZE_T size, WS_XML_WRITER_ENCODING_TYPE encoding, WS_CHARSET charset,
                             const WS_XML_DICTIONARY *dict_static, WS_XML_DICTIONARY *dict )
325 326 327
{
    struct xmlbuf *ret;

328
    if (!size) size = XML_BUFFER_INITIAL_ALLOCATED_SIZE;
329
    if (!(ret = ws_alloc( heap, sizeof(*ret) ))) return NULL;
330
    if (!(ret->bytes.bytes = ws_alloc( heap, size )))
331 332 333 334
    {
        ws_free( heap, ret, sizeof(*ret) );
        return NULL;
    }
335
    ret->heap         = heap;
336
    ret->size         = size;
337
    ret->bytes.length = 0;
338 339
    ret->encoding     = encoding;
    ret->charset      = charset;
340 341
    ret->dict_static  = dict_static;
    ret->dict         = dict;
342 343 344 345 346 347
    return ret;
}

void free_xmlbuf( struct xmlbuf *xmlbuf )
{
    if (!xmlbuf) return;
348
    ws_free( xmlbuf->heap, xmlbuf->bytes.bytes, xmlbuf->size );
349 350 351 352 353 354 355 356 357 358 359
    ws_free( xmlbuf->heap, xmlbuf, sizeof(*xmlbuf) );
}

/**************************************************************************
 *          WsCreateXmlBuffer		[webservices.@]
 */
HRESULT WINAPI WsCreateXmlBuffer( WS_HEAP *heap, const WS_XML_BUFFER_PROPERTY *properties,
                                  ULONG count, WS_XML_BUFFER **handle, WS_ERROR *error )
{
    struct xmlbuf *xmlbuf;

360
    TRACE( "%p %p %lu %p %p\n", heap, properties, count, handle, error );
361 362
    if (error) FIXME( "ignoring error parameter\n" );

363 364 365
    if (!heap || !handle) return E_INVALIDARG;
    if (count) FIXME( "properties not implemented\n" );

366
    if (!(xmlbuf = alloc_xmlbuf( heap, 0, WS_XML_WRITER_ENCODING_TYPE_TEXT, WS_CHARSET_UTF8, NULL, NULL )))
367 368 369
    {
        return WS_E_QUOTA_EXCEEDED;
    }
370

371
    TRACE( "created %p\n", xmlbuf );
372 373 374
    *handle = (WS_XML_BUFFER *)xmlbuf;
    return S_OK;
}