btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / src / add-ons / kernel / file_systems / ext2 / HashRevokeManager.cpp
blob653d6e09b30c899590b17757fd9da33a5f48f94b
1 /*
2 * Copyright 2001-2010, Haiku Inc. All rights reserved.
3 * This file may be used under the terms of the MIT License.
5 * Authors:
6 * Janito V. Ferreira Filho
7 */
10 #include "HashRevokeManager.h"
12 #include <new>
15 //#define TRACE_EXT2
16 #ifdef TRACE_EXT2
17 # define TRACE(x...) dprintf("\33[34mext2:\33[0m " x)
18 #else
19 # define TRACE(x...) ;
20 #endif
23 HashRevokeManager::HashRevokeManager()
25 fHash(NULL),
26 kInitialHashSize(128)
27 // TODO: Benchmark and find an optimal value
32 HashRevokeManager::~HashRevokeManager()
34 if (fHash != NULL) {
35 if (fRevokeCount != 0) {
36 RevokeElement *element = fHash->Clear(true);
38 while (element != NULL) {
39 RevokeElement* next = element->next;
40 delete element;
41 element = next;
45 delete fHash;
50 status_t
51 HashRevokeManager::Init()
53 fHash = new(std::nothrow) RevokeTable();
55 if (fHash == NULL || fHash->Init(kInitialHashSize) != B_OK)
56 return B_NO_MEMORY;
58 return B_OK;
62 status_t
63 HashRevokeManager::Insert(uint32 block, uint32 commitID)
65 RevokeElement* element = fHash->Lookup(block);
67 if (element != NULL) {
68 TRACE("HashRevokeManager::Insert(): Already has an element\n");
69 if (element->commitID < commitID) {
70 TRACE("HashRevokeManager::Insert(): Deleting previous element\n");
71 bool retValue = fHash->Remove(element);
73 if (!retValue)
74 return B_ERROR;
76 delete element;
77 } else {
78 return B_OK;
79 // We already have a newer version of the block
83 return _ForceInsert(block, commitID);
87 status_t
88 HashRevokeManager::Remove(uint32 block)
90 RevokeElement* element = fHash->Lookup(block);
92 if (element == NULL)
93 return B_ERROR; // TODO: Perhaps we should just ignore?
95 fHash->Remove(element);
96 // Can't fail as we just did a sucessful Lookup()
98 delete element;
99 return B_OK;
103 bool
104 HashRevokeManager::Lookup(uint32 block, uint32 commitID)
106 RevokeElement* element = fHash->Lookup(block);
108 if (element == NULL)
109 return false;
111 return element->commitID >= commitID;
115 /*static*/ int
116 HashRevokeManager::Compare(void* _revoked, const void *_block)
118 RevokeElement* revoked = (RevokeElement*)_revoked;
119 uint32 block = *(uint32*)_block;
121 if (revoked->block == block)
122 return 0;
124 return (revoked->block > block) ? 1 : -1;
128 /*static*/ uint32
129 HashRevokeManager::Hash(void* _revoked, const void* _block, uint32 range)
131 TRACE("HashRevokeManager::Hash(): revoked: %p, block: %p, range: %"
132 B_PRIu32 "\n", _revoked, _block, range);
133 RevokeElement* revoked = (RevokeElement*)_revoked;
135 if (revoked != NULL)
136 return revoked->block % range;
138 uint32 block = *(uint32*)_block;
139 return block % range;
143 status_t
144 HashRevokeManager::_ForceInsert(uint32 block, uint32 commitID)
146 RevokeElement* element = new(std::nothrow) RevokeElement;
148 if (element == NULL)
149 return B_NO_MEMORY;
151 element->block = block;
152 element->commitID = commitID;
154 status_t retValue = fHash->Insert(element);
156 if (retValue == B_OK) {
157 fRevokeCount++;
158 TRACE("HashRevokeManager::_ForceInsert(): revoke count: %" B_PRIu32
159 "\n", fRevokeCount);
162 return retValue;