Skip to content

Commit

Permalink
loader: Provide memset, and use hidden visibility instead of symbol r…
Browse files Browse the repository at this point in the history
…enaming. (JuliaLang#51682)

This ensures that the compiler may generate calls to these functions.
  • Loading branch information
maleadt committed Oct 13, 2023
1 parent 3df63a1 commit 42fb22e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
18 changes: 0 additions & 18 deletions cli/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,6 @@
#include "../src/support/dirpath.h"
#include "../src/julia_fasttls.h"

#ifdef _OS_WINDOWS_
/* We need to reimplement a bunch of standard library stuff on windows,
* but we want to make sure that it doesn't conflict with the actual implementations
* once those get linked into this process. */
#define fwrite loader_fwrite
#define fputs loader_fputs
#define exit loader_exit
#define strlen loader_strlen
#define wcslen loader_wcslen
#define strncat loader_strncat
#define memcpy loader_memcpy
#define dirname loader_dirname
#define strchr loader_strchr
#define malloc loader_malloc
#define realloc loader_realloc
#define free loader_free
#endif

#ifdef _OS_WINDOWS_

#define WIN32_LEAN_AND_MEAN
Expand Down
37 changes: 22 additions & 15 deletions cli/loader_win_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ static FILE _stderr = { INVALID_HANDLE_VALUE };
FILE *stdout = &_stdout;
FILE *stderr = &_stderr;

int loader_fwrite(const char *str, size_t nchars, FILE *out) {
int JL_HIDDEN fwrite(const char *str, size_t nchars, FILE *out) {
DWORD written;
if (out->isconsole) {
// Windows consoles do not support UTF-8 (for reading input, though new Windows Terminal does for writing), only UTF-16.
wchar_t* wstr = utf8_to_wchar(str);
if (!wstr)
return -1;
if (WriteConsoleW(out->fd, wstr, wcslen(wstr), &written, NULL)) {
loader_free(wstr);
free(wstr);
return written;
}
loader_free(wstr);
free(wstr);
} else {
// However, we want to print UTF-8 if the output is a file.
if (WriteFile(out->fd, str, nchars, &written, NULL))
Expand All @@ -32,19 +32,19 @@ int loader_fwrite(const char *str, size_t nchars, FILE *out) {
return -1;
}

int loader_fputs(const char *str, FILE *out) {
return loader_fwrite(str, loader_strlen(str), out);
int JL_HIDDEN fputs(const char *str, FILE *out) {
return fwrite(str, strlen(str), out);
}

void * loader_malloc(const size_t size) {
void JL_HIDDEN *malloc(const size_t size) {
return HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, size);
}

void * loader_realloc(void * mem, const size_t size) {
void JL_HIDDEN *realloc(void * mem, const size_t size) {
return HeapReAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, mem, size);
}

void loader_free(void* mem) {
void JL_HIDDEN free(void* mem) {
HeapFree(GetProcessHeap(), 0, mem);
}

Expand Down Expand Up @@ -110,7 +110,7 @@ void setup_stdio() {
_stderr.isconsole = GetConsoleMode(_stderr.fd, &mode);
}

void loader_exit(int code) {
void JL_HIDDEN exit(int code) {
ExitProcess(code);
}

Expand Down Expand Up @@ -148,21 +148,21 @@ wchar_t *utf8_to_wchar(const char * str) {
return wstr;
}

size_t loader_strlen(const char * x) {
size_t JL_HIDDEN strlen(const char * x) {
int idx = 0;
while (x[idx] != 0)
idx++;
return idx;
}

size_t loader_wcslen(const wchar_t * x) {
size_t JL_HIDDEN wcslen(const wchar_t * x) {
int idx = 0;
while (x[idx] != 0)
idx++;
return idx;
}

char * loader_strncat(char * base, const char * tail, size_t maxlen) {
char JL_HIDDEN *strncat(char * base, const char * tail, size_t maxlen) {
int base_len = strlen(base);
int tail_len = strlen(tail);
for (int idx=base_len; idx<min(maxlen, base_len + tail_len); ++idx) {
Expand All @@ -171,14 +171,21 @@ char * loader_strncat(char * base, const char * tail, size_t maxlen) {
return base;
}

void * loader_memcpy(void * dest, const void * src, size_t len) {
void JL_HIDDEN *memcpy(void * dest, const void * src, size_t len) {
for (int idx=0; idx<len; ++idx) {
((char *)dest)[idx] = ((const char *)src)[idx];
}
return dest;
}

char * loader_dirname(char * x) {
void JL_HIDDEN *memset(void *s, int c, size_t n) {
unsigned char* p = s;
while(n--)
*p++ = (unsigned char)c;
return s;
}

char JL_HIDDEN *dirname(char * x) {
int idx = strlen(x);
while (idx > 0 && x[idx] != PATHSEPSTRING[0]) {
idx -= 1;
Expand All @@ -198,7 +205,7 @@ char * loader_dirname(char * x) {
return x;
}

char * loader_strchr(const char * haystack, int needle) {
char JL_HIDDEN *strchr(const char * haystack, int needle) {
int idx=0;
while (haystack[idx] != needle) {
if (haystack[idx] == 0) {
Expand Down

0 comments on commit 42fb22e

Please sign in to comment.