ReactOS 0.4.16-dev-2528-g7139e57
handle.c
Go to the documentation of this file.
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 <stdarg.h>
22
23#include "windef.h"
24#include "winbase.h"
25#include "ws2tcpip.h"
26#include "winhttp.h"
27
28#include "wine/debug.h"
29#include "winhttp_private.h"
30
32
33#define HANDLE_CHUNK_SIZE 0x10
34
37{
38 0, 0, &handle_cs,
40 0, 0, { (ULONG_PTR)(__FILE__ ": handle_cs") }
41};
42static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };
43
44static struct object_header **handles;
47
49{
51 TRACE( "%p -> refcount = %lu\n", hdr, refs );
52 return hdr;
53}
54
56{
57 struct object_header *hdr = NULL;
58 ULONG_PTR handle = (ULONG_PTR)hinternet;
59
61
62 if ((handle > 0) && (handle <= max_handles) && handles[handle - 1])
64
66
67 TRACE( "handle %Ix -> %p\n", handle, hdr );
68 return hdr;
69}
70
72{
74 TRACE( "object %p refcount = %lu\n", hdr, refs );
75 if (!refs)
76 {
78
80
81 TRACE( "destroying object %p\n", hdr );
82 hdr->vtbl->destroy( hdr );
83 }
84}
85
87{
88 struct object_header **p;
90
91 hdr->handle = NULL;
92
94 if (!max_handles)
95 {
97 if (!(p = calloc( 1, sizeof(*p) * num ))) goto end;
98 handles = p;
100 }
102 {
103 size_t new_size, old_size = max_handles * sizeof(*handles);
104 num = max_handles * 2;
105 new_size = num * sizeof(*handles);
106 if (!(p = realloc( handles, new_size ))) goto end;
107 memset( (char *)p + old_size, 0, new_size - old_size );
108 handles = p;
110 }
112 if (handles[handle]) ERR("handle isn't free but should be\n");
113
115 hdr->handle = (HINTERNET)(handle + 1);
117
118end:
120 return hdr->handle;
121}
122
124{
125 BOOL ret = FALSE;
126 ULONG_PTR handle = (ULONG_PTR)hinternet;
127 struct object_header *hdr = NULL;
128
130
131 if ((handle > 0) && (handle <= max_handles))
132 {
133 handle--;
134 if (handles[handle])
135 {
136 hdr = handles[handle];
137 TRACE( "destroying handle %Ix for object %p\n", handle + 1, hdr );
139 ret = TRUE;
140 }
141 }
142
144
145 if (hdr)
146 {
147 if (hdr->vtbl->handle_closing)
148 hdr->vtbl->handle_closing( hdr );
150 }
151
155
156 return ret;
157}
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ERR(fmt,...)
Definition: precomp.h:57
#define realloc
Definition: debug_ros.c:6
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static CRITICAL_SECTION_DEBUG handle_cs_debug
Definition: handle.c:39
static CRITICAL_SECTION handle_cs
Definition: handle.c:38
static BOOL alloc_handle(struct handle_table *lpTable, OBJECTHDR *lpObject, HCRYPTKEY *lpHandle)
Definition: handle.c:183
BOOL free_handle(HINTERNET hinternet)
Definition: handle.c:123
struct object_header * grab_object(HINTERNET hinternet)
Definition: handle.c:55
#define HANDLE_CHUNK_SIZE
Definition: handle.c:33
static struct object_header ** handles
Definition: handle.c:44
static ULONG_PTR max_handles
Definition: handle.c:46
void release_object(struct object_header *hdr)
Definition: handle.c:71
struct object_header * addref_object(struct object_header *hdr)
Definition: handle.c:48
static ULONG_PTR next_handle
Definition: handle.c:45
return ret
Definition: mutex.c:146
#define ULONG_PTR
Definition: config.h:101
size_t const old_size
Definition: expand.cpp:65
size_t const new_size
Definition: expand.cpp:66
unsigned int BOOL
Definition: ntddk_ex.h:94
GLuint GLuint end
Definition: gl.h:1545
GLfloat GLfloat p
Definition: glext.h:8902
GLuint GLuint num
Definition: glext.h:9618
char hdr[14]
Definition: iptest.cpp:33
static void close_connection(void)
Definition: http.c:5576
#define calloc
Definition: rosglue.h:14
#define memset(x, y, z)
Definition: compat.h:39
#define TRACE(s)
Definition: solgame.cpp:4
HINTERNET handle
Definition: tftpd.h:86
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
#define WINHTTP_HANDLE_TYPE_REQUEST
Definition: winhttp.h:503
LPVOID HINTERNET
Definition: winhttp.h:37
#define WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING
Definition: winhttp.h:454