winhttp: Make WinHttpQueryHeaders behave the same way for all query types.
[wine/hramrach.git] / server / user.c
blobc529fc729def49f3661197a52f7997b1f64ed175
1 /*
2 * Server-side USER handles
4 * Copyright (C) 2001 Alexandre Julliard
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 "thread.h"
22 #include "user.h"
23 #include "request.h"
25 struct user_handle
27 void *ptr; /* pointer to object */
28 unsigned short type; /* object type (0 if free) */
29 unsigned short generation; /* generation counter */
32 static struct user_handle *handles;
33 static struct user_handle *freelist;
34 static int nb_handles;
35 static int allocated_handles;
37 static struct user_handle *handle_to_entry( user_handle_t handle )
39 unsigned short generation;
40 int index = ((handle & 0xffff) - FIRST_USER_HANDLE) >> 1;
41 if (index < 0 || index >= nb_handles) return NULL;
42 if (!handles[index].type) return NULL;
43 generation = handle >> 16;
44 if (generation == handles[index].generation || !generation || generation == 0xffff)
45 return &handles[index];
46 return NULL;
49 static inline user_handle_t entry_to_handle( struct user_handle *ptr )
51 unsigned int index = ptr - handles;
52 return (index << 1) + FIRST_USER_HANDLE + (ptr->generation << 16);
55 static inline struct user_handle *alloc_user_entry(void)
57 struct user_handle *handle;
59 if (freelist)
61 handle = freelist;
62 freelist = handle->ptr;
63 return handle;
65 if (nb_handles >= allocated_handles) /* need to grow the array */
67 struct user_handle *new_handles;
68 /* grow array by 50% (but at minimum 32 entries) */
69 int growth = max( 32, allocated_handles / 2 );
70 int new_size = min( allocated_handles + growth, (LAST_USER_HANDLE-FIRST_USER_HANDLE+1) >> 1 );
71 if (new_size <= allocated_handles) return NULL;
72 if (!(new_handles = realloc( handles, new_size * sizeof(*handles) )))
73 return NULL;
74 handles = new_handles;
75 allocated_handles = new_size;
77 handle = &handles[nb_handles++];
78 handle->generation = 0;
79 return handle;
82 static inline void *free_user_entry( struct user_handle *ptr )
84 void *ret;
85 ret = ptr->ptr;
86 ptr->ptr = freelist;
87 ptr->type = 0;
88 freelist = ptr;
89 return ret;
92 /* allocate a user handle for a given object */
93 user_handle_t alloc_user_handle( void *ptr, enum user_object type )
95 struct user_handle *entry = alloc_user_entry();
96 if (!entry) return 0;
97 entry->ptr = ptr;
98 entry->type = type;
99 if (++entry->generation >= 0xffff) entry->generation = 1;
100 return entry_to_handle( entry );
103 /* return a pointer to a user object from its handle */
104 void *get_user_object( user_handle_t handle, enum user_object type )
106 struct user_handle *entry;
108 if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
109 return entry->ptr;
112 /* get the full handle for a possibly truncated handle */
113 user_handle_t get_user_full_handle( user_handle_t handle )
115 struct user_handle *entry;
117 if (handle >> 16) return handle;
118 if (!(entry = handle_to_entry( handle ))) return handle;
119 return entry_to_handle( entry );
122 /* same as get_user_object plus set the handle to the full 32-bit value */
123 void *get_user_object_handle( user_handle_t *handle, enum user_object type )
125 struct user_handle *entry;
127 if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
128 *handle = entry_to_handle( entry );
129 return entry->ptr;
132 /* free a user handle and return a pointer to the object */
133 void *free_user_handle( user_handle_t handle )
135 struct user_handle *entry;
137 if (!(entry = handle_to_entry( handle )))
139 set_error( STATUS_INVALID_HANDLE );
140 return NULL;
142 return free_user_entry( entry );
145 /* return the next user handle after 'handle' that is of a given type */
146 void *next_user_handle( user_handle_t *handle, enum user_object type )
148 struct user_handle *entry;
150 if (!*handle) entry = handles;
151 else
153 int index = ((*handle & 0xffff) - FIRST_USER_HANDLE) >> 1;
154 if (index < 0 || index >= nb_handles) return NULL;
155 entry = handles + index + 1; /* start from the next one */
157 while (entry < handles + nb_handles)
159 if (!type || entry->type == type)
161 *handle = entry_to_handle( entry );
162 return entry->ptr;
164 entry++;
166 return NULL;
169 /* free client-side user handles managed by the process */
170 void free_process_user_handles( struct process *process )
172 unsigned int i;
174 for (i = 0; i < nb_handles; i++)
175 if (handles[i].type == USER_CLIENT && handles[i].ptr == process)
176 free_user_entry( &handles[i] );
179 /* allocate an arbitrary user handle */
180 DECL_HANDLER(alloc_user_handle)
182 reply->handle = alloc_user_handle( current->process, USER_CLIENT );
186 /* free an arbitrary user handle */
187 DECL_HANDLER(free_user_handle)
189 struct user_handle *entry;
191 if ((entry = handle_to_entry( req->handle )) && entry->type == USER_CLIENT)
192 free_user_entry( entry );
193 else
194 set_error( STATUS_INVALID_HANDLE );