Cosmetic fixes.
[wine/multimedia.git] / server / user.c
blobe28189c7ab01029f0332b82e99c90ab0ab00a2e6
1 /*
2 * Server-side USER handles
4 * Copyright (C) 2001 Alexandre Julliard
5 */
7 #include "thread.h"
8 #include "user.h"
10 struct user_handle
12 void *ptr; /* pointer to object */
13 unsigned short type; /* object type (0 if free) */
14 unsigned short generation; /* generation counter */
17 static struct user_handle *handles;
18 static struct user_handle *freelist;
19 static int nb_handles;
20 static int allocated_handles;
22 static struct user_handle *handle_to_entry( user_handle_t handle )
24 int index = (handle & 0xffff) - FIRST_USER_HANDLE;
25 if (index < 0 || index >= nb_handles) return NULL;
26 if (!handles[index].type) return NULL;
27 if ((handle >> 16) && (handle >> 16 != handles[index].generation)) return NULL;
28 return &handles[index];
31 inline static user_handle_t entry_to_handle( struct user_handle *ptr )
33 int index = ptr - handles;
34 return (index + FIRST_USER_HANDLE) + (ptr->generation << 16);
37 inline static struct user_handle *alloc_user_entry(void)
39 struct user_handle *handle;
41 if (freelist)
43 handle = freelist;
44 freelist = handle->ptr;
45 return handle;
47 if (nb_handles >= allocated_handles) /* need to grow the array */
49 struct user_handle *new_handles;
50 /* grow array by 50% (but at minimum 32 entries) */
51 int growth = max( 32, allocated_handles / 2 );
52 int new_size = min( allocated_handles + growth, LAST_USER_HANDLE-FIRST_USER_HANDLE+1 );
53 if (new_size <= allocated_handles) return NULL;
54 if (!(new_handles = realloc( handles, new_size * sizeof(*handles) )))
55 return NULL;
56 handles = new_handles;
57 allocated_handles = new_size;
59 handle = &handles[nb_handles++];
60 handle->generation = 0;
61 return handle;
64 inline static void *free_user_entry( struct user_handle *ptr )
66 void *ret;
67 ret = ptr->ptr;
68 ptr->ptr = freelist;
69 ptr->type = 0;
70 freelist = ptr;
71 return ret;
74 /* allocate a user handle for a given object */
75 user_handle_t alloc_user_handle( void *ptr, enum user_object type )
77 struct user_handle *entry = alloc_user_entry();
78 if (!entry) return 0;
79 entry->ptr = ptr;
80 entry->type = type;
81 if (++entry->generation >= 0xffff) entry->generation = 1;
82 return entry_to_handle( entry );
85 /* return a pointer to a user object from its handle */
86 void *get_user_object( user_handle_t handle, enum user_object type )
88 struct user_handle *entry;
90 if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
91 return entry->ptr;
94 /* get the full handle for a possibly truncated handle */
95 user_handle_t get_user_full_handle( user_handle_t handle )
97 struct user_handle *entry;
99 if (handle >> 16) return handle;
100 if (!(entry = handle_to_entry( handle ))) return handle;
101 return entry_to_handle( entry );
104 /* same as get_user_object plus set the handle to the full 32-bit value */
105 void *get_user_object_handle( user_handle_t *handle, enum user_object type )
107 struct user_handle *entry;
109 if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
110 *handle = entry_to_handle( entry );
111 return entry->ptr;
114 /* free a user handle and return a pointer to the object */
115 void *free_user_handle( user_handle_t handle )
117 struct user_handle *entry;
119 if (!(entry = handle_to_entry( handle )))
121 set_error( STATUS_INVALID_HANDLE );
122 return NULL;
124 return free_user_entry( entry );
127 /* return the next user handle after 'handle' that is of a given type */
128 void *next_user_handle( user_handle_t *handle, enum user_object type )
130 struct user_handle *entry;
132 if (!*handle) entry = handles;
133 else
135 if (!(entry = handle_to_entry( *handle ))) return NULL;
136 entry++; /* start from the next one */
138 while (entry < handles + nb_handles)
140 if (!type || entry->type == type)
142 *handle = entry_to_handle( entry );
143 return entry->ptr;
145 entry++;
147 return NULL;