Add more flexibility to the queue_async server call by moving most
[wine] / server / user.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "thread.h"
22 #include "user.h"
23
24 struct 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
31 static struct user_handle *handles;
32 static struct user_handle *freelist;
33 static int nb_handles;
34 static int allocated_handles;
35
36 static struct user_handle *handle_to_entry( user_handle_t handle )
37 {
38     int index = (handle & 0xffff) - FIRST_USER_HANDLE;
39     if (index < 0 || index >= nb_handles) return NULL;
40     if (!handles[index].type) return NULL;
41     if ((handle >> 16) && (handle >> 16 != handles[index].generation)) return NULL;
42     return &handles[index];
43 }
44
45 inline static user_handle_t entry_to_handle( struct user_handle *ptr )
46 {
47     int index = ptr - handles;
48     return (index + FIRST_USER_HANDLE) + (ptr->generation << 16);
49 }
50
51 inline static struct user_handle *alloc_user_entry(void)
52 {
53     struct user_handle *handle;
54
55     if (freelist)
56     {
57         handle = freelist;
58         freelist = handle->ptr;
59         return handle;
60     }
61     if (nb_handles >= allocated_handles)  /* need to grow the array */
62     {
63         struct user_handle *new_handles;
64         /* grow array by 50% (but at minimum 32 entries) */
65         int growth = max( 32, allocated_handles / 2 );
66         int new_size = min( allocated_handles + growth, LAST_USER_HANDLE-FIRST_USER_HANDLE+1 );
67         if (new_size <= allocated_handles) return NULL;
68         if (!(new_handles = realloc( handles, new_size * sizeof(*handles) )))
69             return NULL;
70         handles = new_handles;
71         allocated_handles = new_size;
72     }
73     handle = &handles[nb_handles++];
74     handle->generation = 0;
75     return handle;
76 }
77
78 inline static void *free_user_entry( struct user_handle *ptr )
79 {
80     void *ret;
81     ret = ptr->ptr;
82     ptr->ptr  = freelist;
83     ptr->type = 0;
84     freelist  = ptr;
85     return ret;
86 }
87
88 /* allocate a user handle for a given object */
89 user_handle_t alloc_user_handle( void *ptr, enum user_object type )
90 {
91     struct user_handle *entry = alloc_user_entry();
92     if (!entry) return 0;
93     entry->ptr  = ptr;
94     entry->type = type;
95     if (++entry->generation >= 0xffff) entry->generation = 1;
96     return entry_to_handle( entry );
97 }
98
99 /* return a pointer to a user object from its handle */
100 void *get_user_object( user_handle_t handle, enum user_object type )
101 {
102     struct user_handle *entry;
103
104     if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
105     return entry->ptr;
106 }
107
108 /* get the full handle for a possibly truncated handle */
109 user_handle_t get_user_full_handle( user_handle_t handle )
110 {
111     struct user_handle *entry;
112
113     if (handle >> 16) return handle;
114     if (!(entry = handle_to_entry( handle ))) return handle;
115     return entry_to_handle( entry );
116 }
117
118 /* same as get_user_object plus set the handle to the full 32-bit value */
119 void *get_user_object_handle( user_handle_t *handle, enum user_object type )
120 {
121     struct user_handle *entry;
122
123     if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
124     *handle = entry_to_handle( entry );
125     return entry->ptr;
126 }
127
128 /* free a user handle and return a pointer to the object */
129 void *free_user_handle( user_handle_t handle )
130 {
131     struct user_handle *entry;
132
133     if (!(entry = handle_to_entry( handle )))
134     {
135         set_error( STATUS_INVALID_HANDLE );
136         return NULL;
137     }
138     return free_user_entry( entry );
139 }
140
141 /* return the next user handle after 'handle' that is of a given type */
142 void *next_user_handle( user_handle_t *handle, enum user_object type )
143 {
144     struct user_handle *entry;
145
146     if (!*handle) entry = handles;
147     else
148     {
149         if (!(entry = handle_to_entry( *handle ))) return NULL;
150         entry++;  /* start from the next one */
151     }
152     while (entry < handles + nb_handles)
153     {
154         if (!type || entry->type == type)
155         {
156             *handle = entry_to_handle( entry );
157             return entry->ptr;
158         }
159         entry++;
160     }
161     return NULL;
162 }