[WINHTTP] Sync with Wine Staging 1.9.23. CORE-12409
[reactos.git] / reactos / 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 #include "winhttp_private.h"
22
23 #define HANDLE_CHUNK_SIZE 0x10
24
25 static CRITICAL_SECTION handle_cs;
26 static CRITICAL_SECTION_DEBUG handle_cs_debug =
27 {
28 0, 0, &handle_cs,
29 { &handle_cs_debug.ProcessLocksList, &handle_cs_debug.ProcessLocksList },
30 0, 0, { (ULONG_PTR)(__FILE__ ": handle_cs") }
31 };
32 static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };
33
34 static object_header_t **handles;
35 static ULONG_PTR next_handle;
36 static ULONG_PTR max_handles;
37
38 object_header_t *addref_object( object_header_t *hdr )
39 {
40 ULONG refs = InterlockedIncrement( &hdr->refs );
41 TRACE("%p -> refcount = %d\n", hdr, refs);
42 return hdr;
43 }
44
45 object_header_t *grab_object( HINTERNET hinternet )
46 {
47 object_header_t *hdr = NULL;
48 ULONG_PTR handle = (ULONG_PTR)hinternet;
49
50 EnterCriticalSection( &handle_cs );
51
52 if ((handle > 0) && (handle <= max_handles) && handles[handle - 1])
53 hdr = addref_object( handles[handle - 1] );
54
55 LeaveCriticalSection( &handle_cs );
56
57 TRACE("handle 0x%lx -> %p\n", handle, hdr);
58 return hdr;
59 }
60
61 void release_object( object_header_t *hdr )
62 {
63 ULONG refs = InterlockedDecrement( &hdr->refs );
64 TRACE("object %p refcount = %d\n", hdr, refs);
65 if (!refs)
66 {
67 if (hdr->type == WINHTTP_HANDLE_TYPE_REQUEST) close_connection( (request_t *)hdr );
68
69 send_callback( hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, &hdr->handle, sizeof(HINTERNET) );
70
71 TRACE("destroying object %p\n", hdr);
72 if (hdr->type != WINHTTP_HANDLE_TYPE_SESSION) list_remove( &hdr->entry );
73 hdr->vtbl->destroy( hdr );
74 }
75 }
76
77 HINTERNET alloc_handle( object_header_t *hdr )
78 {
79 object_header_t **p;
80 ULONG_PTR handle, num;
81
82 list_init( &hdr->children );
83 hdr->handle = NULL;
84
85 EnterCriticalSection( &handle_cs );
86 if (!max_handles)
87 {
88 num = HANDLE_CHUNK_SIZE;
89 if (!(p = heap_alloc_zero( sizeof(ULONG_PTR) * num ))) goto end;
90 handles = p;
91 max_handles = num;
92 }
93 if (max_handles == next_handle)
94 {
95 num = max_handles * 2;
96 if (!(p = heap_realloc_zero( handles, sizeof(ULONG_PTR) * num ))) goto end;
97 handles = p;
98 max_handles = num;
99 }
100 handle = next_handle;
101 if (handles[handle]) ERR("handle isn't free but should be\n");
102
103 handles[handle] = addref_object( hdr );
104 hdr->handle = (HINTERNET)(handle + 1);
105 while ((next_handle < max_handles) && handles[next_handle]) next_handle++;
106
107 end:
108 LeaveCriticalSection( &handle_cs );
109 return hdr->handle;
110 }
111
112 BOOL free_handle( HINTERNET hinternet )
113 {
114 BOOL ret = FALSE;
115 ULONG_PTR handle = (ULONG_PTR)hinternet;
116 object_header_t *hdr = NULL, *child, *next;
117
118 EnterCriticalSection( &handle_cs );
119
120 if ((handle > 0) && (handle <= max_handles))
121 {
122 handle--;
123 if (handles[handle])
124 {
125 hdr = handles[handle];
126 TRACE("destroying handle 0x%lx for object %p\n", handle + 1, hdr);
127 handles[handle] = NULL;
128 ret = TRUE;
129 }
130 }
131
132 LeaveCriticalSection( &handle_cs );
133
134 if (hdr)
135 {
136 LIST_FOR_EACH_ENTRY_SAFE( child, next, &hdr->children, object_header_t, entry )
137 {
138 TRACE("freeing child handle %p for parent handle 0x%lx\n", child->handle, handle + 1);
139 free_handle( child->handle );
140 }
141 release_object( hdr );
142 }
143
144 EnterCriticalSection( &handle_cs );
145 if (next_handle > handle && !handles[handle]) next_handle = handle;
146 LeaveCriticalSection( &handle_cs );
147
148 return ret;
149 }