LLVM OpenMP* Runtime Library
kmp_safe_c_api.h
1 
2 //===----------------------------------------------------------------------===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.txt for details.
8 //
9 //===----------------------------------------------------------------------===//
10 
11 
12 #ifndef KMP_SAFE_C_API_H
13 #define KMP_SAFE_C_API_H
14 
15 // Replacement for banned C API
16 
17 // Not every unsafe call listed here is handled now, but keeping everything
18 // in one place should be handy for future maintenance.
19 #if KMP_OS_WINDOWS
20 
21 #define RSIZE_MAX_STR (4UL << 10) // 4KB
22 
23 // _malloca was suggested, but it is not a drop-in replacement for _alloca
24 #define KMP_ALLOCA _alloca
25 
26 #define KMP_MEMCPY_S memcpy_s
27 #define KMP_SNPRINTF sprintf_s
28 #define KMP_SSCANF sscanf_s
29 #define KMP_STRCPY_S strcpy_s
30 #define KMP_STRNCPY_S strncpy_s
31 
32 // Use this only when buffer size is unknown
33 #define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
34 
35 #define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
36 
37 // Use this only when buffer size is unknown
38 #define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
39 
40 // _TRUNCATE insures buffer size > max string to print.
41 #define KMP_VSNPRINTF(dst, cnt, fmt, arg) \
42  vsnprintf_s(dst, cnt, _TRUNCATE, fmt, arg)
43 
44 #else // KMP_OS_WINDOWS
45 
46 // For now, these macros use the existing API.
47 
48 #define KMP_ALLOCA alloca
49 #define KMP_MEMCPY_S(dst, bsz, src, cnt) memcpy(dst, src, cnt)
50 #define KMP_SNPRINTF snprintf
51 #define KMP_SSCANF sscanf
52 #define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src)
53 #define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt)
54 #define KMP_VSNPRINTF vsnprintf
55 #define KMP_STRNCPY strncpy
56 #define KMP_STRLEN strlen
57 #define KMP_MEMCPY memcpy
58 
59 #endif // KMP_OS_WINDOWS
60 
61 #endif // KMP_SAFE_C_API_H