Sync with trunk r58687.
[reactos.git] / dll / win32 / winhttp / handle.c
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
3 *
4 * Based on the handle implementation from wininet.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <config.h>
26 //#include "wine/port.h"
27 #include <wine/debug.h>
28
29 //#include <stdarg.h>
30
31 //#include "windef.h"
32 #include <winbase.h>
33 #include <winhttp.h>
34
35 #include "winhttp_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
38
39 #define HANDLE_CHUNK_SIZE 0x10
40
41 static CRITICAL_SECTION handle_cs;
42 static CRITICAL_SECTION_DEBUG handle_cs_debug =
43 {
44 0, 0, &handle_cs,
45 { &handle_cs_debug.ProcessLocksList, &handle_cs_debug.ProcessLocksList },
46 0, 0, { (ULONG_PTR)(__FILE__ ": handle_cs") }
47 };
48 static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };
49
50 static object_header_t **handles;
51 static ULONG_PTR next_handle;
52 static ULONG_PTR max_handles;
53
54 object_header_t *addref_object( object_header_t *hdr )
55 {
56 ULONG refs = InterlockedIncrement( &hdr->refs );
57 TRACE("%p -> refcount = %d\n", hdr, refs);
58 return hdr;
59 }
60
61 object_header_t *grab_object( HINTERNET hinternet )
62 {
63 object_header_t *hdr = NULL;
64 ULONG_PTR handle = (ULONG_PTR)hinternet;
65
66 EnterCriticalSection( &handle_cs );
67
68 if ((handle > 0) && (handle <= max_handles) && handles[handle - 1])
69 hdr = addref_object( handles[handle - 1] );
70
71 LeaveCriticalSection( &handle_cs );
72
73 TRACE("handle 0x%lx -> %p\n", handle, hdr);
74 return hdr;
75 }
76
77 void release_object( object_header_t *hdr )
78 {
79 ULONG refs = InterlockedDecrement( &hdr->refs );
80 TRACE("object %p refcount = %d\n", hdr, refs);
81 if (!refs)
82 {
83 if (hdr->type == WINHTTP_HANDLE_TYPE_REQUEST) close_connection( (request_t *)hdr );
84
85 send_callback( hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, &hdr->handle, sizeof(HINTERNET) );
86
87 TRACE("destroying object %p\n", hdr);
88 if (hdr->type != WINHTTP_HANDLE_TYPE_SESSION) list_remove( &hdr->entry );
89 hdr->vtbl->destroy( hdr );
90 }
91 }
92
93 HINTERNET alloc_handle( object_header_t *hdr )
94 {
95 object_header_t **p;
96 ULONG_PTR handle = 0, num;
97
98 list_init( &hdr->children );
99
100 EnterCriticalSection( &handle_cs );
101 if (!max_handles)
102 {
103 num = HANDLE_CHUNK_SIZE;
104 if (!(p = heap_alloc_zero( sizeof(ULONG_PTR) * num ))) goto end;
105 handles = p;
106 max_handles = num;
107 }
108 if (max_handles == next_handle)
109 {
110 num = max_handles * 2;
111 if (!(p = heap_realloc_zero( handles, sizeof(ULONG_PTR) * num ))) goto end;
112 handles = p;
113 max_handles = num;
114 }
115 handle = next_handle;
116 if (handles[handle]) ERR("handle isn't free but should be\n");
117
118 handles[handle] = addref_object( hdr );
119 while (handles[next_handle] && (next_handle < max_handles)) next_handle++;
120
121 end:
122 LeaveCriticalSection( &handle_cs );
123 return hdr->handle = (HINTERNET)(handle + 1);
124 }
125
126 BOOL free_handle( HINTERNET hinternet )
127 {
128 BOOL ret = FALSE;
129 ULONG_PTR handle = (ULONG_PTR)hinternet;
130 object_header_t *hdr = NULL, *child, *next;
131
132 EnterCriticalSection( &handle_cs );
133
134 if ((handle > 0) && (handle <= max_handles))
135 {
136 handle--;
137 if (handles[handle])
138 {
139 hdr = handles[handle];
140 TRACE("destroying handle 0x%lx for object %p\n", handle + 1, hdr);
141 handles[handle] = NULL;
142 ret = TRUE;
143 }
144 }
145
146 LeaveCriticalSection( &handle_cs );
147
148 if (hdr)
149 {
150 LIST_FOR_EACH_ENTRY_SAFE( child, next, &hdr->children, object_header_t, entry )
151 {
152 TRACE("freeing child handle %p for parent handle 0x%lx\n", child->handle, handle + 1);
153 free_handle( child->handle );
154 }
155 release_object( hdr );
156 }
157
158 EnterCriticalSection( &handle_cs );
159 if (next_handle > handle && !handles[handle]) next_handle = handle;
160 LeaveCriticalSection( &handle_cs );
161
162 return ret;
163 }