Move the FPS computation from the D3D code to the common code.
[wine/multimedia.git] / server / object.c
blobefd97b8516ee2b6696f7ede6461078bad8cd441b
1 /*
2 * Server-side objects
4 * Copyright (C) 1998 Alexandre Julliard
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.
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.
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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include "file.h"
32 #include "thread.h"
33 #include "unicode.h"
36 struct object_name
38 struct list entry; /* entry in the hash list */
39 struct object *obj;
40 size_t len;
41 WCHAR name[1];
44 struct namespace
46 unsigned int hash_size; /* size of hash table */
47 int case_sensitive; /* are names case sensitive? */
48 struct list names[1]; /* array of hash entry lists */
52 #ifdef DEBUG_OBJECTS
53 static struct list object_list = LIST_INIT(object_list);
55 void dump_objects(void)
57 struct list *p;
59 LIST_FOR_EACH( p, &object_list )
61 struct object *ptr = LIST_ENTRY( p, struct object, obj_list );
62 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
63 ptr->ops->dump( ptr, 1 );
66 #endif
68 /*****************************************************************/
70 /* malloc replacement */
71 void *mem_alloc( size_t size )
73 void *ptr = malloc( size );
74 if (ptr) memset( ptr, 0x55, size );
75 else set_error( STATUS_NO_MEMORY );
76 return ptr;
79 /* duplicate a block of memory */
80 void *memdup( const void *data, size_t len )
82 void *ptr = malloc( len );
83 if (ptr) memcpy( ptr, data, len );
84 else set_error( STATUS_NO_MEMORY );
85 return ptr;
89 /*****************************************************************/
91 static int get_name_hash( const struct namespace *namespace, const WCHAR *name, size_t len )
93 WCHAR hash = 0;
94 len /= sizeof(WCHAR);
95 if (namespace->case_sensitive) while (len--) hash ^= *name++;
96 else while (len--) hash ^= tolowerW(*name++);
97 return hash % namespace->hash_size;
100 /* allocate a name for an object */
101 static struct object_name *alloc_name( const WCHAR *name, size_t len )
103 struct object_name *ptr;
105 if ((ptr = mem_alloc( sizeof(*ptr) + len - sizeof(ptr->name) )))
107 ptr->len = len;
108 memcpy( ptr->name, name, len );
110 return ptr;
113 /* free the name of an object */
114 static void free_name( struct object *obj )
116 struct object_name *ptr = obj->name;
117 list_remove( &ptr->entry );
118 free( ptr );
121 /* set the name of an existing object */
122 static void set_object_name( struct namespace *namespace,
123 struct object *obj, struct object_name *ptr )
125 int hash = get_name_hash( namespace, ptr->name, ptr->len );
127 list_add_head( &namespace->names[hash], &ptr->entry );
128 ptr->obj = obj;
129 obj->name = ptr;
132 /* allocate and initialize an object */
133 void *alloc_object( const struct object_ops *ops )
135 struct object *obj = mem_alloc( ops->size );
136 if (obj)
138 obj->refcount = 1;
139 obj->ops = ops;
140 obj->head = NULL;
141 obj->tail = NULL;
142 obj->name = NULL;
143 #ifdef DEBUG_OBJECTS
144 list_add_head( &object_list, &obj->obj_list );
145 #endif
146 return obj;
148 return NULL;
151 void *create_named_object( struct namespace *namespace, const struct object_ops *ops,
152 const WCHAR *name, size_t len )
154 struct object *obj;
155 struct object_name *name_ptr;
157 if (!name || !len) return alloc_object( ops );
159 if ((obj = find_object( namespace, name, len )))
161 if (obj->ops == ops)
163 set_error( STATUS_OBJECT_NAME_COLLISION );
164 return obj;
166 set_error( STATUS_OBJECT_TYPE_MISMATCH );
167 return NULL;
169 if (!(name_ptr = alloc_name( name, len ))) return NULL;
170 if ((obj = alloc_object( ops )))
172 set_object_name( namespace, obj, name_ptr );
173 clear_error();
175 else free( name_ptr );
176 return obj;
179 /* dump the name of an object to stderr */
180 void dump_object_name( struct object *obj )
182 if (!obj->name) fprintf( stderr, "name=\"\"" );
183 else
185 fprintf( stderr, "name=L\"" );
186 dump_strW( obj->name->name, obj->name->len/sizeof(WCHAR), stderr, "\"\"" );
187 fputc( '\"', stderr );
191 /* grab an object (i.e. increment its refcount) and return the object */
192 struct object *grab_object( void *ptr )
194 struct object *obj = (struct object *)ptr;
195 assert( obj->refcount < INT_MAX );
196 obj->refcount++;
197 return obj;
200 /* release an object (i.e. decrement its refcount) */
201 void release_object( void *ptr )
203 struct object *obj = (struct object *)ptr;
204 assert( obj->refcount );
205 if (!--obj->refcount)
207 /* if the refcount is 0, nobody can be in the wait queue */
208 assert( !obj->head );
209 assert( !obj->tail );
210 obj->ops->destroy( obj );
211 if (obj->name) free_name( obj );
212 #ifdef DEBUG_OBJECTS
213 list_remove( &obj->obj_list );
214 memset( obj, 0xaa, obj->ops->size );
215 #endif
216 free( obj );
220 /* find an object by its name; the refcount is incremented */
221 struct object *find_object( const struct namespace *namespace, const WCHAR *name, size_t len )
223 const struct list *list, *p;
225 if (!name || !len) return NULL;
227 list = &namespace->names[ get_name_hash( namespace, name, len ) ];
228 if (namespace->case_sensitive)
230 LIST_FOR_EACH( p, list )
232 struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
233 if (ptr->len != len) continue;
234 if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
237 else
239 LIST_FOR_EACH( p, list )
241 struct object_name *ptr = LIST_ENTRY( p, struct object_name, entry );
242 if (ptr->len != len) continue;
243 if (!strncmpiW( ptr->name, name, len/sizeof(WCHAR) )) return grab_object( ptr->obj );
246 return NULL;
249 /* allocate a namespace */
250 struct namespace *create_namespace( unsigned int hash_size, int case_sensitive )
252 struct namespace *namespace;
253 unsigned int i;
255 namespace = mem_alloc( sizeof(*namespace) + (hash_size - 1) * sizeof(namespace->names[0]) );
256 if (namespace)
258 namespace->hash_size = hash_size;
259 namespace->case_sensitive = case_sensitive;
260 for (i = 0; i < hash_size; i++) list_init( &namespace->names[i] );
262 return namespace;
265 /* functions for unimplemented/default object operations */
267 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
269 set_error( STATUS_OBJECT_TYPE_MISMATCH );
270 return 0;
273 int no_satisfied( struct object *obj, struct thread *thread )
275 return 0; /* not abandoned */
278 struct fd *no_get_fd( struct object *obj )
280 set_error( STATUS_OBJECT_TYPE_MISMATCH );
281 return NULL;
284 void no_destroy( struct object *obj )