1 /* Copyright 1998,2001-2003,2007-2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
17 #include "sysincludes.h"
24 #define BITS_PER_INT (sizeof(unsigned int) * 8)
27 static __inline__ unsigned int rol(unsigned int arg, int shift)
29 arg &= 0xffffffff; /* for 64 bit machines */
30 return (arg << shift) | (arg >> (32 - shift));
34 static int calcHash(wchar_t *name)
44 hash = rol(hash,5); /* a shift of 5 makes sure we spread quickly
45 * over the whole width, moreover, 5 is
46 * prime with 32, which makes sure that
47 * successive letters cannot cover each
50 hash ^= (c * (c+2)) ^ (i * (i+2));
54 hash = hash * (hash + 2);
55 /* the following two xors make sure all info is spread evenly over all
56 * bytes. Important if we only keep the low order bits later on */
57 hash ^= (hash & 0xfff) << 12;
58 hash ^= (hash & 0xff000) << 24;
62 static int addBit(unsigned int *bitmap, int hash, int checkOnly)
66 bit = 1 << (hash % BITS_PER_INT);
67 entry = (hash / BITS_PER_INT) % DC_BITMAP_SIZE;
70 return bitmap[entry] & bit;
77 static int _addHash(dirCache_t *cache, unsigned int hash, int checkOnly)
80 addBit(cache->bm0, hash, checkOnly) &&
81 addBit(cache->bm1, rol(hash,12), checkOnly) &&
82 addBit(cache->bm2, rol(hash,24), checkOnly);
86 static void addNameToHash(dirCache_t *cache, wchar_t *name)
88 _addHash(cache, calcHash(name), 0);
91 static void hashDce(dirCache_t *cache, dirCacheEntry_t *dce)
93 if(dce->beginSlot != cache->nrHashed)
95 cache->nrHashed = dce->endSlot;
97 addNameToHash(cache, dce->longName);
98 addNameToHash(cache, dce->shortName);
101 int isHashed(dirCache_t *cache, wchar_t *name)
105 ret = _addHash(cache, calcHash(name), 1);
109 int growDirCache(dirCache_t *cache, int slot)
112 fprintf(stderr, "Bad slot %d\n", slot);
116 if( cache->nr_entries <= slot) {
119 cache->entries = realloc(cache->entries,
121 sizeof(dirCacheEntry_t *));
124 for(i= cache->nr_entries; i < (slot+1) * 2; i++) {
125 cache->entries[i] = 0;
127 cache->nr_entries = (slot+1) * 2;
132 dirCache_t *allocDirCache(Stream_t *Stream, int slot)
137 fprintf(stderr, "Bad slot %d\n", slot);
141 dcp = getDirCacheP(Stream);
143 *dcp = New(dirCache_t);
146 (*dcp)->entries = NewArray((slot+1)*2+5, dirCacheEntry_t *);
147 if(!(*dcp)->entries) {
151 (*dcp)->nr_entries = (slot+1) * 2;
152 memset( (*dcp)->bm0, 0, DC_BITMAP_SIZE);
153 memset( (*dcp)->bm1, 0, DC_BITMAP_SIZE);
154 memset( (*dcp)->bm2, 0, DC_BITMAP_SIZE);
155 (*dcp)->nrHashed = 0;
157 if(growDirCache(*dcp, slot) < 0)
162 static void freeDirCacheRange(dirCache_t *cache,
163 unsigned int beginSlot,
164 unsigned int endSlot)
166 dirCacheEntry_t *entry;
167 unsigned int clearBegin;
168 unsigned int clearEnd;
171 if(endSlot < beginSlot) {
172 fprintf(stderr, "Bad slots %d %d in free range\n",
177 while(beginSlot < endSlot) {
178 entry = cache->entries[beginSlot];
184 clearEnd = entry->endSlot;
185 if(clearEnd > endSlot)
187 clearBegin = beginSlot;
189 for(i = clearBegin; i <clearEnd; i++)
190 cache->entries[i] = 0;
192 if(entry->endSlot == endSlot)
193 entry->endSlot = beginSlot;
194 else if(entry->beginSlot == beginSlot)
195 entry->beginSlot = endSlot;
198 "Internal error, non contiguous de-allocation\n");
199 fprintf(stderr, "%d %d\n", beginSlot, endSlot);
200 fprintf(stderr, "%d %d\n", entry->beginSlot,
205 if(entry->beginSlot == entry->endSlot) {
207 free(entry->longName);
209 free(entry->shortName);
213 beginSlot = clearEnd;
217 static dirCacheEntry_t *allocDirCacheEntry(dirCache_t *cache, int beginSlot,
219 dirCacheEntryType_t type)
221 dirCacheEntry_t *entry;
224 if(growDirCache(cache, endSlot) < 0)
227 entry = New(dirCacheEntry_t);
232 entry->shortName = 0;
233 entry->beginSlot = beginSlot;
234 entry->endSlot = endSlot;
236 freeDirCacheRange(cache, beginSlot, endSlot);
237 for(i=beginSlot; i<endSlot; i++) {
238 cache->entries[i] = entry;
243 dirCacheEntry_t *addUsedEntry(dirCache_t *cache, int beginSlot, int endSlot,
244 wchar_t *longName, wchar_t *shortName,
245 struct directory *dir)
247 dirCacheEntry_t *entry;
249 if(endSlot < beginSlot) {
251 "Bad slots %d %d in add used entry\n",
257 entry = allocDirCacheEntry(cache, beginSlot, endSlot, DCET_USED);
261 entry->beginSlot = beginSlot;
262 entry->endSlot = endSlot;
264 entry->longName = wcsdup(longName);
265 entry->shortName = wcsdup(shortName);
267 hashDce(cache, entry);
271 static void mergeFreeSlots(dirCache_t *cache, int slot)
273 dirCacheEntry_t *previous, *next;
278 previous = cache->entries[slot-1];
279 next = cache->entries[slot];
280 if(next && next->type == DCET_FREE &&
281 previous && previous->type == DCET_FREE) {
282 for(i=next->beginSlot; i < next->endSlot; i++)
283 cache->entries[i] = previous;
284 previous->endSlot = next->endSlot;
289 dirCacheEntry_t *addFreeEntry(dirCache_t *cache,
290 unsigned int beginSlot,
291 unsigned int endSlot)
293 dirCacheEntry_t *entry;
295 if(beginSlot < cache->nrHashed)
296 cache->nrHashed = beginSlot;
298 if(endSlot < beginSlot) {
299 fprintf(stderr, "Bad slots %d %d in add free entry\n",
304 if(endSlot == beginSlot)
306 entry = allocDirCacheEntry(cache, beginSlot, endSlot, DCET_FREE);
307 mergeFreeSlots(cache, beginSlot);
308 mergeFreeSlots(cache, endSlot);
309 return cache->entries[beginSlot];
313 dirCacheEntry_t *addEndEntry(dirCache_t *cache, int pos)
315 return allocDirCacheEntry(cache, pos, pos+1, DCET_END);
318 dirCacheEntry_t *lookupInDircache(dirCache_t *cache, int pos)
320 if(growDirCache(cache, pos+1) < 0)
322 return cache->entries[pos];
325 void freeDirCache(Stream_t *Stream)
327 dirCache_t *cache, **dcp;
329 dcp = getDirCacheP(Stream);
332 freeDirCacheRange(cache, 0, cache->nr_entries);