Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 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 | |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 11 | #ifndef KMP_SAFE_C_API_H |
| 12 | #define KMP_SAFE_C_API_H |
| 13 | |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 14 | // Replacement for banned C API |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 15 | |
| 16 | // Not every unsafe call listed here is handled now, but keeping everything |
| 17 | // in one place should be handy for future maintenance. |
| 18 | #if KMP_OS_WINDOWS |
| 19 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 20 | #define RSIZE_MAX_STR (4UL << 10) // 4KB |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 21 | |
| 22 | // _malloca was suggested, but it is not a drop-in replacement for _alloca |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 23 | #define KMP_ALLOCA _alloca |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 24 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 25 | #define KMP_MEMCPY_S memcpy_s |
| 26 | #define KMP_SNPRINTF sprintf_s |
| 27 | #define KMP_SSCANF sscanf_s |
| 28 | #define KMP_STRCPY_S strcpy_s |
| 29 | #define KMP_STRNCPY_S strncpy_s |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 30 | |
| 31 | // Use this only when buffer size is unknown |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 32 | #define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt) |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 33 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 34 | #define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR) |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 35 | |
| 36 | // Use this only when buffer size is unknown |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 37 | #define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt) |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 38 | |
| 39 | // _TRUNCATE insures buffer size > max string to print. |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 40 | #define KMP_VSNPRINTF(dst, cnt, fmt, arg) \ |
| 41 | vsnprintf_s(dst, cnt, _TRUNCATE, fmt, arg) |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 42 | |
| 43 | #else // KMP_OS_WINDOWS |
| 44 | |
| 45 | // For now, these macros use the existing API. |
| 46 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 47 | #define KMP_ALLOCA alloca |
| 48 | #define KMP_MEMCPY_S(dst, bsz, src, cnt) memcpy(dst, src, cnt) |
| 49 | #define KMP_SNPRINTF snprintf |
| 50 | #define KMP_SSCANF sscanf |
| 51 | #define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src) |
| 52 | #define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt) |
| 53 | #define KMP_VSNPRINTF vsnprintf |
| 54 | #define KMP_STRNCPY strncpy |
| 55 | #define KMP_STRLEN strlen |
| 56 | #define KMP_MEMCPY memcpy |
Andrey Churbanov | 74bf17b | 2015-04-02 13:27:08 +0000 | [diff] [blame] | 57 | |
| 58 | #endif // KMP_OS_WINDOWS |
| 59 | |
| 60 | #endif // KMP_SAFE_C_API_H |