LLVM OpenMP* Runtime Library
src
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
#ifndef KMP_SAFE_C_API_H
12
#define KMP_SAFE_C_API_H
13
14
#include "kmp_platform.h"
15
#include <string.h>
16
17
// Replacement for banned C API
18
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
22
23
#define RSIZE_MAX_STR (4UL << 10) // 4KB
24
25
// _malloca was suggested, but it is not a drop-in replacement for _alloca
26
#define KMP_ALLOCA _alloca
27
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
33
34
// Use this only when buffer size is unknown
35
#define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
36
37
#define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
38
39
// Use this only when buffer size is unknown
40
#define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
41
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)
45
46
#else // KMP_OS_WINDOWS
47
48
// For now, these macros use the existing API.
49
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
60
61
#endif // KMP_OS_WINDOWS
62
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'
;
70
}
else
{
71
KMP_STRNCPY_S(buffer, buf_size, src, src_size);
72
}
73
}
74
75
#endif // KMP_SAFE_C_API_H
Generated by
1.8.13