remove string_view STL dependency (#703)

* remove string_view STL dependency

* fix

* inline string literal handling directly

* simplify dispatch
This commit is contained in:
Schrodinger ZHU Yifan
2024-12-15 02:46:49 -05:00
committed by GitHub
parent 1250ee002f
commit cbc3018e1a
4 changed files with 39 additions and 8 deletions

View File

@@ -71,6 +71,7 @@ namespace snmalloc
*/ */
template<typename, typename = void> template<typename, typename = void>
constexpr bool is_type_complete_v = false; constexpr bool is_type_complete_v = false;
template<typename T> template<typename T>
constexpr bool is_type_complete_v<T, std::void_t<decltype(sizeof(T))>> = true; constexpr bool is_type_complete_v<T, std::void_t<decltype(sizeof(T))>> = true;

View File

@@ -4,7 +4,7 @@
#include <array> #include <array>
#include <atomic> #include <atomic>
#include <string_view> #include <cstddef>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
@@ -192,11 +192,12 @@ namespace snmalloc
/** /**
* Append a string to the buffer. * Append a string to the buffer.
*/ */
void append(std::string_view sv) template<size_t N>
void append(const char (&s)[N])
{ {
for (auto c : sv) for (size_t i = 0; i < N - 1; i++)
{ {
append_char(c); append_char(s[i]);
} }
} }
@@ -224,15 +225,45 @@ namespace snmalloc
} }
#endif #endif
/**
* Append a nullptr
*/
void append(std::nullptr_t)
{
append("(nullptr)");
}
/** /**
* Append a raw pointer to the buffer as a hex string. * Append a raw pointer to the buffer as a hex string.
*/ */
void append(void* ptr) void append(void* ptr)
{ {
if (ptr == nullptr)
{
append(nullptr);
return;
}
append(static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(ptr))); append(static_cast<unsigned long long>(reinterpret_cast<uintptr_t>(ptr)));
// TODO: CHERI bits. // TODO: CHERI bits.
} }
/**
* Append a literal pointer.
*/
void append(const char* ptr)
{
if (ptr == nullptr)
{
append(nullptr);
return;
}
while (char data = *ptr++)
{
append_char(data);
}
}
/** /**
* Append a signed integer to the buffer, as a decimal string. * Append a signed integer to the buffer, as a decimal string.
*/ */

View File

@@ -159,9 +159,7 @@ namespace snmalloc
* The following are supported as arguments: * The following are supported as arguments:
* *
* - Characters (`char`), printed verbatim. * - Characters (`char`), printed verbatim.
* - Strings (anything convertible to `std::string_view`), typically string * - Strings Literals (`const char*` or `const char[]`), printed verbatim.
* literals because nothing on this path should be performing heap
* allocations. Printed verbatim.
* - Raw pointers (void*), printed as hex strings. * - Raw pointers (void*), printed as hex strings.
* - Integers (convertible to `size_t`), printed as hex strings. * - Integers (convertible to `size_t`), printed as hex strings.
* *

View File

@@ -232,7 +232,8 @@ int main(int argc, char** argv)
1234567}; 1234567};
if ( if (
strcmp( strcmp(
"testing pointer 0x42 size_t 0x2a message, hello world, null is 0x0, " "testing pointer 0x42 size_t 0x2a message, hello world, null is "
"(nullptr), "
"-123456 is -123456, 1234567 is 1234567", "-123456 is -123456, 1234567 is 1234567",
b.get_message()) != 0) b.get_message()) != 0)
{ {