1 /*
2 * Server-side USER handles
3 *
4 * Copyright (C) 2001 Alexandre Julliard
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 "thread.h"
22 #include "user.h"
23 #include "request.h"
24
25 struct 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
32 static struct user_handle *handles;
33 static struct user_handle *freelist;
34 static int nb_handles;
35 static int allocated_handles;
36
37 static struct user_handle *handle_to_entry( user_handle_t handle )
38 {
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;
47 }
48
49 static inline user_handle_t entry_to_handle( struct user_handle *ptr )
50 {
51 unsigned int index = ptr - handles;
52 return (index << 1) + FIRST_USER_HANDLE + (ptr->generation << 16);
53 }
54
55 static inline struct user_handle *alloc_user_entry(void)
56 {
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 );
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;
76 }
77 handle = &handles[nb_handles++];
78 handle->generation = 0;
79 return handle;
80 }
81
82 static inline void *free_user_entry( struct user_handle *ptr )
83 {
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 */
93 user_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 */
104 void *get_user_object( user_handle_t handle, enum user_object type )
105 {
106 struct user_handle *entry;
107
108 if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
109 return entry->ptr;
110 }
111
112 /* get the full handle for a possibly truncated handle */
113 user_handle_t get_user_full_handle( user_handle_t handle )
114 {
115 struct user_handle *entry;
116
117 if (handle >> 16) return handle;
118 if (!(entry = handle_to_entry( handle ))) return handle;
119 return entry_to_handle( entry );
120 }
121
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 )
124 {
125 struct user_handle *entry;
126
127 if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
128 *handle = entry_to_handle( entry );
129 return entry->ptr;
130 }
131
132 /* free a user handle and return a pointer to the object */
133 void *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 */
146 void *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 {
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 */
156 }
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 }
168
169 /* free client-side user handles managed by the process */
170 void 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
179 /* allocate an arbitrary user handle */
180 DECL_HANDLER(alloc_user_handle)
181 {
182 reply->handle = alloc_user_handle( current->process, USER_CLIENT );
183 }
184
185
186 /* free an arbitrary user handle */
187 DECL_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 }
196
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.