2 * Copyright 2001-2010, Haiku Inc. All rights reserved.
3 * This file may be used under the terms of the MIT License.
6 * Janito V. Ferreira Filho
10 #include "HashRevokeManager.h"
17 # define TRACE(x...) dprintf("\33[34mext2:\33[0m " x)
19 # define TRACE(x...) ;
23 HashRevokeManager::HashRevokeManager()
27 // TODO: Benchmark and find an optimal value
32 HashRevokeManager::~HashRevokeManager()
35 if (fRevokeCount
!= 0) {
36 RevokeElement
*element
= fHash
->Clear(true);
38 while (element
!= NULL
) {
39 RevokeElement
* next
= element
->next
;
51 HashRevokeManager::Init()
53 fHash
= new(std::nothrow
) RevokeTable();
55 if (fHash
== NULL
|| fHash
->Init(kInitialHashSize
) != B_OK
)
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
);
79 // We already have a newer version of the block
83 return _ForceInsert(block
, commitID
);
88 HashRevokeManager::Remove(uint32 block
)
90 RevokeElement
* element
= fHash
->Lookup(block
);
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()
104 HashRevokeManager::Lookup(uint32 block
, uint32 commitID
)
106 RevokeElement
* element
= fHash
->Lookup(block
);
111 return element
->commitID
>= commitID
;
116 HashRevokeManager::Compare(void* _revoked
, const void *_block
)
118 RevokeElement
* revoked
= (RevokeElement
*)_revoked
;
119 uint32 block
= *(uint32
*)_block
;
121 if (revoked
->block
== block
)
124 return (revoked
->block
> block
) ? 1 : -1;
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
;
136 return revoked
->block
% range
;
138 uint32 block
= *(uint32
*)_block
;
139 return block
% range
;
144 HashRevokeManager::_ForceInsert(uint32 block
, uint32 commitID
)
146 RevokeElement
* element
= new(std::nothrow
) RevokeElement
;
151 element
->block
= block
;
152 element
->commitID
= commitID
;
154 status_t retValue
= fHash
->Insert(element
);
156 if (retValue
== B_OK
) {
158 TRACE("HashRevokeManager::_ForceInsert(): revoke count: %" B_PRIu32