blob: c529fc729def49f3661197a52f7997b1f64ed175 [file] [log] [blame]
Alexandre Julliard1a66d222001-08-28 18:44:52 +00001/*
2 * Server-side USER handles
3 *
4 * Copyright (C) 2001 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard1a66d222001-08-28 18:44:52 +000019 */
20
21#include "thread.h"
22#include "user.h"
Alexandre Julliard9018e132009-10-12 14:25:01 +020023#include "request.h"
Alexandre Julliard1a66d222001-08-28 18:44:52 +000024
25struct user_handle
26{
27 void *ptr; /* pointer to object */
28 unsigned short type; /* object type (0 if free) */
29 unsigned short generation; /* generation counter */
30};
31
32static struct user_handle *handles;
33static struct user_handle *freelist;
34static int nb_handles;
35static int allocated_handles;
36
Alexandre Julliard1a66d222001-08-28 18:44:52 +000037static struct user_handle *handle_to_entry( user_handle_t handle )
38{
Alexandre Julliard90af05f2005-05-07 15:03:00 +000039 unsigned short generation;
Alexandre Julliardd7641072008-12-08 16:57:38 +010040 int index = ((handle & 0xffff) - FIRST_USER_HANDLE) >> 1;
Alexandre Julliard1a66d222001-08-28 18:44:52 +000041 if (index < 0 || index >= nb_handles) return NULL;
42 if (!handles[index].type) return NULL;
Alexandre Julliardd7641072008-12-08 16:57:38 +010043 generation = handle >> 16;
Alexandre Julliard90af05f2005-05-07 15:03:00 +000044 if (generation == handles[index].generation || !generation || generation == 0xffff)
45 return &handles[index];
46 return NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +000047}
48
Andrew Talbotb1788c82007-03-17 10:52:14 +000049static inline user_handle_t entry_to_handle( struct user_handle *ptr )
Alexandre Julliard1a66d222001-08-28 18:44:52 +000050{
Alexandre Julliardd7641072008-12-08 16:57:38 +010051 unsigned int index = ptr - handles;
52 return (index << 1) + FIRST_USER_HANDLE + (ptr->generation << 16);
Alexandre Julliard1a66d222001-08-28 18:44:52 +000053}
54
Andrew Talbotb1788c82007-03-17 10:52:14 +000055static inline struct user_handle *alloc_user_entry(void)
Alexandre Julliard1a66d222001-08-28 18:44:52 +000056{
57 struct user_handle *handle;
58
59 if (freelist)
60 {
61 handle = freelist;
62 freelist = handle->ptr;
63 return handle;
64 }
65 if (nb_handles >= allocated_handles) /* need to grow the array */
66 {
67 struct user_handle *new_handles;
68 /* grow array by 50% (but at minimum 32 entries) */
69 int growth = max( 32, allocated_handles / 2 );
Alexandre Julliarded8a41c2004-05-28 19:35:37 +000070 int new_size = min( allocated_handles + growth, (LAST_USER_HANDLE-FIRST_USER_HANDLE+1) >> 1 );
Alexandre Julliard1a66d222001-08-28 18:44:52 +000071 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;
76 }
77 handle = &handles[nb_handles++];
78 handle->generation = 0;
79 return handle;
80}
81
Andrew Talbotb1788c82007-03-17 10:52:14 +000082static inline void *free_user_entry( struct user_handle *ptr )
Alexandre Julliard1a66d222001-08-28 18:44:52 +000083{
84 void *ret;
85 ret = ptr->ptr;
86 ptr->ptr = freelist;
87 ptr->type = 0;
88 freelist = ptr;
89 return ret;
90}
91
92/* allocate a user handle for a given object */
93user_handle_t alloc_user_handle( void *ptr, enum user_object type )
94{
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 );
101}
102
103/* return a pointer to a user object from its handle */
104void *get_user_object( user_handle_t handle, enum user_object type )
105{
106 struct user_handle *entry;
107
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000108 if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000109 return entry->ptr;
110}
111
Alexandre Julliardbc878ef2001-09-12 17:09:24 +0000112/* get the full handle for a possibly truncated handle */
113user_handle_t get_user_full_handle( user_handle_t handle )
114{
115 struct user_handle *entry;
116
Alexandre Julliardd7641072008-12-08 16:57:38 +0100117 if (handle >> 16) return handle;
Alexandre Julliardbc878ef2001-09-12 17:09:24 +0000118 if (!(entry = handle_to_entry( handle ))) return handle;
119 return entry_to_handle( entry );
120}
121
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000122/* same as get_user_object plus set the handle to the full 32-bit value */
123void *get_user_object_handle( user_handle_t *handle, enum user_object type )
124{
125 struct user_handle *entry;
126
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000127 if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000128 *handle = entry_to_handle( entry );
129 return entry->ptr;
130}
131
132/* free a user handle and return a pointer to the object */
133void *free_user_handle( user_handle_t handle )
134{
135 struct user_handle *entry;
136
137 if (!(entry = handle_to_entry( handle )))
138 {
139 set_error( STATUS_INVALID_HANDLE );
140 return NULL;
141 }
142 return free_user_entry( entry );
143}
144
145/* return the next user handle after 'handle' that is of a given type */
146void *next_user_handle( user_handle_t *handle, enum user_object type )
147{
148 struct user_handle *entry;
149
150 if (!*handle) entry = handles;
151 else
152 {
Alexandre Julliardd7641072008-12-08 16:57:38 +0100153 int index = ((*handle & 0xffff) - FIRST_USER_HANDLE) >> 1;
Alexandre Julliard65adc242002-05-31 18:25:53 +0000154 if (index < 0 || index >= nb_handles) return NULL;
155 entry = handles + index + 1; /* start from the next one */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000156 }
157 while (entry < handles + nb_handles)
158 {
159 if (!type || entry->type == type)
160 {
161 *handle = entry_to_handle( entry );
162 return entry->ptr;
163 }
164 entry++;
165 }
166 return NULL;
167}
Alexandre Julliard9018e132009-10-12 14:25:01 +0200168
Alexandre Julliard538b2472009-10-15 17:56:04 +0200169/* free client-side user handles managed by the process */
170void free_process_user_handles( struct process *process )
171{
172 unsigned int i;
173
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] );
177}
178
Alexandre Julliard9018e132009-10-12 14:25:01 +0200179/* allocate an arbitrary user handle */
180DECL_HANDLER(alloc_user_handle)
181{
Alexandre Julliard538b2472009-10-15 17:56:04 +0200182 reply->handle = alloc_user_handle( current->process, USER_CLIENT );
Alexandre Julliard9018e132009-10-12 14:25:01 +0200183}
184
185
186/* free an arbitrary user handle */
187DECL_HANDLER(free_user_handle)
188{
189 struct user_handle *entry;
190
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 );
195}