diff options
author | Yuchen Zeng <zyc@google.com> | 2016-05-31 14:06:01 -0700 |
---|---|---|
committer | Yuchen Zeng <zyc@google.com> | 2016-05-31 14:19:57 -0700 |
commit | 4594bd9d1b966e33d27396eb015f289eba82f443 (patch) | |
tree | fa1a65ab679c309d227b5cb493452da632c5c983 /src/core/lib/support | |
parent | 886b3441441768216d8cb61ec63b8f974833c931 (diff) | |
parent | 0d6196025e62aea5aabc6341459f2c370e264230 (diff) |
Merge remote-tracking branch 'upstream/master' into base
Diffstat (limited to 'src/core/lib/support')
-rw-r--r-- | src/core/lib/support/env_windows.c | 44 | ||||
-rw-r--r-- | src/core/lib/support/log_linux.c | 4 | ||||
-rw-r--r-- | src/core/lib/support/log_windows.c | 18 | ||||
-rw-r--r-- | src/core/lib/support/murmur_hash.c | 8 | ||||
-rw-r--r-- | src/core/lib/support/string_util_windows.c | 94 | ||||
-rw-r--r-- | src/core/lib/support/string_windows.c | 32 | ||||
-rw-r--r-- | src/core/lib/support/time_windows.c | 4 | ||||
-rw-r--r-- | src/core/lib/support/tmpfile_msys.c | 73 | ||||
-rw-r--r-- | src/core/lib/support/tmpfile_posix.c | 4 | ||||
-rw-r--r-- | src/core/lib/support/tmpfile_windows.c | 4 |
10 files changed, 208 insertions, 77 deletions
diff --git a/src/core/lib/support/env_windows.c b/src/core/lib/support/env_windows.c index 796b93f19e..9116959442 100644 --- a/src/core/lib/support/env_windows.c +++ b/src/core/lib/support/env_windows.c @@ -33,41 +33,47 @@ #include <grpc/support/port_platform.h> -#ifdef GPR_WINDOWS +#ifdef GPR_WINDOWS_ENV + +#include <windows.h> #include "src/core/lib/support/env.h" #include "src/core/lib/support/string.h" - -#ifdef __MINGW32__ -errno_t getenv_s(size_t *size_needed, char *buffer, size_t size, - const char *varname); -#else -#include <stdlib.h> -#endif +#include "src/core/lib/support/string_windows.h" #include <grpc/support/alloc.h> #include <grpc/support/log.h> #include <grpc/support/string_util.h> char *gpr_getenv(const char *name) { - size_t size; char *result = NULL; - errno_t err; + DWORD size; + LPTSTR tresult = NULL; + LPTSTR tname = gpr_char_to_tchar(name); + DWORD ret; - err = getenv_s(&size, NULL, 0, name); - if (err || (size == 0)) return NULL; - result = gpr_malloc(size); - err = getenv_s(&size, result, size, name); - if (err) { - gpr_free(result); + ret = GetEnvironmentVariable(tname, NULL, 0); + if (ret == 0) return NULL; + size = ret * (DWORD)sizeof(TCHAR); + tresult = gpr_malloc(size); + ret = GetEnvironmentVariable(tname, tresult, size); + gpr_free(tname); + if (ret == 0) { + gpr_free(tresult); return NULL; } + result = gpr_tchar_to_char(tresult); + gpr_free(tresult); return result; } void gpr_setenv(const char *name, const char *value) { - errno_t res = _putenv_s(name, value); - GPR_ASSERT(res == 0); + LPTSTR tname = gpr_char_to_tchar(name); + LPTSTR tvalue = gpr_char_to_tchar(value); + BOOL res = SetEnvironmentVariable(tname, tvalue); + gpr_free(tname); + gpr_free(tvalue); + GPR_ASSERT(res); } -#endif /* GPR_WINDOWS */ +#endif /* GPR_WINDOWS_ENV */ diff --git a/src/core/lib/support/log_linux.c b/src/core/lib/support/log_linux.c index ff3febb38e..ca04c022e3 100644 --- a/src/core/lib/support/log_linux.c +++ b/src/core/lib/support/log_linux.c @@ -41,7 +41,7 @@ #include <grpc/support/port_platform.h> -#ifdef GPR_LINUX +#ifdef GPR_LINUX_LOG #include <grpc/support/alloc.h> #include <grpc/support/log.h> @@ -103,4 +103,4 @@ void gpr_default_log(gpr_log_func_args *args) { gpr_free(prefix); } -#endif +#endif /* GPR_LINUX_LOG */ diff --git a/src/core/lib/support/log_windows.c b/src/core/lib/support/log_windows.c index 801042f39d..ea898c359d 100644 --- a/src/core/lib/support/log_windows.c +++ b/src/core/lib/support/log_windows.c @@ -33,7 +33,7 @@ #include <grpc/support/port_platform.h> -#ifdef GPR_WINDOWS +#ifdef GPR_WINDOWS_LOG #include <stdarg.h> #include <stdio.h> @@ -109,18 +109,4 @@ void gpr_default_log(gpr_log_func_args *args) { fflush(stderr); } -char *gpr_format_message(int messageid) { - LPTSTR tmessage; - char *message; - DWORD status = FormatMessage( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, (DWORD)messageid, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)(&tmessage), 0, NULL); - if (status == 0) return gpr_strdup("Unable to retrieve error string"); - message = gpr_tchar_to_char(tmessage); - LocalFree(tmessage); - return message; -} - -#endif /* GPR_WINDOWS */ +#endif /* GPR_WINDOWS_LOG */ diff --git a/src/core/lib/support/murmur_hash.c b/src/core/lib/support/murmur_hash.c index 5711fff0c0..7137c1f313 100644 --- a/src/core/lib/support/murmur_hash.c +++ b/src/core/lib/support/murmur_hash.c @@ -33,6 +33,8 @@ #include "src/core/lib/support/murmur_hash.h" +#include <string.h> + #define ROTL32(x, r) ((x) << (r)) | ((x) >> (32 - (r))) #define FMIX32(h) \ @@ -42,10 +44,6 @@ (h) *= 0xc2b2ae35; \ (h) ^= (h) >> 16; -/* Block read - if your platform needs to do endian-swapping or can only - handle aligned reads, do the conversion here */ -#define GETBLOCK32(p, i) (p)[(i)] - uint32_t gpr_murmur_hash3(const void *key, size_t len, uint32_t seed) { const uint8_t *data = (const uint8_t *)key; const size_t nblocks = len / 4; @@ -62,7 +60,7 @@ uint32_t gpr_murmur_hash3(const void *key, size_t len, uint32_t seed) { /* body */ for (i = -(int)nblocks; i; i++) { - k1 = GETBLOCK32(blocks, i); + memcpy(&k1, blocks + i, sizeof(uint32_t)); k1 *= c1; k1 = ROTL32(k1, 15); diff --git a/src/core/lib/support/string_util_windows.c b/src/core/lib/support/string_util_windows.c new file mode 100644 index 0000000000..049c9a8c04 --- /dev/null +++ b/src/core/lib/support/string_util_windows.c @@ -0,0 +1,94 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* Posix code for gpr snprintf support. */ + +#include <grpc/support/port_platform.h> + +#ifdef GPR_WINDOWS + +/* Some platforms (namely msys) need wchar to be included BEFORE + anything else, especially strsafe.h. */ +#include <wchar.h> + +#include <stdarg.h> +#include <stdio.h> +#include <string.h> +#include <strsafe.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/string_util.h> + +#include "src/core/lib/support/string.h" + +#if defined UNICODE || defined _UNICODE +LPTSTR +gpr_char_to_tchar(LPCSTR input) { + LPTSTR ret; + int needed = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0); + if (needed <= 0) return NULL; + ret = gpr_malloc((unsigned)needed * sizeof(TCHAR)); + MultiByteToWideChar(CP_UTF8, 0, input, -1, ret, needed); + return ret; +} + +LPSTR +gpr_tchar_to_char(LPCTSTR input) { + LPSTR ret; + int needed = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL); + if (needed <= 0) return NULL; + ret = gpr_malloc((unsigned)needed); + WideCharToMultiByte(CP_UTF8, 0, input, -1, ret, needed, NULL, NULL); + return ret; +} +#else +char *gpr_tchar_to_char(LPTSTR input) { return gpr_strdup(input); } + +char *gpr_char_to_tchar(LPTSTR input) { return gpr_strdup(input); } +#endif + +char *gpr_format_message(int messageid) { + LPTSTR tmessage; + char *message; + DWORD status = FormatMessage( + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, (DWORD)messageid, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), + (LPTSTR)(&tmessage), 0, NULL); + if (status == 0) return gpr_strdup("Unable to retrieve error string"); + message = gpr_tchar_to_char(tmessage); + LocalFree(tmessage); + return message; +} + +#endif /* GPR_WINDOWS */ diff --git a/src/core/lib/support/string_windows.c b/src/core/lib/support/string_windows.c index c89c89f428..ecc2a3a4e5 100644 --- a/src/core/lib/support/string_windows.c +++ b/src/core/lib/support/string_windows.c @@ -31,11 +31,11 @@ * */ -/* Posix code for gpr snprintf support. */ +/* Windows code for gpr snprintf support. */ #include <grpc/support/port_platform.h> -#ifdef GPR_WINDOWS +#ifdef GPR_WINDOWS_STRING #include <stdarg.h> #include <stdio.h> @@ -80,30 +80,4 @@ int gpr_asprintf(char **strp, const char *format, ...) { return -1; } -#if defined UNICODE || defined _UNICODE -LPTSTR -gpr_char_to_tchar(LPCSTR input) { - LPTSTR ret; - int needed = MultiByteToWideChar(CP_UTF8, 0, input, -1, NULL, 0); - if (needed <= 0) return NULL; - ret = gpr_malloc((unsigned)needed * sizeof(TCHAR)); - MultiByteToWideChar(CP_UTF8, 0, input, -1, ret, needed); - return ret; -} - -LPSTR -gpr_tchar_to_char(LPCTSTR input) { - LPSTR ret; - int needed = WideCharToMultiByte(CP_UTF8, 0, input, -1, NULL, 0, NULL, NULL); - if (needed <= 0) return NULL; - ret = gpr_malloc((unsigned)needed); - WideCharToMultiByte(CP_UTF8, 0, input, -1, ret, needed, NULL, NULL); - return ret; -} -#else -char *gpr_tchar_to_char(LPTSTR input) { return gpr_strdup(input); } - -char *gpr_char_to_tchar(LPTSTR input) { return gpr_strdup(input); } -#endif - -#endif /* GPR_WINDOWS */ +#endif /* GPR_WINDOWS_STRING */ diff --git a/src/core/lib/support/time_windows.c b/src/core/lib/support/time_windows.c index 5ba62b3196..6459732879 100644 --- a/src/core/lib/support/time_windows.c +++ b/src/core/lib/support/time_windows.c @@ -35,7 +35,7 @@ #include <grpc/support/port_platform.h> -#ifdef GPR_WINDOWS +#ifdef GPR_WINDOWS_TIME #include <grpc/support/log.h> #include <grpc/support/time.h> @@ -107,4 +107,4 @@ void gpr_sleep_until(gpr_timespec until) { } } -#endif /* GPR_WINDOWS */ +#endif /* GPR_WINDOWS_TIME */ diff --git a/src/core/lib/support/tmpfile_msys.c b/src/core/lib/support/tmpfile_msys.c new file mode 100644 index 0000000000..4f566c4c28 --- /dev/null +++ b/src/core/lib/support/tmpfile_msys.c @@ -0,0 +1,73 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include <grpc/support/port_platform.h> + +#ifdef GPR_MSYS_TMPFILE + +#include <io.h> +#include <stdio.h> +#include <string.h> +#include <tchar.h> + +#include <grpc/support/alloc.h> +#include <grpc/support/log.h> +#include <grpc/support/string_util.h> + +#include "src/core/lib/support/string_windows.h" +#include "src/core/lib/support/tmpfile.h" + +FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) { + FILE *result = NULL; + char tmp_filename[MAX_PATH]; + UINT success; + + if (tmp_filename_out != NULL) *tmp_filename_out = NULL; + + /* Generate a unique filename with our template + temporary path. */ + success = GetTempFileNameA(".", prefix, 0, tmp_filename); + fprintf(stderr, "success = %d\n", success); + + if (success) { + /* Open a file there. */ + result = fopen(tmp_filename, "wb+"); + fprintf(stderr, "result = %p\n", result); + } + if (result != NULL && tmp_filename_out) { + *tmp_filename_out = gpr_strdup(tmp_filename); + } + + return result; +} + +#endif /* GPR_MSYS_TMPFILE */ diff --git a/src/core/lib/support/tmpfile_posix.c b/src/core/lib/support/tmpfile_posix.c index 9e0e7ad808..0cd4bb6fc3 100644 --- a/src/core/lib/support/tmpfile_posix.c +++ b/src/core/lib/support/tmpfile_posix.c @@ -33,7 +33,7 @@ #include <grpc/support/port_platform.h> -#ifdef GPR_POSIX_FILE +#ifdef GPR_POSIX_TMPFILE #include "src/core/lib/support/tmpfile.h" @@ -82,4 +82,4 @@ end: return result; } -#endif /* GPR_POSIX_FILE */ +#endif /* GPR_POSIX_TMPFILE */ diff --git a/src/core/lib/support/tmpfile_windows.c b/src/core/lib/support/tmpfile_windows.c index 11d05a65ce..542f53e589 100644 --- a/src/core/lib/support/tmpfile_windows.c +++ b/src/core/lib/support/tmpfile_windows.c @@ -33,7 +33,7 @@ #include <grpc/support/port_platform.h> -#ifdef GPR_WINDOWS +#ifdef GPR_WINDOWS_TMPFILE #include <io.h> #include <stdio.h> @@ -81,4 +81,4 @@ end: return result; } -#endif /* GPR_WINDOWS */ +#endif /* GPR_WINDOWS_TMPFILE */ |