blob: 6f99ac4b7e46fbe7d6d36d1ccbc1497aff13f23a [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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard1a66d222001-08-28 18:44:52 +000019 */
20
21#include "thread.h"
22#include "user.h"
23
24struct user_handle
25{
26 void *ptr; /* pointer to object */
27 unsigned short type; /* object type (0 if free) */
28 unsigned short generation; /* generation counter */
29};
30
31static struct user_handle *handles;
32static struct user_handle *freelist;
33static int nb_handles;
34static int allocated_handles;
35
Alexandre Julliard1a66d222001-08-28 18:44:52 +000036static struct user_handle *handle_to_entry( user_handle_t handle )
37{
Alexandre Julliardb3332d72002-10-19 01:00:59 +000038 int index = ((unsigned int)handle & 0xffff) - FIRST_USER_HANDLE;
Alexandre Julliard1a66d222001-08-28 18:44:52 +000039 if (index < 0 || index >= nb_handles) return NULL;
40 if (!handles[index].type) return NULL;
Alexandre Julliardb3332d72002-10-19 01:00:59 +000041 if (((unsigned int)handle >> 16) && ((unsigned int)handle >> 16 != handles[index].generation))
42 return NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +000043 return &handles[index];
44}
45
46inline static user_handle_t entry_to_handle( struct user_handle *ptr )
47{
48 int index = ptr - handles;
Alexandre Julliardb3332d72002-10-19 01:00:59 +000049 return (user_handle_t)((index + FIRST_USER_HANDLE) + (ptr->generation << 16));
Alexandre Julliard1a66d222001-08-28 18:44:52 +000050}
51
52inline static struct user_handle *alloc_user_entry(void)
53{
54 struct user_handle *handle;
55
56 if (freelist)
57 {
58 handle = freelist;
59 freelist = handle->ptr;
60 return handle;
61 }
62 if (nb_handles >= allocated_handles) /* need to grow the array */
63 {
64 struct user_handle *new_handles;
65 /* grow array by 50% (but at minimum 32 entries) */
66 int growth = max( 32, allocated_handles / 2 );
Alexandre Julliard7695d692001-09-24 01:19:59 +000067 int new_size = min( allocated_handles + growth, LAST_USER_HANDLE-FIRST_USER_HANDLE+1 );
Alexandre Julliard1a66d222001-08-28 18:44:52 +000068 if (new_size <= allocated_handles) return NULL;
69 if (!(new_handles = realloc( handles, new_size * sizeof(*handles) )))
70 return NULL;
71 handles = new_handles;
72 allocated_handles = new_size;
73 }
74 handle = &handles[nb_handles++];
75 handle->generation = 0;
76 return handle;
77}
78
79inline static void *free_user_entry( struct user_handle *ptr )
80{
81 void *ret;
82 ret = ptr->ptr;
83 ptr->ptr = freelist;
84 ptr->type = 0;
85 freelist = ptr;
86 return ret;
87}
88
89/* allocate a user handle for a given object */
90user_handle_t alloc_user_handle( void *ptr, enum user_object type )
91{
92 struct user_handle *entry = alloc_user_entry();
93 if (!entry) return 0;
94 entry->ptr = ptr;
95 entry->type = type;
96 if (++entry->generation >= 0xffff) entry->generation = 1;
97 return entry_to_handle( entry );
98}
99
100/* return a pointer to a user object from its handle */
101void *get_user_object( user_handle_t handle, enum user_object type )
102{
103 struct user_handle *entry;
104
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000105 if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000106 return entry->ptr;
107}
108
Alexandre Julliardbc878ef2001-09-12 17:09:24 +0000109/* get the full handle for a possibly truncated handle */
110user_handle_t get_user_full_handle( user_handle_t handle )
111{
112 struct user_handle *entry;
113
Alexandre Julliardb3332d72002-10-19 01:00:59 +0000114 if ((unsigned int)handle >> 16) return handle;
Alexandre Julliardbc878ef2001-09-12 17:09:24 +0000115 if (!(entry = handle_to_entry( handle ))) return handle;
116 return entry_to_handle( entry );
117}
118
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000119/* same as get_user_object plus set the handle to the full 32-bit value */
120void *get_user_object_handle( user_handle_t *handle, enum user_object type )
121{
122 struct user_handle *entry;
123
Alexandre Julliarda09da0c2001-09-21 21:08:40 +0000124 if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000125 *handle = entry_to_handle( entry );
126 return entry->ptr;
127}
128
129/* free a user handle and return a pointer to the object */
130void *free_user_handle( user_handle_t handle )
131{
132 struct user_handle *entry;
133
134 if (!(entry = handle_to_entry( handle )))
135 {
136 set_error( STATUS_INVALID_HANDLE );
137 return NULL;
138 }
139 return free_user_entry( entry );
140}
141
142/* return the next user handle after 'handle' that is of a given type */
143void *next_user_handle( user_handle_t *handle, enum user_object type )
144{
145 struct user_handle *entry;
146
147 if (!*handle) entry = handles;
148 else
149 {
Alexandre Julliardb3332d72002-10-19 01:00:59 +0000150 int index = ((unsigned int)*handle & 0xffff) - FIRST_USER_HANDLE;
Alexandre Julliard65adc242002-05-31 18:25:53 +0000151 if (index < 0 || index >= nb_handles) return NULL;
152 entry = handles + index + 1; /* start from the next one */
Alexandre Julliard1a66d222001-08-28 18:44:52 +0000153 }
154 while (entry < handles + nb_handles)
155 {
156 if (!type || entry->type == type)
157 {
158 *handle = entry_to_handle( entry );
159 return entry->ptr;
160 }
161 entry++;
162 }
163 return NULL;
164}