btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / system / kernel / fs / EntryCache.h
blob4cbf8659156452d6ad05c902748fc36183fd2444
1 /*
2 * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef ENTRY_CACHE_H
6 #define ENTRY_CACHE_H
9 #include <stdlib.h>
11 #include <util/AutoLock.h>
12 #include <util/OpenHashTable.h>
13 #include <util/StringHash.h>
16 struct EntryCacheKey {
17 EntryCacheKey(ino_t dirID, const char* name)
19 dir_id(dirID),
20 name(name)
22 hash = (uint32)dir_id ^ (uint32)(dir_id >> 32) ^ hash_hash_string(name);
23 // We cache the hash value, so we can easily compute it before
24 // holding any locks.
27 ino_t dir_id;
28 const char* name;
29 size_t hash;
33 struct EntryCacheEntry {
34 EntryCacheEntry* hash_link;
35 ino_t node_id;
36 ino_t dir_id;
37 int32 generation;
38 int32 index;
39 bool missing;
40 char name[1];
44 struct EntryCacheGeneration {
45 int32 next_index;
46 EntryCacheEntry** entries;
48 EntryCacheGeneration();
49 ~EntryCacheGeneration();
51 status_t Init();
55 struct EntryCacheHashDefinition {
56 typedef EntryCacheKey KeyType;
57 typedef EntryCacheEntry ValueType;
59 uint32 HashKey(const EntryCacheKey& key) const
61 return key.hash;
64 size_t Hash(const EntryCacheEntry* value) const
66 return (uint32)value->dir_id ^ (uint32)(value->dir_id >> 32)
67 ^ hash_hash_string(value->name);
70 bool Compare(const EntryCacheKey& key, const EntryCacheEntry* value) const
72 return value->dir_id == key.dir_id
73 && strcmp(value->name, key.name) == 0;
76 EntryCacheEntry*& GetLink(EntryCacheEntry* value) const
78 return value->hash_link;
83 class EntryCache {
84 public:
85 EntryCache();
86 ~EntryCache();
88 status_t Init();
90 status_t Add(ino_t dirID, const char* name,
91 ino_t nodeID, bool missing);
93 status_t Remove(ino_t dirID, const char* name);
95 bool Lookup(ino_t dirID, const char* name,
96 ino_t& nodeID, bool& missing);
98 const char* DebugReverseLookup(ino_t nodeID, ino_t& _dirID);
100 private:
101 static const int32 kGenerationCount = 8;
103 typedef BOpenHashTable<EntryCacheHashDefinition> EntryTable;
104 typedef DoublyLinkedList<EntryCacheEntry> EntryList;
106 private:
107 void _AddEntryToCurrentGeneration(
108 EntryCacheEntry* entry);
110 private:
111 rw_lock fLock;
112 EntryTable fEntries;
113 EntryCacheGeneration fGenerations[kGenerationCount];
114 int32 fCurrentGeneration;
118 #endif // ENTRY_CACHE_H