AK: Move memory stuff (fast memcpy, etc) to a separate header

Move the "fast memcpy" stuff out of StdLibExtras.h and into Memory.h.
This will break a ton of things that were relying on StdLibExtras.h
to include a bunch of other headers. Fix will follow immediately after.

This makes it possible to include StdLibExtras.h from Types.h, which is
the main point of this exercise.
This commit is contained in:
Andreas Kling 2020-03-08 12:34:33 +01:00
parent fa9fba6901
commit 900f51ccd0
Notes: sideshowbarker 2024-07-19 08:50:24 +09:00
11 changed files with 57 additions and 49 deletions

View file

@ -27,6 +27,7 @@
#include <AK/JsonArray.h> #include <AK/JsonArray.h>
#include <AK/JsonObject.h> #include <AK/JsonObject.h>
#include <AK/JsonParser.h> #include <AK/JsonParser.h>
#include <AK/Memory.h>
namespace AK { namespace AK {

38
AK/Memory.h Normal file
View file

@ -0,0 +1,38 @@
#pragma once
#include <AK/Types.h>
#if defined(KERNEL) || defined(BOOTSTRAPPER)
# include <LibBareMetal/StdLib.h>
#else
# include <stdlib.h>
# include <string.h>
#endif
#if defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)
extern "C" void* mmx_memcpy(void* to, const void* from, size_t);
#endif
[[gnu::always_inline]] inline void fast_u32_copy(u32* dest, const u32* src, size_t count)
{
#if defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)
if (count >= 256) {
mmx_memcpy(dest, src, count * sizeof(count));
return;
}
#endif
asm volatile(
"rep movsl\n"
: "=S"(src), "=D"(dest), "=c"(count)
: "S"(src), "D"(dest), "c"(count)
: "memory");
}
[[gnu::always_inline]] inline void fast_u32_fill(u32* dest, u32 value, size_t count)
{
asm volatile(
"rep stosl\n"
: "=D"(dest), "=c"(count)
: "D"(dest), "c"(count), "a"(value)
: "memory");
}

View file

@ -29,6 +29,7 @@
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/Platform.h> #include <AK/Platform.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/Types.h>
namespace AK { namespace AK {
@ -157,7 +158,7 @@ private:
ASSERT(m_has_value); ASSERT(m_has_value);
return *reinterpret_cast<const T*>(&m_storage); return *reinterpret_cast<const T*>(&m_storage);
} }
unsigned char m_storage[sizeof(T)] { 0 }; u8 m_storage[sizeof(T)] { 0 };
bool m_has_value { false }; bool m_has_value { false };
}; };

View file

@ -28,6 +28,7 @@
#include <AK/Assertions.h> #include <AK/Assertions.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/Types.h>
namespace AK { namespace AK {

View file

@ -26,46 +26,9 @@
#pragma once #pragma once
#if defined(KERNEL) || defined(BOOTSTRAPPER)
# include <LibBareMetal/StdLib.h>
#else
# include <stdlib.h>
# include <string.h>
#endif
#define UNUSED_PARAM(x) (void)x #define UNUSED_PARAM(x) (void)x
#include <AK/Types.h> inline constexpr unsigned round_up_to_power_of_two(unsigned value, unsigned power_of_two)
#if defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)
extern "C" void* mmx_memcpy(void* to, const void* from, size_t);
#endif
[[gnu::always_inline]] inline void fast_u32_copy(u32* dest, const u32* src, size_t count)
{
#if defined(__serenity__) && !defined(KERNEL) && !defined(BOOTSTRAPPER)
if (count >= 256) {
mmx_memcpy(dest, src, count * sizeof(count));
return;
}
#endif
asm volatile(
"rep movsl\n"
: "=S"(src), "=D"(dest), "=c"(count)
: "S"(src), "D"(dest), "c"(count)
: "memory");
}
[[gnu::always_inline]] inline void fast_u32_fill(u32* dest, u32 value, size_t count)
{
asm volatile(
"rep stosl\n"
: "=D"(dest), "=c"(count)
: "D"(dest), "c"(count), "a"(value)
: "memory");
}
inline constexpr u32 round_up_to_power_of_two(u32 value, u32 power_of_two)
{ {
return ((value - 1) & ~(power_of_two - 1)) + power_of_two; return ((value - 1) & ~(power_of_two - 1)) + power_of_two;
} }

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/Memory.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>

View file

@ -252,7 +252,7 @@ inline bool StringView::operator==(const String& string) const
return false; return false;
if (m_characters == string.characters()) if (m_characters == string.characters())
return true; return true;
return !memcmp(m_characters, string.characters(), m_length); return !__builtin_memcmp(m_characters, string.characters(), m_length);
} }
template<> template<>

View file

@ -24,10 +24,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <AK/Memory.h>
#include <AK/PrintfImplementation.h> #include <AK/PrintfImplementation.h>
#include <AK/StdLibExtras.h> #include <AK/StdLibExtras.h>
#include <AK/StringBuilder.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringBuilder.h>
namespace AK { namespace AK {

View file

@ -24,10 +24,11 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "StringImpl.h" #include <AK/HashTable.h>
#include "HashTable.h" #include <AK/Memory.h>
#include "StdLibExtras.h" #include <AK/StdLibExtras.h>
#include "kmalloc.h" #include <AK/StringImpl.h>
#include <AK/kmalloc.h>
//#define DEBUG_STRINGIMPL //#define DEBUG_STRINGIMPL

View file

@ -25,6 +25,7 @@
*/ */
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/Memory.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/Vector.h> #include <AK/Vector.h>

View file

@ -47,7 +47,7 @@ public:
} }
[[gnu::always_inline]] inline StringView(const char* cstring) [[gnu::always_inline]] inline StringView(const char* cstring)
: m_characters(cstring) : m_characters(cstring)
, m_length(cstring ? strlen(cstring) : 0) , m_length(cstring ? __builtin_strlen(cstring) : 0)
{ {
} }
@ -105,10 +105,10 @@ public:
return !cstring; return !cstring;
if (!cstring) if (!cstring)
return false; return false;
size_t other_length = strlen(cstring); size_t other_length = __builtin_strlen(cstring);
if (m_length != other_length) if (m_length != other_length)
return false; return false;
return !memcmp(m_characters, cstring, m_length); return !__builtin_memcmp(m_characters, cstring, m_length);
} }
bool operator!=(const char* cstring) const bool operator!=(const char* cstring) const
{ {
@ -125,7 +125,7 @@ public:
return false; return false;
if (length() != other.length()) if (length() != other.length())
return false; return false;
return !memcmp(m_characters, other.m_characters, m_length); return !__builtin_memcmp(m_characters, other.m_characters, m_length);
} }
bool operator!=(const StringView& other) const bool operator!=(const StringView& other) const