dbghelp: Properly fail on PDB files generated by MSVC compiler version 14.31.
[wine.git] / dlls / winhttp / handle.c
blob38959fd6456e2667bc7d5f0447936b2d5e8874d0
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * Based on the handle implementation from wininet.
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.
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.
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "ws2tcpip.h"
26 #include "winhttp.h"
28 #include "wine/debug.h"
29 #include "winhttp_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
33 #define HANDLE_CHUNK_SIZE 0x10
35 static CRITICAL_SECTION handle_cs;
36 static CRITICAL_SECTION_DEBUG handle_cs_debug =
38 0, 0, &handle_cs,
39 { &handle_cs_debug.ProcessLocksList, &handle_cs_debug.ProcessLocksList },
40 0, 0, { (ULONG_PTR)(__FILE__ ": handle_cs") }
42 static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };
44 static struct object_header **handles;
45 static ULONG_PTR next_handle;
46 static ULONG_PTR max_handles;
48 struct object_header *addref_object( struct object_header *hdr )
50 ULONG refs = InterlockedIncrement( &hdr->refs );
51 TRACE( "%p -> refcount = %lu\n", hdr, refs );
52 return hdr;
55 struct object_header *grab_object( HINTERNET hinternet )
57 struct object_header *hdr = NULL;
58 ULONG_PTR handle = (ULONG_PTR)hinternet;
60 EnterCriticalSection( &handle_cs );
62 if ((handle > 0) && (handle <= max_handles) && handles[handle - 1])
63 hdr = addref_object( handles[handle - 1] );
65 LeaveCriticalSection( &handle_cs );
67 TRACE( "handle %Ix -> %p\n", handle, hdr );
68 return hdr;
71 void release_object( struct object_header *hdr )
73 ULONG refs = InterlockedDecrement( &hdr->refs );
74 TRACE( "object %p refcount = %lu\n", hdr, refs );
75 if (!refs)
77 if (hdr->type == WINHTTP_HANDLE_TYPE_REQUEST) close_connection( (struct request *)hdr );
79 send_callback( hdr, WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING, &hdr->handle, sizeof(HINTERNET) );
81 TRACE( "destroying object %p\n", hdr );
82 hdr->vtbl->destroy( hdr );
86 HINTERNET alloc_handle( struct object_header *hdr )
88 struct object_header **p;
89 ULONG_PTR handle, num;
91 hdr->handle = NULL;
93 EnterCriticalSection( &handle_cs );
94 if (!max_handles)
96 num = HANDLE_CHUNK_SIZE;
97 if (!(p = calloc( 1, sizeof(ULONG_PTR) * num ))) goto end;
98 handles = p;
99 max_handles = num;
101 if (max_handles == next_handle)
103 size_t new_size, old_size = max_handles * sizeof(ULONG_PTR);
104 num = max_handles * 2;
105 new_size = num * sizeof(ULONG_PTR);
106 if (!(p = realloc( handles, new_size ))) goto end;
107 memset( (char *)p + old_size, 0, new_size - old_size );
108 handles = p;
109 max_handles = num;
111 handle = next_handle;
112 if (handles[handle]) ERR("handle isn't free but should be\n");
114 handles[handle] = addref_object( hdr );
115 hdr->handle = (HINTERNET)(handle + 1);
116 while ((next_handle < max_handles) && handles[next_handle]) next_handle++;
118 end:
119 LeaveCriticalSection( &handle_cs );
120 return hdr->handle;
123 BOOL free_handle( HINTERNET hinternet )
125 BOOL ret = FALSE;
126 ULONG_PTR handle = (ULONG_PTR)hinternet;
127 struct object_header *hdr = NULL;
129 EnterCriticalSection( &handle_cs );
131 if ((handle > 0) && (handle <= max_handles))
133 handle--;
134 if (handles[handle])
136 hdr = handles[handle];
137 TRACE( "destroying handle %Ix for object %p\n", handle + 1, hdr );
138 handles[handle] = NULL;
139 ret = TRUE;
143 LeaveCriticalSection( &handle_cs );
145 if (hdr) release_object( hdr );
147 EnterCriticalSection( &handle_cs );
148 if (next_handle > handle && !handles[handle]) next_handle = handle;
149 LeaveCriticalSection( &handle_cs );
151 return ret;