Send WM_CTLCOLOREDIT not WM_CTLCOLORSTATIC messages to parent of a
[wine.git] / server / object.c
blobd50fd7d7052ef1f0232aafcc108fc12efca42be6
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 <stdio.h>
12 #include <string.h>
14 #include "winerror.h"
15 #include "thread.h"
17 int debug_level = 0;
19 struct object_name
21 struct object_name *next;
22 struct object_name *prev;
23 struct object *obj;
24 size_t len;
25 char name[1];
28 #define NAME_HASH_SIZE 37
30 static struct object_name *names[NAME_HASH_SIZE];
32 #ifdef DEBUG_OBJECTS
33 static struct object *first;
35 void dump_objects(void)
37 struct object *ptr = first;
38 while (ptr)
40 fprintf( stderr, "%p:%d: ", ptr, ptr->refcount );
41 ptr->ops->dump( ptr, 1 );
42 ptr = ptr->next;
45 #endif
47 /*****************************************************************/
49 /* malloc replacement */
50 void *mem_alloc( size_t size )
52 void *ptr = malloc( size );
53 if (ptr) memset( ptr, 0x55, size );
54 else if (current) set_error( ERROR_OUTOFMEMORY );
55 return ptr;
58 /*****************************************************************/
60 static int get_name_hash( const char *name, size_t len )
62 char hash = 0;
63 while (len--) hash ^= *name++;
64 return hash % NAME_HASH_SIZE;
67 /* allocate a name for an object */
68 static struct object_name *alloc_name( const char *name, size_t len )
70 struct object_name *ptr;
72 if ((ptr = mem_alloc( sizeof(*ptr) + len )))
74 ptr->len = len;
75 memcpy( ptr->name, name, len );
76 ptr->name[len] = 0;
78 return ptr;
81 /* free the name of an object */
82 static void free_name( struct object *obj )
84 struct object_name *ptr = obj->name;
85 if (ptr->next) ptr->next->prev = ptr->prev;
86 if (ptr->prev) ptr->prev->next = ptr->next;
87 else
89 int hash;
90 for (hash = 0; hash < NAME_HASH_SIZE; hash++)
91 if (names[hash] == ptr)
93 names[hash] = ptr->next;
94 break;
97 free( ptr );
100 /* set the name of an existing object */
101 static void set_object_name( struct object *obj, struct object_name *ptr )
103 int hash = get_name_hash( ptr->name, ptr->len );
105 if ((ptr->next = names[hash]) != NULL) ptr->next->prev = ptr;
106 ptr->obj = obj;
107 ptr->prev = NULL;
108 names[hash] = ptr;
109 assert( !obj->name );
110 obj->name = ptr;
113 /* allocate and initialize an object */
114 void *alloc_object( const struct object_ops *ops )
116 struct object *obj = mem_alloc( ops->size );
117 if (obj)
119 obj->refcount = 1;
120 obj->ops = ops;
121 obj->head = NULL;
122 obj->tail = NULL;
123 obj->name = NULL;
124 #ifdef DEBUG_OBJECTS
125 obj->prev = NULL;
126 if ((obj->next = first) != NULL) obj->next->prev = obj;
127 first = obj;
128 #endif
130 return obj;
133 void *create_named_object( const struct object_ops *ops, const char *name, size_t len )
135 struct object *obj;
136 struct object_name *name_ptr;
138 if (!name || !len) return alloc_object( ops );
139 if (!(name_ptr = alloc_name( name, len ))) return NULL;
141 if ((obj = find_object( name_ptr->name, name_ptr->len )))
143 free( name_ptr ); /* we no longer need it */
144 if (obj->ops == ops)
146 set_error( ERROR_ALREADY_EXISTS );
147 return obj;
149 set_error( ERROR_INVALID_HANDLE );
150 return NULL;
152 if ((obj = alloc_object( ops )))
154 set_object_name( obj, name_ptr );
155 clear_error();
157 else free( name_ptr );
158 return obj;
161 /* return a pointer to the object name, or to an empty string */
162 const char *get_object_name( struct object *obj )
164 if (!obj->name) return "";
165 return obj->name->name;
168 /* grab an object (i.e. increment its refcount) and return the object */
169 struct object *grab_object( void *ptr )
171 struct object *obj = (struct object *)ptr;
172 assert( obj->refcount < INT_MAX );
173 obj->refcount++;
174 return obj;
177 /* release an object (i.e. decrement its refcount) */
178 void release_object( void *ptr )
180 struct object *obj = (struct object *)ptr;
181 assert( obj->refcount );
182 if (!--obj->refcount)
184 /* if the refcount is 0, nobody can be in the wait queue */
185 assert( !obj->head );
186 assert( !obj->tail );
187 if (obj->name) free_name( obj );
188 #ifdef DEBUG_OBJECTS
189 if (obj->next) obj->next->prev = obj->prev;
190 if (obj->prev) obj->prev->next = obj->next;
191 else first = obj->next;
192 #endif
193 obj->ops->destroy( obj );
194 memset( obj, 0xaa, obj->ops->size );
195 free( obj );
199 /* find an object by its name; the refcount is incremented */
200 struct object *find_object( const char *name, size_t len )
202 struct object_name *ptr;
203 if (!name || !len) return NULL;
204 for (ptr = names[ get_name_hash( name, len ) ]; ptr; ptr = ptr->next)
206 if (ptr->len != len) continue;
207 if (!memcmp( ptr->name, name, len )) return grab_object( ptr->obj );
209 return NULL;
212 /* functions for unimplemented object operations */
214 int no_add_queue( struct object *obj, struct wait_queue_entry *entry )
216 set_error( ERROR_INVALID_HANDLE );
217 return 0;
220 int no_satisfied( struct object *obj, struct thread *thread )
222 return 0; /* not abandoned */
225 int no_read_fd( struct object *obj )
227 set_error( ERROR_INVALID_HANDLE );
228 return -1;
231 int no_write_fd( struct object *obj )
233 set_error( ERROR_INVALID_HANDLE );
234 return -1;
237 int no_flush( struct object *obj )
239 set_error( ERROR_INVALID_HANDLE );
240 return 0;
243 int no_get_file_info( struct object *obj, struct get_file_info_request *info )
245 set_error( ERROR_INVALID_HANDLE );
246 return 0;
249 void no_destroy( struct object *obj )
253 void default_select_event( int event, void *private )
255 struct object *obj = (struct object *)private;
256 assert( obj );
257 wake_up( obj, 0 );