Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbeyj@wpi.edu>
[wine] / server / object.c
1 /*
2  * Server-side objects
3  * These are the server equivalent of K32OBJ
4  *
5  * Copyright (C) 1998 Alexandre Julliard
6  */
7
8 #include <assert.h>
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "winerror.h"
14 #include "thread.h"
15
16 int debug_level = 0;
17
18 struct object_name
19 {
20     struct object_name *next;
21     struct object      *obj;
22     int                 len;
23     char                name[1];
24 };
25
26 #define NAME_HASH_SIZE 37
27
28 static struct object_name *names[NAME_HASH_SIZE];
29
30 /*****************************************************************/
31
32 void *mem_alloc( size_t size )
33 {
34     void *ptr = malloc( size );
35     if (ptr) memset( ptr, 0x55, size );
36     else if (current) SET_ERROR( ERROR_OUTOFMEMORY );
37     return ptr;
38 }
39
40 /*****************************************************************/
41
42 static int get_name_hash( const char *name )
43 {
44     int hash = 0;
45     while (*name) hash ^= *name++;
46     return hash % NAME_HASH_SIZE;
47 }
48
49 static struct object_name *add_name( struct object *obj, const char *name )
50 {
51     struct object_name *ptr;
52     int hash = get_name_hash( name );
53     int len = strlen( name );
54
55     if (!(ptr = (struct object_name *)mem_alloc( sizeof(*ptr) + len )))
56         return NULL;
57     ptr->next = names[hash];
58     ptr->obj  = obj;
59     ptr->len  = len;
60     strcpy( ptr->name, name );
61     names[hash] = ptr;
62     return ptr;
63 }
64
65 static void free_name( struct object *obj )
66 {
67     int hash = get_name_hash( obj->name->name );
68     struct object_name **pptr = &names[hash];
69     while (*pptr && *pptr != obj->name) pptr = &(*pptr)->next;
70     assert( *pptr );
71     *pptr = (*pptr)->next;
72     free( obj->name );
73 }
74
75 /* initialize an already allocated object */
76 /* return 1 if OK, 0 on error */
77 int init_object( struct object *obj, const struct object_ops *ops,
78                  const char *name )
79 {
80     obj->refcount = 1;
81     obj->ops      = ops;
82     obj->head     = NULL;
83     obj->tail     = NULL;
84     if (!name) obj->name = NULL;
85     else if (!(obj->name = add_name( obj, name ))) return 0;
86     return 1;
87 }
88
89 struct object *create_named_object( const char *name, const struct object_ops *ops, size_t size )
90 {
91     struct object *obj;
92     if ((obj = find_object( name )))
93     {
94         if (obj->ops == ops)
95         {
96             SET_ERROR( ERROR_ALREADY_EXISTS );
97             return obj;
98         }
99         SET_ERROR( ERROR_INVALID_HANDLE );
100         return NULL;
101     }
102     if (!(obj = mem_alloc( size ))) return NULL;
103     if (!init_object( obj, ops, name ))
104     {
105         free( obj );
106         return NULL;
107     }
108     CLEAR_ERROR();
109     return obj;
110 }
111
112 /* return a pointer to the object name, or to an empty string */
113 const char *get_object_name( struct object *obj )
114 {
115     if (!obj->name) return "";
116     return obj->name->name;
117 }
118
119 /* grab an object (i.e. increment its refcount) and return the object */
120 struct object *grab_object( void *ptr )
121 {
122     struct object *obj = (struct object *)ptr;
123     assert( obj->refcount < INT_MAX );
124     obj->refcount++;
125     return obj;
126 }
127
128 /* release an object (i.e. decrement its refcount) */
129 void release_object( void *ptr )
130 {
131     struct object *obj = (struct object *)ptr;
132     assert( obj->refcount );
133     if (!--obj->refcount)
134     {
135         /* if the refcount is 0, nobody can be in the wait queue */
136         assert( !obj->head );
137         assert( !obj->tail );
138         if (obj->name) free_name( obj );
139         obj->ops->destroy( obj );
140     }
141 }
142
143 /* find an object by its name; the refcount is incremented */
144 struct object *find_object( const char *name )
145 {
146     struct object_name *ptr;
147     if (!name) return NULL;
148     ptr = names[ get_name_hash( name ) ];
149     while (ptr && strcmp( ptr->name, name )) ptr = ptr->next;
150     if (!ptr) return NULL;
151     return grab_object( ptr->obj );
152 }
153
154 /* functions for unimplemented object operations */
155
156 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
157 {
158     SET_ERROR( ERROR_INVALID_HANDLE );
159     return 0;
160 }
161
162 int no_satisfied( struct object *obj, struct thread *thread )
163 {
164     return 0;  /* not abandoned */
165 }
166
167 int no_read_fd( struct object *obj )
168 {
169     SET_ERROR( ERROR_INVALID_HANDLE );
170     return -1;
171 }
172
173 int no_write_fd( struct object *obj )
174 {
175     SET_ERROR( ERROR_INVALID_HANDLE );
176     return -1;
177 }
178
179 int no_flush( struct object *obj )
180 {
181     SET_ERROR( ERROR_INVALID_HANDLE );
182     return 0;
183 }
184
185 int no_get_file_info( struct object *obj, struct get_file_info_reply *info )
186 {
187     SET_ERROR( ERROR_INVALID_HANDLE );
188     return 0;
189 }
190
191 void default_select_event( int event, void *private )
192 {
193     struct object *obj = (struct object *)private;
194     assert( obj );
195     wake_up( obj, 0 );
196 }