From ac06bf48c48621a3c8f852b6dd957736fb26c841 Mon Sep 17 00:00:00 2001 From: Augustin Cavalier Date: Thu, 5 Jul 2018 22:22:38 -0400 Subject: [PATCH] freebsd11_network: Combine Unit.cpp and unit.c into unit.cpp. There's a number of these "C++ wrappers for C code" wrappers in the freebsd_network compat layer, and there don't seem to be very many good reasons to use them. We can just as well declare these C++ functions with C linkage so they can be used from C code directly. So, time to start cleaning this up. --- src/libs/compat/freebsd11_network/Jamfile | 3 +- src/libs/compat/freebsd11_network/Unit.cpp | 59 ------------------- .../freebsd11_network/{unit.c => unit.cpp} | 30 +++++++--- src/libs/compat/freebsd11_network/unit.h | 7 +-- 4 files changed, 26 insertions(+), 73 deletions(-) delete mode 100644 src/libs/compat/freebsd11_network/Unit.cpp rename src/libs/compat/freebsd11_network/{unit.c => unit.cpp} (69%) diff --git a/src/libs/compat/freebsd11_network/Jamfile b/src/libs/compat/freebsd11_network/Jamfile index d1b5219d8f..cdedcb8023 100644 --- a/src/libs/compat/freebsd11_network/Jamfile +++ b/src/libs/compat/freebsd11_network/Jamfile @@ -45,8 +45,7 @@ KernelStaticLibrary libfreebsd11_network.a : synch.c systm.c taskqueue.c - unit.c - Unit.cpp + unit.cpp ; rule MIIHeaderGen diff --git a/src/libs/compat/freebsd11_network/Unit.cpp b/src/libs/compat/freebsd11_network/Unit.cpp deleted file mode 100644 index 390878ef14..0000000000 --- a/src/libs/compat/freebsd11_network/Unit.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2009 Colin Günther, coling@gmx.de - * All Rights Reserved. Distributed under the terms of the MIT License. - * - */ - - -/*! Wrapper functions for accessing the number buffer.*/ - - -#include "unit.h" - -#include - - -#define ID_STORE_FULL -1 - - -status_t -_new_unrhdr_buffer(struct unrhdr* idStore, uint32 maxIdCount) -{ - status_t status = B_OK; - - idStore->idBuffer = radix_bitmap_create(maxIdCount); - if (idStore->idBuffer == NULL) - status = B_NO_MEMORY; - - return status; -} - - -void -_delete_unrhdr_buffer_locked(struct unrhdr* idStore) -{ - radix_bitmap_destroy(idStore->idBuffer); -} - - -int -_alloc_unr_locked(struct unrhdr* idStore) -{ - radix_slot_t slotIndex; - int id = ID_STORE_FULL; - - slotIndex = radix_bitmap_alloc(idStore->idBuffer, 1); - if (slotIndex != RADIX_SLOT_NONE) - id = slotIndex + idStore->idBias; - - return id; -} - - -void -_free_unr_locked(struct unrhdr* idStore, u_int identity) -{ - uint32 slotIndex = (int32)identity - idStore->idBias; - - radix_bitmap_dealloc(idStore->idBuffer, slotIndex, 1); -} diff --git a/src/libs/compat/freebsd11_network/unit.c b/src/libs/compat/freebsd11_network/unit.cpp similarity index 69% rename from src/libs/compat/freebsd11_network/unit.c rename to src/libs/compat/freebsd11_network/unit.cpp index 1bfe8b90b1..0eacc7254c 100644 --- a/src/libs/compat/freebsd11_network/unit.c +++ b/src/libs/compat/freebsd11_network/unit.cpp @@ -1,14 +1,16 @@ /* * Copyright 2009 Colin Günther, coling@gmx.de - * All Rights Reserved. Distributed under the terms of the MIT License. - * + * Copyright 2018, Haiku, Inc. + * All rights reserved. Distributed under the terms of the MIT license. */ /*! Implementation of a number allocator.*/ +extern "C" { #include "unit.h" +} #include @@ -16,6 +18,9 @@ #include +#define ID_STORE_FULL -1 + + extern struct mtx gIdStoreLock; @@ -28,11 +33,12 @@ new_unrhdr(int low, int high, struct mtx* mutex) KASSERT(low <= high, ("ID-Store: use error: %s(%u, %u)", __func__, low, high)); - idStore = malloc(sizeof *idStore); + idStore = (unrhdr*)malloc(sizeof *idStore); if (idStore == NULL) return NULL; - if (_new_unrhdr_buffer(idStore, maxIdCount) != B_OK) { + idStore->idBuffer = radix_bitmap_create(maxIdCount); + if (idStore->idBuffer == NULL) { free(idStore); return NULL; } @@ -59,7 +65,7 @@ delete_unrhdr(struct unrhdr* idStore) KASSERT(idStore->idBuffer->root_size == 0, ("ID-Store: %s: some ids are still in use..", __func__)); - _delete_unrhdr_buffer_locked(idStore); + radix_bitmap_destroy(idStore->idBuffer); mtx_unlock(idStore->storeMutex); free(idStore); @@ -76,7 +82,14 @@ alloc_unr(struct unrhdr* idStore) ("ID-Store: %s: NULL pointer as argument.", __func__)); mtx_lock(idStore->storeMutex); - id = _alloc_unr_locked(idStore); + + radix_slot_t slotIndex; + id = ID_STORE_FULL; + + slotIndex = radix_bitmap_alloc(idStore->idBuffer, 1); + if (slotIndex != RADIX_SLOT_NONE) + id = slotIndex + idStore->idBias; + mtx_unlock(idStore->storeMutex); return id; @@ -91,10 +104,11 @@ free_unr(struct unrhdr* idStore, u_int identity) mtx_lock(idStore->storeMutex); - KASSERT((int32)identity - idStore->idBias >= 0, ("ID-Store: %s(%p, %u): second " + uint32 slotIndex = (int32)identity - idStore->idBias; + KASSERT(slotIndex >= 0, ("ID-Store: %s(%p, %u): second " "parameter is not in interval.", __func__, idStore, identity)); - _free_unr_locked(idStore, identity); + radix_bitmap_dealloc(idStore->idBuffer, slotIndex, 1); mtx_unlock(idStore->storeMutex); } diff --git a/src/libs/compat/freebsd11_network/unit.h b/src/libs/compat/freebsd11_network/unit.h index a975675112..8c5841e9aa 100644 --- a/src/libs/compat/freebsd11_network/unit.h +++ b/src/libs/compat/freebsd11_network/unit.h @@ -5,6 +5,9 @@ #ifndef UNIT_H_ #define UNIT_H_ +#ifdef __cplusplus +extern "C" { +#endif #include @@ -17,10 +20,6 @@ struct unrhdr { }; -#ifdef __cplusplus -extern "C" { -#endif - status_t _new_unrhdr_buffer(struct unrhdr*, uint32); void _delete_unrhdr_buffer_locked(struct unrhdr*); int _alloc_unr_locked(struct unrhdr*);