ntdll: Add a test for NtNotifyChangeDirectoryFile.
[wine/multimedia.git] / dlls / netapi32 / nbnamecache.c
blob1fdb70553d4352f88b508ca682255d52a44b54f7
1 /* Copyright (c) 2003 Juan Lang
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * This implementation uses a linked list, because I don't have a decent
18 * hash table implementation handy. This is somewhat inefficient, but it's
19 * rather more efficient than not having a name cache at all.
22 #include "config.h"
23 #include "wine/port.h"
24 #include "wine/debug.h"
26 #include "nbnamecache.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(netbios);
30 typedef struct _NBNameCacheNode
32 DWORD expireTime;
33 NBNameCacheEntry *entry;
34 struct _NBNameCacheNode *next;
35 } NBNameCacheNode;
37 struct NBNameCache
39 HANDLE heap;
40 CRITICAL_SECTION cs;
41 DWORD entryExpireTimeMS;
42 NBNameCacheNode *head;
45 /* Unlinks the node pointed to by *prev, and frees any associated memory.
46 * If that node's next pointed to another node, *prev now points to it.
47 * Assumes the caller owns cache's lock.
49 static void NBNameCacheUnlinkNode(struct NBNameCache *cache,
50 NBNameCacheNode **prev)
52 if (cache && prev && *prev)
54 NBNameCacheNode *next = (*prev)->next;
56 HeapFree(cache->heap, 0, (*prev)->entry);
57 HeapFree(cache->heap, 0, *prev);
58 *prev = next;
62 /* Walks the list beginning with cache->head looking for the node with name
63 * name. If the node is found, returns a pointer to the next pointer of the
64 * node _prior_ to the found node (or head if head points to it). Thus, if the
65 * node's all you want, dereference the return value twice. If you want to
66 * modify the list, modify the referent of the return value.
67 * While it's at it, deletes nodes whose time has expired (except the node
68 * you're looking for, of course).
69 * Returns NULL if the node isn't found.
70 * Assumes the caller owns cache's lock.
72 static NBNameCacheNode **NBNameCacheWalk(struct NBNameCache *cache,
73 const char name[NCBNAMSZ])
75 NBNameCacheNode **ret = NULL;
77 if (cache && cache->head)
79 NBNameCacheNode **ptr;
81 ptr = &cache->head;
82 while (ptr && *ptr && (*ptr)->entry)
84 if (!memcmp((*ptr)->entry->name, name, NCBNAMSZ - 1))
85 ret = ptr;
86 else
88 if (GetTickCount() > (*ptr)->expireTime)
89 NBNameCacheUnlinkNode(cache, ptr);
91 if (*ptr)
92 ptr = &(*ptr)->next;
95 return ret;
98 struct NBNameCache *NBNameCacheCreate(HANDLE heap, DWORD entryExpireTimeMS)
100 struct NBNameCache *cache;
103 if (!heap)
104 heap = GetProcessHeap();
105 cache = HeapAlloc(heap, 0, sizeof(struct NBNameCache));
106 if (cache)
108 cache->heap = heap;
109 InitializeCriticalSection(&cache->cs);
110 cache->entryExpireTimeMS = entryExpireTimeMS;
111 cache->head = NULL;
113 return cache;
116 BOOL NBNameCacheAddEntry(struct NBNameCache *cache, NBNameCacheEntry *entry)
118 BOOL ret;
120 if (cache && entry)
122 NBNameCacheNode **node;
124 EnterCriticalSection(&cache->cs);
125 node = NBNameCacheWalk(cache, (char*)entry->name);
126 if (node)
128 (*node)->expireTime = GetTickCount() +
129 cache->entryExpireTimeMS;
130 HeapFree(cache->heap, 0, (*node)->entry);
131 (*node)->entry = entry;
132 ret = TRUE;
134 else
136 NBNameCacheNode *newNode = HeapAlloc(cache->heap, 0, sizeof(NBNameCacheNode));
137 if (newNode)
139 newNode->expireTime = GetTickCount() +
140 cache->entryExpireTimeMS;
141 newNode->entry = entry;
142 newNode->next = cache->head;
143 cache->head = newNode;
144 ret = TRUE;
146 else
147 ret = FALSE;
149 LeaveCriticalSection(&cache->cs);
151 else
152 ret = FALSE;
153 return ret;
156 const NBNameCacheEntry *NBNameCacheFindEntry(struct NBNameCache *cache,
157 const UCHAR name[NCBNAMSZ])
159 const NBNameCacheEntry *ret;
160 UCHAR printName[NCBNAMSZ];
162 memcpy(printName, name, NCBNAMSZ - 1);
163 printName[NCBNAMSZ - 1] = '\0';
164 if (cache)
166 NBNameCacheNode **node;
168 EnterCriticalSection(&cache->cs);
169 node = NBNameCacheWalk(cache, (char*)name);
170 if (node)
171 ret = (*node)->entry;
172 else
173 ret = NULL;
174 LeaveCriticalSection(&cache->cs);
176 else
177 ret = NULL;
178 return ret;
181 BOOL NBNameCacheUpdateNBName(struct NBNameCache *cache,
182 const UCHAR name[NCBNAMSZ], const UCHAR nbname[NCBNAMSZ])
184 BOOL ret;
186 if (cache)
188 NBNameCacheNode **node;
190 EnterCriticalSection(&cache->cs);
191 node = NBNameCacheWalk(cache, (char*)name);
192 if (node && *node && (*node)->entry)
194 memcpy((*node)->entry->nbname, nbname, NCBNAMSZ);
195 ret = TRUE;
197 else
198 ret = FALSE;
199 LeaveCriticalSection(&cache->cs);
201 else
202 ret = FALSE;
203 return ret;
206 void NBNameCacheDestroy(struct NBNameCache *cache)
208 if (cache)
210 DeleteCriticalSection(&cache->cs);
211 while (cache->head)
212 NBNameCacheUnlinkNode(cache, &cache->head);
213 HeapFree(cache->heap, 0, cache);