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