2 //===----------------------------------------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.txt for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef KMP_SAFE_C_API_H
12 #define KMP_SAFE_C_API_H
14 #include "kmp_platform.h"
17 // Replacement for banned C API
19 // Not every unsafe call listed here is handled now, but keeping everything
20 // in one place should be handy for future maintenance.
21 #if KMP_OS_WINDOWS && KMP_MSVC_COMPAT
23 #define RSIZE_MAX_STR (4UL << 10) // 4KB
25 // _malloca was suggested, but it is not a drop-in replacement for _alloca
26 #define KMP_ALLOCA _alloca
28 #define KMP_MEMCPY_S memcpy_s
29 #define KMP_SNPRINTF sprintf_s
30 #define KMP_SSCANF sscanf_s
31 #define KMP_STRCPY_S strcpy_s
32 #define KMP_STRNCPY_S strncpy_s
34 // Use this only when buffer size is unknown
35 #define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
37 #define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
39 // Use this only when buffer size is unknown
40 #define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
42 // _TRUNCATE insures buffer size > max string to print.
43 #define KMP_VSNPRINTF(dst, cnt, fmt, arg) \
44 vsnprintf_s(dst, cnt, _TRUNCATE, fmt, arg)
46 #else // KMP_OS_WINDOWS
48 // For now, these macros use the existing API.
50 #define KMP_ALLOCA alloca
51 #define KMP_MEMCPY_S(dst, bsz, src, cnt) memcpy(dst, src, cnt)
52 #define KMP_SNPRINTF snprintf
53 #define KMP_SSCANF sscanf
54 #define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src)
55 #define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt)
56 #define KMP_VSNPRINTF vsnprintf
57 #define KMP_STRNCPY strncpy
58 #define KMP_STRLEN strlen
59 #define KMP_MEMCPY memcpy
61 #endif // KMP_OS_WINDOWS
63 // Offer truncated version of strncpy
64 static inline void __kmp_strncpy_truncate(char *buffer, size_t buf_size,
65 char const *src, size_t src_size) {
66 if (src_size >= buf_size) {
67 src_size = buf_size - 1;
68 KMP_STRNCPY_S(buffer, buf_size, src, src_size);
69 buffer[buf_size - 1] = '\0';
71 KMP_STRNCPY_S(buffer, buf_size, src, src_size);
75 #endif // KMP_SAFE_C_API_H