handle.c 4.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright 2008 Hans Leidekker for CodeWeavers
 *
 * Based on the handle implementation from wininet.
 *
 * 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 "config.h"
22
#include "ws2tcpip.h"
23 24 25 26 27 28
#include <stdarg.h>

#include "windef.h"
#include "winbase.h"
#include "winhttp.h"

29
#include "wine/debug.h"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
#include "winhttp_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(winhttp);

#define HANDLE_CHUNK_SIZE 0x10

static CRITICAL_SECTION handle_cs;
static CRITICAL_SECTION_DEBUG handle_cs_debug =
{
    0, 0, &handle_cs,
    { &handle_cs_debug.ProcessLocksList, &handle_cs_debug.ProcessLocksList },
      0, 0, { (ULONG_PTR)(__FILE__ ": handle_cs") }
};
static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };

45
static struct object_header **handles;
46 47 48
static ULONG_PTR next_handle;
static ULONG_PTR max_handles;

49
struct object_header *addref_object( struct object_header *hdr )
50 51 52 53 54 55
{
    ULONG refs = InterlockedIncrement( &hdr->refs );
    TRACE("%p -> refcount = %d\n", hdr, refs);
    return hdr;
}

56
struct object_header *grab_object( HINTERNET hinternet )
57
{
58
    struct object_header *hdr = NULL;
59 60 61 62 63 64 65 66 67 68 69 70 71
    ULONG_PTR handle = (ULONG_PTR)hinternet;

    EnterCriticalSection( &handle_cs );

    if ((handle > 0) && (handle <= max_handles) && handles[handle - 1])
        hdr = addref_object( handles[handle - 1] );

    LeaveCriticalSection( &handle_cs );

    TRACE("handle 0x%lx -> %p\n", handle, hdr);
    return hdr;
}

72
void release_object( struct object_header *hdr )
73 74 75 76 77
{
    ULONG refs = InterlockedDecrement( &hdr->refs );
    TRACE("object %p refcount = %d\n", hdr, refs);
    if (!refs)
    {
78
        if (hdr->type == WINHTTP_HANDLE_TYPE_REQUEST) close_connection( (struct request *)hdr );
79

80 81
        send_callback( hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, &hdr->handle, sizeof(HINTERNET) );

82 83 84 85 86 87
        TRACE("destroying object %p\n", hdr);
        if (hdr->type != WINHTTP_HANDLE_TYPE_SESSION) list_remove( &hdr->entry );
        hdr->vtbl->destroy( hdr );
    }
}

88
HINTERNET alloc_handle( struct object_header *hdr )
89
{
90
    struct object_header **p;
91
    ULONG_PTR handle, num;
92 93

    list_init( &hdr->children );
94
    hdr->handle = NULL;
95 96 97 98 99 100 101 102 103 104 105

    EnterCriticalSection( &handle_cs );
    if (!max_handles)
    {
        num = HANDLE_CHUNK_SIZE;
        if (!(p = heap_alloc_zero( sizeof(ULONG_PTR) * num ))) goto end;
        handles = p;
        max_handles = num;
    }
    if (max_handles == next_handle)
    {
106
        num = max_handles * 2;
107 108 109 110 111 112 113 114
        if (!(p = heap_realloc_zero( handles, sizeof(ULONG_PTR) * num ))) goto end;
        handles = p;
        max_handles = num;
    }
    handle = next_handle;
    if (handles[handle]) ERR("handle isn't free but should be\n");

    handles[handle] = addref_object( hdr );
115
    hdr->handle = (HINTERNET)(handle + 1);
116
    while ((next_handle < max_handles) && handles[next_handle]) next_handle++;
117 118 119

end:
    LeaveCriticalSection( &handle_cs );
120
    return hdr->handle;
121 122 123 124 125 126
}

BOOL free_handle( HINTERNET hinternet )
{
    BOOL ret = FALSE;
    ULONG_PTR handle = (ULONG_PTR)hinternet;
127
    struct object_header *hdr = NULL, *child, *next;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    EnterCriticalSection( &handle_cs );

    if ((handle > 0) && (handle <= max_handles))
    {
        handle--;
        if (handles[handle])
        {
            hdr = handles[handle];
            TRACE("destroying handle 0x%lx for object %p\n", handle + 1, hdr);
            handles[handle] = NULL;
            ret = TRUE;
        }
    }

    LeaveCriticalSection( &handle_cs );

    if (hdr)
    {
147
        LIST_FOR_EACH_ENTRY_SAFE( child, next, &hdr->children, struct object_header, entry )
148 149 150 151 152 153 154 155 156 157 158 159 160
        {
            TRACE("freeing child handle %p for parent handle 0x%lx\n", child->handle, handle + 1);
            free_handle( child->handle );
        }
        release_object( hdr );
    }

    EnterCriticalSection( &handle_cs );
    if (next_handle > handle && !handles[handle]) next_handle = handle;
    LeaveCriticalSection( &handle_cs );

    return ret;
}