Save the registry before exiting on a SIGTERM.
[wine] / server / user.c
1 /*
2  * Server-side USER handles
3  *
4  * Copyright (C) 2001 Alexandre Julliard
5  */
6
7 #include "thread.h"
8 #include "user.h"
9
10 struct user_handle
11 {
12     void          *ptr;          /* pointer to object */
13     unsigned short type;         /* object type (0 if free) */
14     unsigned short generation;   /* generation counter */
15 };
16
17 static struct user_handle *handles;
18 static struct user_handle *freelist;
19 static int nb_handles;
20 static int allocated_handles;
21
22 static struct user_handle *handle_to_entry( user_handle_t handle )
23 {
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];
29 }
30
31 inline static user_handle_t entry_to_handle( struct user_handle *ptr )
32 {
33     int index = ptr - handles;
34     return (index + FIRST_USER_HANDLE) + (ptr->generation << 16);
35 }
36
37 inline static struct user_handle *alloc_user_entry(void)
38 {
39     struct user_handle *handle;
40
41     if (freelist)
42     {
43         handle = freelist;
44         freelist = handle->ptr;
45         return handle;
46     }
47     if (nb_handles >= allocated_handles)  /* need to grow the array */
48     {
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;
58     }
59     handle = &handles[nb_handles++];
60     handle->generation = 0;
61     return handle;
62 }
63
64 inline static void *free_user_entry( struct user_handle *ptr )
65 {
66     void *ret;
67     ret = ptr->ptr;
68     ptr->ptr  = freelist;
69     ptr->type = 0;
70     freelist  = ptr;
71     return ret;
72 }
73
74 /* allocate a user handle for a given object */
75 user_handle_t alloc_user_handle( void *ptr, enum user_object type )
76 {
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 );
83 }
84
85 /* return a pointer to a user object from its handle */
86 void *get_user_object( user_handle_t handle, enum user_object type )
87 {
88     struct user_handle *entry;
89
90     if (!(entry = handle_to_entry( handle )) || entry->type != type) return NULL;
91     return entry->ptr;
92 }
93
94 /* get the full handle for a possibly truncated handle */
95 user_handle_t get_user_full_handle( user_handle_t handle )
96 {
97     struct user_handle *entry;
98
99     if (handle >> 16) return handle;
100     if (!(entry = handle_to_entry( handle ))) return handle;
101     return entry_to_handle( entry );
102 }
103
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 )
106 {
107     struct user_handle *entry;
108
109     if (!(entry = handle_to_entry( *handle )) || entry->type != type) return NULL;
110     *handle = entry_to_handle( entry );
111     return entry->ptr;
112 }
113
114 /* free a user handle and return a pointer to the object */
115 void *free_user_handle( user_handle_t handle )
116 {
117     struct user_handle *entry;
118
119     if (!(entry = handle_to_entry( handle )))
120     {
121         set_error( STATUS_INVALID_HANDLE );
122         return NULL;
123     }
124     return free_user_entry( entry );
125 }
126
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 )
129 {
130     struct user_handle *entry;
131
132     if (!*handle) entry = handles;
133     else
134     {
135         if (!(entry = handle_to_entry( *handle ))) return NULL;
136         entry++;  /* start from the next one */
137     }
138     while (entry < handles + nb_handles)
139     {
140         if (!type || entry->type == type)
141         {
142             *handle = entry_to_handle( entry );
143             return entry->ptr;
144         }
145         entry++;
146     }
147     return NULL;
148 }