Browse Source

Everywhere: Remove string.h include from AK/Traits.h and resolve fallout

A lot of places were relying on AK/Traits.h to give it strnlen, memcmp,
memcpy and other related declarations.

In the quest to remove inclusion of LibC headers from Kernel files, deal
with all the fallout of this included-everywhere header including less
things.
Andrew Kaster 2 years ago
parent
commit
7ab37ee22c

+ 1 - 0
AK/FuzzyMatch.cpp

@@ -6,6 +6,7 @@
 
 #include <AK/CharacterTypes.h>
 #include <AK/FuzzyMatch.h>
+#include <string.h>
 
 namespace AK {
 

+ 2 - 2
AK/JsonObjectSerializer.h

@@ -84,11 +84,11 @@ public:
         TRY(begin_item(key));
         if constexpr (IsLegacyBuilder<Builder>) {
             TRY(m_builder.try_append('"'));
-            TRY(m_builder.try_append_escaped_for_json({ value, strlen(value) }));
+            TRY(m_builder.try_append_escaped_for_json({ value, __builtin_strlen(value) }));
             TRY(m_builder.try_append('"'));
         } else {
             TRY(m_builder.append('"'));
-            TRY(m_builder.append_escaped_for_json({ value, strlen(value) }));
+            TRY(m_builder.append_escaped_for_json({ value, __builtin_strlen(value) }));
             TRY(m_builder.append('"'));
         }
         return {};

+ 1 - 1
AK/PrintfImplementation.h

@@ -10,10 +10,10 @@
 #include <AK/StdLibExtras.h>
 #include <AK/Types.h>
 #include <stdarg.h>
-#include <wchar.h>
 
 #ifndef KERNEL
 #    include <math.h>
+#    include <wchar.h>
 #endif
 
 #ifdef AK_OS_SERENITY

+ 4 - 1
AK/StringUtils.cpp

@@ -14,9 +14,12 @@
 #include <AK/StringView.h>
 #include <AK/Vector.h>
 
-#ifndef KERNEL
+#ifdef KERNEL
+#    include <Kernel/StdLib.h>
+#else
 #    include <AK/DeprecatedString.h>
 #    include <AK/FloatingPointStringConversions.h>
+#    include <string.h>
 #endif
 
 namespace AK {

+ 5 - 6
AK/Time.h

@@ -11,12 +11,11 @@
 #include <AK/Platform.h>
 #include <AK/Types.h>
 
-// Kernel and Userspace pull in the definitions from different places.
-// Avoid trying to figure out which one.
-struct timeval;
-struct timespec;
-
-#if defined(AK_OS_WINDOWS)
+#if defined(AK_OS_SERENITY) && defined(KERNEL)
+#    include <Kernel/API/POSIX/sys/time.h>
+#    include <Kernel/API/POSIX/time.h>
+#else
+#    include <sys/time.h>
 #    include <time.h>
 #endif
 

+ 0 - 1
AK/Traits.h

@@ -11,7 +11,6 @@
 #include <AK/Forward.h>
 #include <AK/HashFunctions.h>
 #include <AK/StringHash.h>
-#include <string.h>
 
 namespace AK {
 

+ 1 - 0
Kernel/Bus/USB/USBConfiguration.cpp

@@ -9,6 +9,7 @@
 #include <Kernel/Bus/USB/USBConfiguration.h>
 #include <Kernel/Bus/USB/USBInterface.h>
 #include <Kernel/Bus/USB/USBRequest.h>
+#include <Kernel/StdLib.h>
 
 namespace Kernel::USB {
 

+ 1 - 0
Kernel/Bus/USB/USBTransfer.cpp

@@ -6,6 +6,7 @@
 
 #include <Kernel/Bus/USB/USBTransfer.h>
 #include <Kernel/Memory/MemoryManager.h>
+#include <Kernel/StdLib.h>
 
 namespace Kernel::USB {
 

+ 3 - 3
Kernel/FileSystem/Ext2FS/Inode.cpp

@@ -992,11 +992,11 @@ ErrorOr<void> Ext2FSInode::update_timestamps(Optional<Time> atime, Optional<Time
     MutexLocker locker(m_inode_lock);
     if (fs().is_readonly())
         return EROFS;
-    if (atime.value_or({}).to_timespec().tv_sec > INT32_MAX)
+    if (atime.value_or({}).to_timespec().tv_sec > NumericLimits<i32>::max())
         return EINVAL;
-    if (ctime.value_or({}).to_timespec().tv_sec > INT32_MAX)
+    if (ctime.value_or({}).to_timespec().tv_sec > NumericLimits<i32>::max())
         return EINVAL;
-    if (mtime.value_or({}).to_timespec().tv_sec > INT32_MAX)
+    if (mtime.value_or({}).to_timespec().tv_sec > NumericLimits<i32>::max())
         return EINVAL;
     if (atime.has_value())
         m_raw_inode.i_atime = atime.value().to_timespec().tv_sec;

+ 1 - 0
Kernel/KBuffer.h

@@ -18,6 +18,7 @@
 #include <AK/Assertions.h>
 #include <AK/StringView.h>
 #include <Kernel/Memory/MemoryManager.h>
+#include <Kernel/StdLib.h> // For memcpy. FIXME: Make memcpy less expensive to access a declaration of in the Kernel.
 
 namespace Kernel {
 

+ 2 - 0
Kernel/StdLib.h

@@ -51,6 +51,8 @@ void const* memmem(void const* haystack, size_t, void const* needle, size_t);
 [[nodiscard]] inline u16 htons(u16 w) { return (w & 0xff) << 8 | ((w >> 8) & 0xff); }
 }
 
+#define offsetof(type, member) __builtin_offsetof(type, member)
+
 template<typename T>
 [[nodiscard]] inline ErrorOr<void> copy_from_user(T* dest, T const* src)
 {

+ 3 - 1
Userland/Libraries/LibEDID/EDID.cpp

@@ -10,7 +10,9 @@
 #include <AK/Try.h>
 #include <LibEDID/EDID.h>
 
-#ifndef KERNEL
+#ifdef KERNEL
+#    include <Kernel/StdLib.h>
+#else
 #    include <AK/ScopeGuard.h>
 #    include <Kernel/API/Graphics.h>
 #    include <fcntl.h>

+ 6 - 1
Userland/Libraries/LibELF/Image.cpp

@@ -14,7 +14,12 @@
 #include <Kernel/API/serenity_limits.h>
 #include <LibELF/Image.h>
 #include <LibELF/Validation.h>
-#include <limits.h>
+
+#ifdef KERNEL
+#    include <Kernel/StdLib.h>
+#else
+#    include <string.h>
+#endif
 
 namespace ELF {
 

+ 5 - 2
Userland/Libraries/LibELF/Validation.cpp

@@ -10,8 +10,11 @@
 #include <Kernel/API/serenity_limits.h>
 #include <LibC/elf.h>
 #include <LibELF/Validation.h>
-#include <limits.h>
-#include <pthread.h>
+
+#ifndef KERNEL
+#    include <limits.h>
+#    include <pthread.h>
+#endif
 
 namespace ELF {