ReactOS  0.4.14-dev-608-gd495a4f
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 "config.h"
22 #include "ws2tcpip.h"
23 #include <stdarg.h>
24 
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winhttp.h"
28 
29 #include "wine/debug.h"
30 #include "winhttp_private.h"
31 
33 
34 #define HANDLE_CHUNK_SIZE 0x10
35 
38 {
39  0, 0, &handle_cs,
41  0, 0, { (ULONG_PTR)(__FILE__ ": handle_cs") }
42 };
43 static CRITICAL_SECTION handle_cs = { &handle_cs_debug, -1, 0, 0, 0, 0 };
44 
45 static struct object_header **handles;
48 
50 {
51  ULONG refs = InterlockedIncrement( &hdr->refs );
52  TRACE("%p -> refcount = %d\n", hdr, refs);
53  return hdr;
54 }
55 
56 struct object_header *grab_object( HINTERNET hinternet )
57 {
58  struct object_header *hdr = NULL;
59  ULONG_PTR handle = (ULONG_PTR)hinternet;
60 
62 
63  if ((handle > 0) && (handle <= max_handles) && handles[handle - 1])
64  hdr = addref_object( handles[handle - 1] );
65 
67 
68  TRACE("handle 0x%lx -> %p\n", handle, hdr);
69  return hdr;
70 }
71 
73 {
74  ULONG refs = InterlockedDecrement( &hdr->refs );
75  TRACE("object %p refcount = %d\n", hdr, refs);
76  if (!refs)
77  {
78  if (hdr->type == WINHTTP_HANDLE_TYPE_REQUEST) close_connection( (struct request *)hdr );
79 
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 
89 {
90  struct object_header **p;
92 
93  list_init( &hdr->children );
94  hdr->handle = NULL;
95 
97  if (!max_handles)
98  {
100  if (!(p = heap_alloc_zero( sizeof(ULONG_PTR) * num ))) goto end;
101  handles = p;
102  max_handles = num;
103  }
104  if (max_handles == next_handle)
105  {
106  num = max_handles * 2;
107  if (!(p = heap_realloc_zero( handles, sizeof(ULONG_PTR) * num ))) goto end;
108  handles = p;
109  max_handles = num;
110  }
112  if (handles[handle]) ERR("handle isn't free but should be\n");
113 
115  hdr->handle = (HINTERNET)(handle + 1);
117 
118 end:
120  return hdr->handle;
121 }
122 
124 {
125  BOOL ret = FALSE;
126  ULONG_PTR handle = (ULONG_PTR)hinternet;
127  struct object_header *hdr = NULL, *child, *next;
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 0x%lx for object %p\n", handle + 1, hdr);
138  handles[handle] = NULL;
139  ret = TRUE;
140  }
141  }
142 
144 
145  if (hdr)
146  {
147  LIST_FOR_EACH_ENTRY_SAFE( child, next, &hdr->children, struct object_header, entry )
148  {
149  TRACE("freeing child handle %p for parent handle 0x%lx\n", child->handle, handle + 1);
150  free_handle( child->handle );
151  }
152  release_object( hdr );
153  }
154 
158 
159  return ret;
160 }
#define WINHTTP_HANDLE_TYPE_REQUEST
Definition: winhttp.h:432
LPVOID HINTERNET
Definition: winhttp.h:32
namespace GUID const ADDRINFOEXW ADDRINFOEXW struct timeval OVERLAPPED LPLOOKUPSERVICE_COMPLETION_ROUTINE HANDLE * handle
Definition: sock.c:82
#define TRUE
Definition: types.h:120
char hdr[14]
Definition: iptest.cpp:33
static struct object_header ** handles
Definition: handle.c:45
HINTERNET alloc_handle(struct object_header *hdr)
Definition: handle.c:88
struct object_header * grab_object(HINTERNET hinternet)
Definition: handle.c:56
GLuint GLuint end
Definition: gl.h:1545
#define HANDLE_CHUNK_SIZE
Definition: handle.c:34
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
static HWND child
Definition: cursoricon.c:298
uint32_t ULONG_PTR
Definition: typedefs.h:63
unsigned int BOOL
Definition: ntddk_ex.h:94
WINE_DEFAULT_DEBUG_CHANNEL(msi)
void close_connection(struct request *request)
Definition: request.c:1809
smooth NULL
Definition: ftsmooth.c:416
#define WINHTTP_HANDLE_TYPE_SESSION
Definition: winhttp.h:430
__WINE_SERVER_LIST_INLINE void list_remove(struct list *elem)
Definition: list.h:108
#define TRACE(s)
Definition: solgame.cpp:4
static ULONG_PTR next_handle
Definition: handle.c:46
GLuint GLuint num
Definition: glext.h:9618
#define WINHTTP_CALLBACK_STATUS_HANDLE_CLOSING
Definition: winhttp.h:391
int ret
#define InterlockedDecrement
Definition: armddk.h:52
uint32_t entry
Definition: isohybrid.c:63
BOOL free_handle(HINTERNET hinternet)
Definition: handle.c:123
LIST_ENTRY ProcessLocksList
Definition: winbase.h:855
#define ERR(fmt,...)
Definition: debug.h:109
static CRITICAL_SECTION_DEBUG handle_cs_debug
Definition: handle.c:37
static unsigned __int64 next
Definition: rand_nt.c:6
#define LIST_FOR_EACH_ENTRY_SAFE(cursor, cursor2, list, type, field)
Definition: list.h:204
#define InterlockedIncrement
Definition: armddk.h:53
static ULONG_PTR max_handles
Definition: handle.c:47
void send_callback(struct object_header *hdr, DWORD status, void *info, DWORD buflen)
Definition: session.c:55
Definition: tftpd.h:85
__WINE_SERVER_LIST_INLINE void list_init(struct list *list)
Definition: list.h:149
unsigned int ULONG
Definition: retypes.h:1
#define ULONG_PTR
Definition: config.h:101
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
GLfloat GLfloat p
Definition: glext.h:8902
struct object_header * addref_object(struct object_header *hdr)
Definition: handle.c:49
static CRITICAL_SECTION handle_cs
Definition: handle.c:36
void release_object(struct object_header *hdr)
Definition: handle.c:72