Added TEB in init_thread request.
[wine/multimedia.git] / server / object.c
blobb907af3e2bb7baf2d2a4bcd329abff812eacde04
1 /*
2 * Server-side objects
3 * These are the server equivalent of K32OBJ
5 * Copyright (C) 1998 Alexandre Julliard
6 */
8 #include <assert.h>
9 #include <limits.h>
10 #include <stdlib.h>
11 #include <string.h>
13 #include "winerror.h"
14 #include "thread.h"
16 int debug_level = 0;
18 struct object_name
20 struct object_name *next;
21 struct object *obj;
22 int len;
23 char name[1];
26 #define NAME_HASH_SIZE 37
28 static struct object_name *names[NAME_HASH_SIZE];
30 /*****************************************************************/
32 void *mem_alloc( size_t size )
34 void *ptr = malloc( size );
35 if (ptr) memset( ptr, 0x55, size );
36 else if (current) SET_ERROR( ERROR_OUTOFMEMORY );
37 return ptr;
40 /*****************************************************************/
42 static int get_name_hash( const char *name )
44 int hash = 0;
45 while (*name) hash ^= *name++;
46 return hash % NAME_HASH_SIZE;
49 static struct object_name *add_name( struct object *obj, const char *name )
51 struct object_name *ptr;
52 int hash = get_name_hash( name );
53 int len = strlen( name );
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;
65 static void free_name( struct object *obj )
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 );
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 )
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;
89 struct object *create_named_object( const char *name, const struct object_ops *ops, size_t size )
91 struct object *obj;
92 if ((obj = find_object( name )))
94 if (obj->ops == ops)
96 SET_ERROR( ERROR_ALREADY_EXISTS );
97 return obj;
99 SET_ERROR( ERROR_INVALID_HANDLE );
100 return NULL;
102 if (!(obj = mem_alloc( size ))) return NULL;
103 if (!init_object( obj, ops, name ))
105 free( obj );
106 return NULL;
108 CLEAR_ERROR();
109 return obj;
112 /* return a pointer to the object name, or to an empty string */
113 const char *get_object_name( struct object *obj )
115 if (!obj->name) return "";
116 return obj->name->name;
119 /* grab an object (i.e. increment its refcount) and return the object */
120 struct object *grab_object( void *ptr )
122 struct object *obj = (struct object *)ptr;
123 assert( obj->refcount < INT_MAX );
124 obj->refcount++;
125 return obj;
128 /* release an object (i.e. decrement its refcount) */
129 void release_object( void *ptr )
131 struct object *obj = (struct object *)ptr;
132 assert( obj->refcount );
133 if (!--obj->refcount)
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 );
143 /* find an object by its name; the refcount is incremented */
144 struct object *find_object( const char *name )
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 );
154 /* functions for unimplemented object operations */
156 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
158 SET_ERROR( ERROR_INVALID_HANDLE );
159 return 0;
162 int no_satisfied( struct object *obj, struct thread *thread )
164 return 0; /* not abandoned */
167 int no_read_fd( struct object *obj )
169 SET_ERROR( ERROR_INVALID_HANDLE );
170 return -1;
173 int no_write_fd( struct object *obj )
175 SET_ERROR( ERROR_INVALID_HANDLE );
176 return -1;
179 int no_flush( struct object *obj )
181 SET_ERROR( ERROR_INVALID_HANDLE );
182 return 0;
185 int no_get_file_info( struct object *obj, struct get_file_info_reply *info )
187 SET_ERROR( ERROR_INVALID_HANDLE );
188 return 0;
191 void default_select_event( int fd, int event, void *private )
193 struct object *obj = (struct object *)private;
194 assert( obj );
195 wake_up( obj, 0 );