btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / add-ons / kernel / network / protocols / unix / UnixAddressManager.h
blob1359b8a3d20cdfb7c5a4056f3903288933daf682
1 /*
2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef UNIX_ADDRESS_MANAGER_H
6 #define UNIX_ADDRESS_MANAGER_H
8 #include <lock.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 {
42 public:
43 UnixAddressManager()
45 mutex_init(&fLock, "unix address manager");
48 ~UnixAddressManager()
50 mutex_destroy(&fLock);
53 status_t Init()
55 return fBoundEndpoints.Init();
58 bool Lock()
60 return mutex_lock(&fLock) == B_OK;
63 void Unlock()
65 mutex_unlock(&fLock);
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;
87 return id;
90 int32 NextUnusedInternalID()
92 for (int32 i = 0xfffff; i >= 0; i--) {
93 int32 id = NextInternalID();
94 UnixAddress address(id);
95 if (Lookup(address) == NULL)
96 return id;
99 return ENOBUFS;
102 private:
103 typedef BOpenHashTable<UnixAddressHashDefinition, false> EndpointTable;
105 mutex fLock;
106 EndpointTable fBoundEndpoints;
107 int32 fNextInternalID;
111 typedef AutoLocker<UnixAddressManager> UnixAddressManagerLocker;
114 #endif // UNIX_ADDRESS_MANAGER_H