HaikuDepot: notify work status from main window
[haiku.git] / src / libs / compat / freebsd_network / unit.c
blob0053875eef7fd1da33b3ecce0f2f2f0413cbb8b1
1 /*
2 * Copyright 2009 Colin Günther, coling@gmx.de
3 * All Rights Reserved. Distributed under the terms of the MIT License.
5 */
8 /*! Implementation of a number allocator.*/
11 #include "unit.h"
13 #include <compat/sys/mutex.h>
15 #include <stdlib.h>
18 extern struct mtx gIdStoreLock;
21 struct unrhdr*
22 new_unrhdr(int low, int high, struct mtx* mutex)
24 struct unrhdr* idStore;
25 uint32 maxIdCount = high - low + 1;
27 KASSERT(low <= high,
28 ("ID-Store: use error: %s(%u, %u)", __func__, low, high));
30 idStore = malloc(sizeof *idStore);
31 if (idStore == NULL)
32 return NULL;
34 if (_new_unrhdr_buffer(idStore, maxIdCount) != B_OK) {
35 free(idStore);
36 return NULL;
39 if (mutex)
40 idStore->storeMutex = mutex;
41 else
42 idStore->storeMutex = &gIdStoreLock;
44 idStore->idBias = low;
46 return idStore;
50 void
51 delete_unrhdr(struct unrhdr* idStore)
53 KASSERT(uh != NULL,
54 ("ID-Store: %s: NULL pointer as argument.", __func__));
56 mtx_lock(idStore->storeMutex);
58 KASSERT(uh->idBuffer->root_size == 0,
59 ("ID-Store: %s: some ids are still in use..", __func__));
61 _delete_unrhdr_buffer_locked(idStore);
62 mtx_unlock(idStore->storeMutex);
64 free(idStore);
65 idStore = NULL;
69 int
70 alloc_unr(struct unrhdr* idStore)
72 int id;
74 KASSERT(uh != NULL,
75 ("ID-Store: %s: NULL pointer as argument.", __func__));
77 mtx_lock(idStore->storeMutex);
78 id = _alloc_unr_locked(idStore);
79 mtx_unlock(idStore->storeMutex);
81 return id;
85 void
86 free_unr(struct unrhdr* idStore, u_int identity)
88 KASSERT(uh != NULL,
89 ("ID-Store: %s: NULL pointer as argument.", __func__));
91 mtx_lock(idStore->storeMutex);
93 KASSERT((int32)item - uh->idBias >= 0, ("ID-Store: %s(%p, %u): second "
94 + "parameter is not in interval.", __func__, uh, item));
96 _free_unr_locked(idStore, identity);
98 mtx_unlock(idStore->storeMutex);