Skip to content

Commit

Permalink
Initial C++ to C conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Jun 17, 2023
1 parent 0bd1d9c commit 5e12894
Show file tree
Hide file tree
Showing 11 changed files with 2,299 additions and 2,958 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ set(SOURCES
loader/module.cpp
locale/unicode.cpp
locale/unicodedata.cpp
${CLR_SRC_NATIVE_DIR}/minipal/utf8.cpp
${CLR_SRC_NATIVE_DIR}/minipal/utf8.c
map/common.cpp
map/map.cpp
map/virtual.cpp
Expand Down
30 changes: 26 additions & 4 deletions src/coreclr/pal/src/locale/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,22 @@ MultiByteToWideChar(
goto EXIT;
}

// Use minipal_utf8_to_utf16_preallocated on all systems, since it replaces
// Use minipal_convert_utf8_to_utf16 on all systems, since it replaces
// invalid characters and Core Foundation doesn't do that.
if (CodePage == CP_UTF8 || CodePage == CP_ACP)
{
retval = minipal_utf8_to_utf16_preallocated(lpMultiByteStr, cbMultiByte, &lpWideCharStr, cchWideChar, dwFlags, /* treatAsLE */ false);
if (cbMultiByte < 0)
cbMultiByte = strlen(lpMultiByteStr) + 1;

if (!lpWideCharStr || cchWideChar == 0)
retval = minipal_get_length_utf8_to_utf16(lpMultiByteStr, cbMultiByte, dwFlags);

if (lpWideCharStr)
{
if (cchWideChar == 0) cchWideChar = retval;
retval = minipal_convert_utf8_to_utf16(lpMultiByteStr, cbMultiByte, (CHAR16_T*)lpWideCharStr, cchWideChar, dwFlags);
}

goto EXIT;
}

Expand Down Expand Up @@ -333,11 +344,22 @@ WideCharToMultiByte(
defaultChar = *lpDefaultChar;
}

// Use minipal_utf16_to_utf8_preallocated on all systems because we use
// Use minipal_convert_utf16_to_utf8 on all systems because we use
// UTF8ToUnicode in MultiByteToWideChar() on all systems.
if (CodePage == CP_UTF8 || CodePage == CP_ACP)
{
retval = minipal_utf16_to_utf8_preallocated(lpWideCharStr, cchWideChar, &lpMultiByteStr, cbMultiByte);
if (cchWideChar < 0)
cchWideChar = PAL_wcslen(lpWideCharStr) + 1;

if (!lpMultiByteStr || cbMultiByte == 0)
retval = minipal_get_length_utf16_to_utf8((CHAR16_T*)lpWideCharStr, cchWideChar, dwFlags);

if (lpMultiByteStr)
{
if (cbMultiByte == 0) cbMultiByte = retval;
retval = minipal_convert_utf16_to_utf8((CHAR16_T*)lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, dwFlags);
}

goto EXIT;
}

Expand Down
8 changes: 0 additions & 8 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,6 @@ if(GCC)
if(ENABLE_WERROR)
append("-Werror" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
endif()

# don't link with C++ runtime lib for targets which don't require it (tvOS and iOS need it for ICU)
if(NOT LLVM_PREFIX AND NOT HOST_TVOS AND NOT HOST_IOS)
append("-nodefaultlibs" CMAKE_CXX_FLAGS)
if(NOT HOST_WASM)
append("-lc" CMAKE_CXX_FLAGS)
endif()
endif()
endif()

######################################
Expand Down
10 changes: 4 additions & 6 deletions src/mono/mono/eglib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ set(eglib_common_sources
gfile.c
gfile-posix.c
gutf8.c
${CLR_SRC_NATIVE_DIR}/minipal/utf8.cpp)
${CLR_SRC_NATIVE_DIR}/minipal/utf8.c)

if(HOST_WIN32)
set_source_files_properties("${CLR_SRC_NATIVE_DIR}/minipal/utf8.cpp" PROPERTIES COMPILE_FLAGS "/wd4100 /wd4267 /wd4458 /wd4310")
else()
set_source_files_properties("${CLR_SRC_NATIVE_DIR}/minipal/utf8.cpp" PROPERTIES COMPILE_FLAGS "-std=c++11 -fno-rtti -fno-exceptions")
if(IS_BIG_ENDIAN)
set_source_files_properties("${CLR_SRC_NATIVE_DIR}/minipal/utf8.c" PROPERTIES COMPILE_FLAGS "-DBIGENDIAN=1")
endif()

set(eglib_headers
Expand All @@ -48,7 +46,7 @@ set(eglib_headers
gmodule.h)

if(HAVE_CLOCK_NANOSLEEP)
list(APPEND eglib_common_sources gclock-nanosleep.c)
list(APPEND eglib_common_sources gclock-nanosleep.c)
endif()

set(eglib_sources "${eglib_platform_sources};${eglib_common_sources}")
Expand Down
98 changes: 75 additions & 23 deletions src/mono/mono/eglib/giconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <errno.h>
#include "../utils/mono-errno.h"

typedef gunichar2 char16_t;
#include <minipal/utf8.h>

#ifdef _MSC_VER
Expand All @@ -44,9 +43,6 @@ static FORCE_INLINE (int) decode_utf8 (char *inbuf, size_t inleft, gunichar *out
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
#define decode_utf16 decode_utf16le
#else
#ifndef BIGENDIAN
#define BIGENDIAN
#endif
#define decode_utf16 decode_utf16be
#endif

Expand Down Expand Up @@ -328,36 +324,60 @@ g_utf8_to_ucs4_fast (const gchar *str, glong len, glong *items_written)
static FORCE_INLINE (void)
map_error(GError **err)
{
if (errno == 0) return;
if (errno == ERROR_INSUFFICIENT_BUFFER) {
if (errno == MINIPAL_ERROR_INSUFFICIENT_BUFFER) {
g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_MEMORY,
"Allocation failed.");
} else if (errno == ERROR_NO_UNICODE_TRANSLATION) {
g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
"Illegal byte sequence encountered in the input.");
} else {
g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
"Partial byte sequence encountered in the input.");
}
}

static gunichar2 *
g_utf8_to_utf16_impl (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err, int dwFlags, bool treatAsLE)
g_utf8_to_utf16_impl (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err, int flags, bool treatAsLE)
{
errno = 0;
gunichar2* lpDestStr = NULL;
int ret = minipal_utf8_to_utf16_allocate (str, len, &lpDestStr, dwFlags, treatAsLE);
#if G_BYTE_ORDER == G_BIG_ENDIAN
if (treatAsLE)
flags |= MINIPAL_TREAT_AS_LITTLE_ENDIAN;
#endif

if (len < 0)
len = (glong)strlen(str) + 1;

glong ret = (glong)minipal_get_length_utf8_to_utf16 (str, len, flags);

map_error(err);

if (items_written)
*items_written = errno == 0 ? ret : 0;

if (ret <= 0)
return NULL;

lpDestStr = malloc((ret + 1) * sizeof(gunichar2));
ret = (glong)minipal_convert_utf8_to_utf16 (str, len, lpDestStr, ret, flags);
lpDestStr[ret] = '\0';

if (items_written)
*items_written = errno == 0 ? ret : 0;

map_error(err);
return lpDestStr;
}

static gunichar2 *
g_utf8_to_utf16le_custom_alloc_impl (const gchar *str, glong len, glong *items_read, glong *items_written, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err, bool treatAsLE)
{
guint flags = 0;
errno = 0;
int ret = minipal_utf8_to_utf16_preallocated (str, len, 0, 0, 0, /* treatAsLE */ treatAsLE);
#if G_BYTE_ORDER == G_BIG_ENDIAN
if (treatAsLE)
flags = MINIPAL_TREAT_AS_LITTLE_ENDIAN;
#endif
if (len < 0)
len = (glong)strlen(str) + 1;

glong ret = (glong)minipal_get_length_utf8_to_utf16 (str, len, flags);

map_error(err);

if (items_written)
Expand All @@ -366,22 +386,24 @@ g_utf8_to_utf16le_custom_alloc_impl (const gchar *str, glong len, glong *items_r
if (ret <= 0)
return NULL;

gunichar2* lpDestStr = custom_alloc_func((ret + 1) * sizeof (gunichar2), custom_alloc_data);
ret = minipal_utf8_to_utf16_preallocated (str, len, &lpDestStr, ret, MB_ERR_INVALID_CHARS, /* treatAsLE */ treatAsLE);
gunichar2 *lpDestStr = custom_alloc_func((ret + 1) * sizeof (gunichar2), custom_alloc_data);
flags |= MINIPAL_MB_NO_REPLACE_INVALID_CHARS;
ret = (glong)minipal_convert_utf8_to_utf16 (str, len, lpDestStr, ret, flags);

map_error(err);
return lpDestStr;
}

gunichar2 *
g_utf8_to_utf16 (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
{
return g_utf8_to_utf16_impl (str, len, items_read, items_written, err, MB_ERR_INVALID_CHARS, false);
return g_utf8_to_utf16_impl (str, len, items_read, items_written, err, MINIPAL_MB_NO_REPLACE_INVALID_CHARS, false);
}

gunichar2 *
g_utf8_to_utf16le (const gchar *str, glong len, glong *items_read, glong *items_written, GError **err)
{
return g_utf8_to_utf16_impl (str, len, items_read, items_written, err, MB_ERR_INVALID_CHARS, true);
return g_utf8_to_utf16_impl (str, len, items_read, items_written, err, MINIPAL_MB_NO_REPLACE_INVALID_CHARS, true);
}

gunichar2 *
Expand Down Expand Up @@ -477,9 +499,31 @@ g_utf8_to_ucs4 (const gchar *str, glong len, glong *items_read, glong *items_wri
static gchar *
g_utf16_to_utf8_impl (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GError **err, bool treatAsLE)
{
guint flags = 0;
errno = 0;
gchar* lpDestStr = NULL;
int ret = minipal_utf16_to_utf8_allocate (str, len, &lpDestStr, treatAsLE);
#if G_BYTE_ORDER == G_BIG_ENDIAN
if (treatAsLE)
flags |= MINIPAL_TREAT_AS_LITTLE_ENDIAN;
#endif
if (len < 0) {
len = 0;
while (str[len])
len++;
}

glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, flags);
map_error(err);

if (items_written)
*items_written = errno == 0 ? ret : 0;

if (ret <= 0)
return NULL;

lpDestStr = (gchar *)malloc((ret + 1) * sizeof(gchar));
ret = (glong)minipal_convert_utf16_to_utf8 (str, len, lpDestStr, ret, flags);
lpDestStr[ret] = '\0';

if (items_written)
*items_written = errno == 0 ? ret : 0;
Expand All @@ -504,7 +548,14 @@ gchar *
g_utf16_to_utf8_custom_alloc (const gunichar2 *str, glong len, glong *items_read, glong *items_written, GCustomAllocator custom_alloc_func, gpointer custom_alloc_data, GError **err)
{
errno = 0;
int ret = minipal_utf16_to_utf8_preallocated (str, len, 0, 0);

if (len < 0) {
len = 0;
while (str[len])
len++;
}

glong ret = (glong)minipal_get_length_utf16_to_utf8 (str, len, 0);
map_error(err);

if (items_written)
Expand All @@ -513,8 +564,9 @@ g_utf16_to_utf8_custom_alloc (const gunichar2 *str, glong len, glong *items_read
if (ret <= 0)
return NULL;

gchar* lpDestStr = custom_alloc_func((ret + 1) * sizeof (gunichar2), custom_alloc_data);
ret = minipal_utf16_to_utf8_preallocated (str, len, &lpDestStr, ret);
gchar *lpDestStr = custom_alloc_func((ret + 1) * sizeof (gunichar2), custom_alloc_data);
ret = (glong)minipal_convert_utf16_to_utf8 (str, len, lpDestStr, ret, 0);

map_error(err);
return lpDestStr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ get_type_init_exception_for_vtable (MonoVTable *vtable)

mono_mem_manager_init_reflection_hashes (mem_manager);

/*
/*
* If the initializing thread was rudely aborted, the exception is not stored
* in the hash.
*/
Expand Down
7 changes: 2 additions & 5 deletions src/mono/mono/mini/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ set(OS_LIBS "-framework CoreFoundation" "-lobjc" "-lc++")
elseif(HOST_ANDROID)
set(OS_LIBS m dl log)
elseif(HOST_LINUX)
set(OS_LIBS pthread m dl gcc_s)
if(NOT CLR_CMAKE_HOST_LINUX_MUSL) # glibc build also requires libc_nonshared.a for atexit(3) usage
set(OS_LIBS ${OS_LIBS} -l:libc_nonshared.a)
endif()
set(OS_LIBS pthread m dl)
elseif(HOST_WIN32)
set(OS_LIBS bcrypt.lib Mswsock.lib ws2_32.lib psapi.lib version.lib advapi32.lib winmm.lib kernel32.lib)
elseif(HOST_SOLARIS)
Expand Down Expand Up @@ -554,7 +551,7 @@ if(NOT DISABLE_EXECUTABLES)
target_link_libraries(mono-sgen PRIVATE monoapi eglib_api monosgen-static)
if(HAVE_ICU_SHIM)
target_link_libraries(mono-sgen PRIVATE icu_shim_objects)
endif()
endif()
target_link_libraries(mono-sgen PRIVATE ${OS_LIBS} ${LLVM_LIBS} ${ICU_LIBS} ${Z_LIBS})
# Alpine Linux implements ucontext in a different library
if(CLR_CMAKE_HOST_ALPINE_LINUX AND TARGET_S390X)
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/profiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if(NOT DISABLE_LIBS)
# Build the logging profiler only for certain platforms
add_library(mono-profiler-log SHARED helper.c log.c log-args.c ${ZLIB_SOURCES})
target_compile_definitions(mono-profiler-log PRIVATE -DMONO_DLL_EXPORT)
target_link_libraries(mono-profiler-log PRIVATE monosgen-shared monoapi eglib_objects ${CMAKE_DL_LIBS})
target_link_libraries(mono-profiler-log PRIVATE monosgen-shared monoapi eglib_objects)
if(HOST_ANDROID)
target_link_libraries(mono-profiler-log PRIVATE log)
endif()
Expand Down
Loading

0 comments on commit 5e12894

Please sign in to comment.