2 * Copyright 2008, Axel Dörfler. All Rights Reserved.
3 * Copyright 2007, Hugo Santos. All Rights Reserved.
5 * Distributed under the terms of the MIT License.
7 #ifndef HASHED_OBJECT_CACHE_H
8 #define HASHED_OBJECT_CACHE_H
11 #include <util/OpenHashTable.h>
13 #include "ObjectCache.h"
14 #include "slab_private.h"
17 struct HashedSlab
: slab
{
18 HashedSlab
* hash_next
;
22 struct HashedObjectCache
: ObjectCache
{
25 static HashedObjectCache
* Create(const char* name
, size_t object_size
,
26 size_t alignment
, size_t maximum
,
27 size_t magazineCapacity
,
28 size_t maxMagazineCount
,
29 uint32 flags
, void* cookie
,
30 object_cache_constructor constructor
,
31 object_cache_destructor destructor
,
32 object_cache_reclaimer reclaimer
);
33 virtual void Delete();
35 virtual slab
* CreateSlab(uint32 flags
);
36 virtual void ReturnSlab(slab
* slab
, uint32 flags
);
37 virtual slab
* ObjectSlab(void* object
) const;
41 typedef HashedObjectCache ParentType
;
42 typedef const void* KeyType
;
43 typedef HashedSlab ValueType
;
45 Definition(HashedObjectCache
* parent
)
51 Definition(const Definition
& definition
)
53 parent(definition
.parent
)
57 size_t HashKey(const void* key
) const
59 return (addr_t
)::lower_boundary(key
, parent
->slab_size
)
60 >> parent
->lower_boundary
;
63 size_t Hash(HashedSlab
* value
) const
65 return HashKey(value
->pages
);
68 bool Compare(const void* key
, HashedSlab
* value
) const
70 return value
->pages
== key
;
73 HashedSlab
*& GetLink(HashedSlab
* value
) const
75 return value
->hash_next
;
78 HashedObjectCache
* parent
;
81 struct InternalAllocator
{
82 void* Allocate(size_t size
) const
84 return slab_internal_alloc(size
, 0);
87 void Free(void* memory
) const
89 slab_internal_free(memory
, 0);
93 typedef BOpenHashTable
<Definition
, false, false,
94 InternalAllocator
> HashTable
;
96 friend struct Definition
;
99 void _ResizeHashTableIfNeeded(uint32 flags
);
102 HashTable hash_table
;
103 size_t lower_boundary
;
108 #endif // HASHED_OBJECT_CACHE_H