From 899e0ef82b5624ace2ccfa5f5a58c8ebee54aaef Mon Sep 17 00:00:00 2001 From: PulkoMandy Date: Sat, 1 Jul 2023 13:26:25 +0200 Subject: [PATCH] userlandfs: use std::bitset for FSCapabilities gcc 13 is confused by the custom bitset implementation, so use the C++ standard one instead. However, this results in including the C++ header in kernel code. This doesn't work for gcc2, because of two problems: - That header includes a declaration of atomic_add that doesn't match the one in SupportsDefs.h (which can in some cases replace the function with a #define for an inline version). Adjust the header to use the be a problem because it creates a risk of circular inclusions. Standard C++ headers shouldn't depend on BeAPI ones. - It also leads to the inclusion of iostream which defined lock and unlock functions in the global namespace. We don't want these, and they are not part of the C++ standard, so just remove them. Ideally we could use std::hash for the GetHashCode implementation, but that doesn't work because it depends on an helper function that's in libstdc++, and we can't link that from kernel add-ons. Change-Id: Iee07280beb4dddf7a9b6160e37f3b816e4de89ae Reviewed-on: https://review.haiku-os.org/c/haiku/+/6663 Reviewed-by: Niels Sascha Reedijk Tested-by: Commit checker robot --- headers/cpp/iostream.h | 5 --- headers/cpp/std/bastring.h | 2 + .../userlandfs/private/FSCapabilities.h | 45 +++++++------------ .../userlandfs/server/beos/Jamfile | 2 +- .../userlandfs/server/fuse/Jamfile | 2 +- .../userlandfs/server/haiku/Jamfile | 2 +- 6 files changed, 21 insertions(+), 37 deletions(-) diff --git a/headers/cpp/iostream.h b/headers/cpp/iostream.h index 22b9d69e25..0a68fe86da 100644 --- a/headers/cpp/iostream.h +++ b/headers/cpp/iostream.h @@ -271,11 +271,6 @@ __asm__ ("__IO_clog") #endif ; -extern istream& lock(istream& ins); -extern istream& unlock(istream& ins); -extern ostream& lock(ostream& outs); -extern ostream& unlock(ostream& outs); - struct Iostream_init { } ; // Compatibility hack for AT&T library. inline ios& dec(ios& i) diff --git a/headers/cpp/std/bastring.h b/headers/cpp/std/bastring.h index 59eceab5a1..46d07cd75b 100644 --- a/headers/cpp/std/bastring.h +++ b/headers/cpp/std/bastring.h @@ -66,8 +66,10 @@ extern void __length_error (const char *); #endif #ifdef __HAIKU__ +#ifndef atomic_add extern "C" __haiku_int32 atomic_add(__haiku_int32* value, __haiku_int32 addvalue); +#endif #endif /* __HAIKU__ */ template , diff --git a/headers/private/userlandfs/private/FSCapabilities.h b/headers/private/userlandfs/private/FSCapabilities.h index 27f6551eea..741c87af0d 100644 --- a/headers/private/userlandfs/private/FSCapabilities.h +++ b/headers/private/userlandfs/private/FSCapabilities.h @@ -8,6 +8,8 @@ #include #include +#include + #include "Debug.h" enum client_fs_type { @@ -148,7 +150,7 @@ namespace UserlandFSUtil { template struct FSCapabilitiesBase { - uint8 capabilities[(CapabilityCount + 7) / 8]; + std::bitset capabilities; inline void ClearAll(); @@ -171,7 +173,7 @@ template inline void FSCapabilitiesBase::ClearAll() { - memset(capabilities, 0, sizeof(capabilities)); + capabilities.reset(); } @@ -183,11 +185,7 @@ FSCapabilitiesBase::Set(uint32 capability, bool set) if (capability >= CapabilityCount) return; - uint8 flag = uint8(1 << (capability % 8)); - if (set) - capabilities[capability / 8] |= flag; - else - capabilities[capability / 8] &= ~flag; + capabilities.set(capability, set); } @@ -196,7 +194,10 @@ template inline void FSCapabilitiesBase::Clear(uint32 capability) { - Set(capability, false); + if (capability >= CapabilityCount) + return; + + capabilities.reset(capability); } @@ -208,8 +209,7 @@ FSCapabilitiesBase::Get(uint32 capability) const if (capability >= CapabilityCount) return false; - uint8 flag = uint8(1 << (capability % 8)); - return (capabilities[capability / 8] & flag); + return capabilities.test(capability); } @@ -218,12 +218,10 @@ template inline uint32 FSCapabilitiesBase::GetHashCode() const { - uint32 hashCode = 0; - int byteCount = sizeof(capabilities); - for (int i = 0; i < byteCount; i++) - hashCode = hashCode * 37 + capabilities[i]; - - return hashCode; + uint32 hash = 0; + for (int i = 0; i < CapabilityCount; i++) + hash ^= capabilities.test(i) << (i % 32); + return hash; } @@ -233,13 +231,7 @@ inline bool FSCapabilitiesBase::operator==( const FSCapabilitiesBase& other) const { - int byteCount = sizeof(capabilities); - for (int i = 0; i < byteCount; i++) { - if (capabilities[i] != other.capabilities[i]) - return false; - } - - return true; + return capabilities == other.capabilities; } @@ -249,12 +241,7 @@ inline void FSCapabilitiesBase::Dump() const { D( - char buffer[128]; - int byteCount = sizeof(capabilities); - for (int i = 0; i < byteCount; i++) - sprintf(buffer + 2 * i, "%02x", (int)capabilities[i]); - - PRINT(("FSCapabilities[%s]\n", buffer)); + PRINT(("FSCapabilities[%s]\n", capabilities.to_string().c_str())); ) } diff --git a/src/add-ons/kernel/file_systems/userlandfs/server/beos/Jamfile b/src/add-ons/kernel/file_systems/userlandfs/server/beos/Jamfile index ce4d64bf13..406ac87ce5 100644 --- a/src/add-ons/kernel/file_systems/userlandfs/server/beos/Jamfile +++ b/src/add-ons/kernel/file_systems/userlandfs/server/beos/Jamfile @@ -33,5 +33,5 @@ SharedLibrary libuserlandfs_beos_kernel.so : userlandfs_server - [ TargetLibsupc++ ] libbsd.so + [ TargetLibsupc++ ] [ TargetLibstdc++ ] libbsd.so ; diff --git a/src/add-ons/kernel/file_systems/userlandfs/server/fuse/Jamfile b/src/add-ons/kernel/file_systems/userlandfs/server/fuse/Jamfile index 1a1b058577..1928d9c3a8 100644 --- a/src/add-ons/kernel/file_systems/userlandfs/server/fuse/Jamfile +++ b/src/add-ons/kernel/file_systems/userlandfs/server/fuse/Jamfile @@ -36,7 +36,7 @@ SharedLibrary libuserlandfs_fuse.so FUSEVolume.cpp mime_ext_table.c : - userlandfs_server shared be [ TargetLibsupc++ ] + userlandfs_server shared be [ TargetLibsupc++ ] [ TargetLibstdc++ ] ; SEARCH on [ FGristFiles mime_ext_table.c ] diff --git a/src/add-ons/kernel/file_systems/userlandfs/server/haiku/Jamfile b/src/add-ons/kernel/file_systems/userlandfs/server/haiku/Jamfile index b59ea6cb24..f886f463c3 100644 --- a/src/add-ons/kernel/file_systems/userlandfs/server/haiku/Jamfile +++ b/src/add-ons/kernel/file_systems/userlandfs/server/haiku/Jamfile @@ -51,7 +51,7 @@ SharedLibrary libuserlandfs_haiku_kernel.so : userlandfs_server be # for BLocker only - [ TargetLibsupc++ ] + [ TargetLibsupc++ ] [ TargetLibstdc++ ] ;