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