2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
5 #ifndef UNIX_ADDRESS_MANAGER_H
6 #define UNIX_ADDRESS_MANAGER_H
9 #include <util/OpenHashTable.h>
11 #include "UnixAddress.h"
12 #include "UnixEndpoint.h"
15 struct UnixAddressHashDefinition
{
16 typedef UnixAddress KeyType
;
17 typedef UnixEndpoint ValueType
;
19 size_t HashKey(const KeyType
& key
) const
21 return key
.HashCode();
24 size_t Hash(UnixEndpoint
* endpoint
) const
26 return HashKey(endpoint
->Address());
29 bool Compare(const KeyType
& key
, UnixEndpoint
* endpoint
) const
31 return key
== endpoint
->Address();
34 UnixEndpoint
*& GetLink(UnixEndpoint
* endpoint
) const
36 return endpoint
->HashTableLink();
41 class UnixAddressManager
{
45 mutex_init(&fLock
, "unix address manager");
50 mutex_destroy(&fLock
);
55 return fBoundEndpoints
.Init();
60 return mutex_lock(&fLock
) == B_OK
;
68 UnixEndpoint
* Lookup(const UnixAddress
& address
) const
70 return fBoundEndpoints
.Lookup(address
);
73 void Add(UnixEndpoint
* endpoint
)
75 fBoundEndpoints
.Insert(endpoint
);
78 void Remove(UnixEndpoint
* endpoint
)
80 fBoundEndpoints
.Remove(endpoint
);
83 int32
NextInternalID()
85 int32 id
= fNextInternalID
;
86 fNextInternalID
= (id
+ 1) & 0xfffff;
90 int32
NextUnusedInternalID()
92 for (int32 i
= 0xfffff; i
>= 0; i
--) {
93 int32 id
= NextInternalID();
94 UnixAddress
address(id
);
95 if (Lookup(address
) == NULL
)
103 typedef BOpenHashTable
<UnixAddressHashDefinition
, false> EndpointTable
;
106 EndpointTable fBoundEndpoints
;
107 int32 fNextInternalID
;
111 typedef AutoLocker
<UnixAddressManager
> UnixAddressManagerLocker
;
114 #endif // UNIX_ADDRESS_MANAGER_H