Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side USER handles |
| 3 | * |
| 4 | * Copyright (C) 2001 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "thread.h" |
| 22 | #include "user.h" |
Alexandre Julliard | 9018e13 | 2009-10-12 14:25:01 +0200 | [diff] [blame] | 23 | #include "request.h" |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 37 | static struct user_handle *handle_to_entry( user_handle_t handle ) |
| 38 | { |
Alexandre Julliard | 90af05f | 2005-05-07 15:03:00 +0000 | [diff] [blame] | 39 | unsigned short generation; |
Alexandre Julliard | d764107 | 2008-12-08 16:57:38 +0100 | [diff] [blame] | 40 | int index = ((handle & 0xffff) - FIRST_USER_HANDLE) >> 1; |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 41 | if (index < 0 || index >= nb_handles) return NULL; |
| 42 | if (!handles[index].type) return NULL; |
Alexandre Julliard | d764107 | 2008-12-08 16:57:38 +0100 | [diff] [blame] | 43 | generation = handle >> 16; |
Alexandre Julliard | 90af05f | 2005-05-07 15:03:00 +0000 | [diff] [blame] | 44 | if (generation == handles[index].generation || !generation || generation == 0xffff) |
| 45 | return &handles[index]; |
| 46 | return NULL; |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 47 | } |
| 48 | |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 49 | static inline user_handle_t entry_to_handle( struct user_handle *ptr ) |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 50 | { |
Alexandre Julliard | d764107 | 2008-12-08 16:57:38 +0100 | [diff] [blame] | 51 | unsigned int index = ptr - handles; |
| 52 | return (index << 1) + FIRST_USER_HANDLE + (ptr->generation << 16); |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 55 | static inline struct user_handle *alloc_user_entry(void) |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 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 ); |
Alexandre Julliard | ed8a41c | 2004-05-28 19:35:37 +0000 | [diff] [blame] | 70 | int new_size = min( allocated_handles + growth, (LAST_USER_HANDLE-FIRST_USER_HANDLE+1) >> 1 ); |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 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 | |
Andrew Talbot | b1788c8 | 2007-03-17 10:52:14 +0000 | [diff] [blame] | 82 | static inline void *free_user_entry( struct user_handle *ptr ) |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | a09da0c | 2001-09-21 21:08:40 +0000 | [diff] [blame] | 108 | if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL; |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 109 | return entry->ptr; |
| 110 | } |
| 111 | |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | d764107 | 2008-12-08 16:57:38 +0100 | [diff] [blame] | 117 | if (handle >> 16) return handle; |
Alexandre Julliard | bc878ef | 2001-09-12 17:09:24 +0000 | [diff] [blame] | 118 | if (!(entry = handle_to_entry( handle ))) return handle; |
| 119 | return entry_to_handle( entry ); |
| 120 | } |
| 121 | |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 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 | |
Alexandre Julliard | a09da0c | 2001-09-21 21:08:40 +0000 | [diff] [blame] | 127 | if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL; |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 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 | { |
Alexandre Julliard | d764107 | 2008-12-08 16:57:38 +0100 | [diff] [blame] | 153 | int index = ((*handle & 0xffff) - FIRST_USER_HANDLE) >> 1; |
Alexandre Julliard | 65adc24 | 2002-05-31 18:25:53 +0000 | [diff] [blame] | 154 | if (index < 0 || index >= nb_handles) return NULL; |
| 155 | entry = handles + index + 1; /* start from the next one */ |
Alexandre Julliard | 1a66d22 | 2001-08-28 18:44:52 +0000 | [diff] [blame] | 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 | } |
Alexandre Julliard | 9018e13 | 2009-10-12 14:25:01 +0200 | [diff] [blame] | 168 | |
Alexandre Julliard | 538b247 | 2009-10-15 17:56:04 +0200 | [diff] [blame] | 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 | |
Alexandre Julliard | 9018e13 | 2009-10-12 14:25:01 +0200 | [diff] [blame] | 179 | /* allocate an arbitrary user handle */ |
| 180 | DECL_HANDLER(alloc_user_handle) |
| 181 | { |
Alexandre Julliard | 538b247 | 2009-10-15 17:56:04 +0200 | [diff] [blame] | 182 | reply->handle = alloc_user_handle( current->process, USER_CLIENT ); |
Alexandre Julliard | 9018e13 | 2009-10-12 14:25:01 +0200 | [diff] [blame] | 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 | } |