ReactOS 0.4.15-dev-7788-g1ad9096
nbnamecache.c
Go to the documentation of this file.
1/* Copyright (c) 2003 Juan Lang
2 *
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.
7 *
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.
12 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 *
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.
20 */
21
22#include "netapi32.h"
23
24typedef struct _NBNameCacheNode
25{
30
32{
37};
38
39/* Unlinks the node pointed to by *prev, and frees any associated memory.
40 * If that node's next pointed to another node, *prev now points to it.
41 * Assumes the caller owns cache's lock.
42 */
44 NBNameCacheNode **prev)
45{
46 if (cache && prev && *prev)
47 {
48 NBNameCacheNode *next = (*prev)->next;
49
50 HeapFree(cache->heap, 0, (*prev)->entry);
51 HeapFree(cache->heap, 0, *prev);
52 *prev = next;
53 }
54}
55
56/* Walks the list beginning with cache->head looking for the node with name
57 * name. If the node is found, returns a pointer to the next pointer of the
58 * node _prior_ to the found node (or head if head points to it). Thus, if the
59 * node's all you want, dereference the return value twice. If you want to
60 * modify the list, modify the referent of the return value.
61 * While it's at it, deletes nodes whose time has expired (except the node
62 * you're looking for, of course).
63 * Returns NULL if the node isn't found.
64 * Assumes the caller owns cache's lock.
65 */
67 const char name[NCBNAMSZ])
68{
70
71 if (cache && cache->head)
72 {
74
75 ptr = &cache->head;
76 while (ptr && *ptr && (*ptr)->entry)
77 {
78 if (!memcmp((*ptr)->entry->name, name, NCBNAMSZ - 1))
79 ret = ptr;
80 else
81 {
82 if (GetTickCount() > (*ptr)->expireTime)
84 }
85 if (*ptr)
86 ptr = &(*ptr)->next;
87 }
88 }
89 return ret;
90}
91
93{
94 struct NBNameCache *cache;
95
96
97 if (!heap)
99 cache = HeapAlloc(heap, 0, sizeof(struct NBNameCache));
100 if (cache)
101 {
102 cache->heap = heap;
104 cache->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": NBNameCache.cs");
105 cache->entryExpireTimeMS = entryExpireTimeMS;
106 cache->head = NULL;
107 }
108 return cache;
109}
110
112{
113 BOOL ret;
114
115 if (cache && entry)
116 {
118
120 node = NBNameCacheWalk(cache, (char*)entry->name);
121 if (node)
122 {
123 (*node)->expireTime = GetTickCount() +
124 cache->entryExpireTimeMS;
125 HeapFree(cache->heap, 0, (*node)->entry);
126 (*node)->entry = entry;
127 ret = TRUE;
128 }
129 else
130 {
131 NBNameCacheNode *newNode = HeapAlloc(cache->heap, 0, sizeof(NBNameCacheNode));
132 if (newNode)
133 {
134 newNode->expireTime = GetTickCount() +
135 cache->entryExpireTimeMS;
136 newNode->entry = entry;
137 newNode->next = cache->head;
138 cache->head = newNode;
139 ret = TRUE;
140 }
141 else
142 ret = FALSE;
143 }
145 }
146 else
147 ret = FALSE;
148 return ret;
149}
150
152 const UCHAR name[NCBNAMSZ])
153{
154 const NBNameCacheEntry *ret;
155 UCHAR printName[NCBNAMSZ];
156
157 memcpy(printName, name, NCBNAMSZ - 1);
158 printName[NCBNAMSZ - 1] = '\0';
159 if (cache)
160 {
162
164 node = NBNameCacheWalk(cache, (const char *)name);
165 if (node)
166 ret = (*node)->entry;
167 else
168 ret = NULL;
170 }
171 else
172 ret = NULL;
173 return ret;
174}
175
177{
178 if (cache)
179 {
180 cache->cs.DebugInfo->Spare[0] = 0;
182 while (cache->head)
184 HeapFree(cache->heap, 0, cache);
185 }
186}
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:455
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
uint32_t entry
Definition: isohybrid.c:63
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static PVOID ptr
Definition: dispmode.c:27
#define NCBNAMSZ
Definition: nb30.h:7
struct _NBNameCacheNode NBNameCacheNode
BOOL NBNameCacheAddEntry(struct NBNameCache *cache, NBNameCacheEntry *entry)
Definition: nbnamecache.c:111
void NBNameCacheDestroy(struct NBNameCache *cache)
Definition: nbnamecache.c:176
static void NBNameCacheUnlinkNode(struct NBNameCache *cache, NBNameCacheNode **prev)
Definition: nbnamecache.c:43
static NBNameCacheNode ** NBNameCacheWalk(struct NBNameCache *cache, const char name[NCBNAMSZ])
Definition: nbnamecache.c:66
struct NBNameCache * NBNameCacheCreate(HANDLE heap, DWORD entryExpireTimeMS)
Definition: nbnamecache.c:92
const NBNameCacheEntry * NBNameCacheFindEntry(struct NBNameCache *cache, const UCHAR name[NCBNAMSZ])
Definition: nbnamecache.c:151
static unsigned __int64 next
Definition: rand_nt.c:6
static HANDLE heap
Definition: heap.c:65
DWORD entryExpireTimeMS
Definition: nbnamecache.c:35
HANDLE heap
Definition: nbnamecache.c:33
CRITICAL_SECTION cs
Definition: nbnamecache.c:34
NBNameCacheNode * head
Definition: nbnamecache.c:36
struct _NBNameCacheNode * next
Definition: nbnamecache.c:28
NBNameCacheEntry * entry
Definition: nbnamecache.c:27
Definition: cache.c:49
Definition: name.c:39
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751
#define DWORD_PTR
Definition: treelist.c:76
Definition: dlist.c:348
int ret
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)
unsigned char UCHAR
Definition: xmlstorage.h:181