Release 970305
[wine/multimedia.git] / win32 / k32obj.c
blobbcb056518b87c716cdc1ec00bbcf3430f1eb5228
1 /*
2 * KERNEL32 objects
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include "winerror.h"
9 #include "handle32.h"
10 #include "heap.h"
11 #include "file.h"
12 #include "process.h"
13 #include "thread.h"
15 typedef void (*destroy_object)(K32OBJ *);
17 extern void VIRTUAL_DestroyMapping( K32OBJ *obj );
19 static const destroy_object K32OBJ_Destroy[K32OBJ_NBOBJECTS] =
21 NULL,
22 NULL, /* K32OBJ_SEMAPHORE */
23 NULL, /* K32OBJ_EVENT */
24 NULL, /* K32OBJ_MUTEX */
25 NULL, /* K32OBJ_CRITICAL_SECTION */
26 PROCESS_Destroy, /* K32OBJ_PROCESS */
27 THREAD_Destroy, /* K32OBJ_THREAD */
28 FILE_Destroy, /* K32OBJ_FILE */
29 NULL, /* K32OBJ_CHANGE */
30 NULL, /* K32OBJ_CONSOLE */
31 NULL, /* K32OBJ_SCREEN_BUFFER */
32 VIRTUAL_DestroyMapping, /* K32OBJ_MEM_MAPPED_FILE */
33 NULL, /* K32OBJ_SERIAL */
34 NULL, /* K32OBJ_DEVICE_IOCTL */
35 NULL, /* K32OBJ_PIPE */
36 NULL, /* K32OBJ_MAILSLOT */
37 NULL, /* K32OBJ_TOOLHELP_SNAPSHOT */
38 NULL /* K32OBJ_SOCKET */
41 typedef struct _NE
43 struct _NE *next;
44 K32OBJ *obj;
45 UINT32 len;
46 char name[1];
47 } NAME_ENTRY;
49 static NAME_ENTRY *K32OBJ_FirstEntry = NULL;
52 /***********************************************************************
53 * K32OBJ_IncCount
55 void K32OBJ_IncCount( K32OBJ *ptr )
57 /* FIXME: not atomic */
58 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
59 ptr->refcount++;
63 /***********************************************************************
64 * K32OBJ_DecCount
66 void K32OBJ_DecCount( K32OBJ *ptr )
68 NAME_ENTRY **pptr;
70 /* FIXME: not atomic */
71 assert( ptr->type && ((unsigned)ptr->type < K32OBJ_NBOBJECTS) );
72 assert( ptr->refcount );
73 if (--ptr->refcount) return;
75 /* Check if the object has a name entry and free it */
77 pptr = &K32OBJ_FirstEntry;
78 while (*pptr && ((*pptr)->obj != ptr)) pptr = &(*pptr)->next;
79 if (*pptr)
81 NAME_ENTRY *entry = *pptr;
82 *pptr = entry->next;
83 HeapFree( SystemHeap, 0, entry );
86 /* Free the object */
88 if (K32OBJ_Destroy[ptr->type]) K32OBJ_Destroy[ptr->type]( ptr );
92 /***********************************************************************
93 * K32OBJ_AddName
95 * Add a name entry for an object. We don't check for duplicates here.
96 * FIXME: should use some sort of hashing.
98 BOOL32 K32OBJ_AddName( K32OBJ *obj, LPCSTR name )
100 NAME_ENTRY *entry = K32OBJ_FirstEntry;
101 UINT32 len = strlen( name );
103 if (!(entry = HeapAlloc( SystemHeap, 0, sizeof(entry) + len )))
105 SetLastError( ERROR_OUTOFMEMORY );
106 return FALSE;
108 entry->next = K32OBJ_FirstEntry;
109 entry->obj = obj;
110 lstrcpy32A( entry->name, name );
111 K32OBJ_FirstEntry = entry;
112 return TRUE;
116 /***********************************************************************
117 * K32OBJ_FindName
119 * Find the object referenced by a given name.
120 * The reference count is not incremented.
122 K32OBJ *K32OBJ_FindName( LPCSTR name )
124 NAME_ENTRY *entry = K32OBJ_FirstEntry;
125 UINT32 len;
127 if (!name) return NULL; /* Anonymous object */
128 len = strlen( name );
129 while (entry)
131 if ((len == entry->len) && !lstrcmp32A( name, entry->name))
132 return entry->obj;
133 entry = entry->next;
135 return NULL;
139 /***********************************************************************
140 * K32OBJ_FindNameType
142 * Find an object by name and check its type.
143 * The reference count is not incremented.
145 K32OBJ *K32OBJ_FindNameType( LPCSTR name, K32OBJ_TYPE type )
147 K32OBJ *obj = K32OBJ_FindName( name );
148 if (!obj) return NULL;
149 if (obj->type == type) return obj;
150 SetLastError( ERROR_DUP_NAME );
151 return NULL;