llvm/openmp/runtime/src/kmp_safe_c_api.h


//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef KMP_SAFE_C_API_H
#define KMP_SAFE_C_API_H

#include <type_traits>
#include "kmp_platform.h"
#include <string.h>

// Replacement for banned C API

// Not every unsafe call listed here is handled now, but keeping everything
// in one place should be handy for future maintenance.
#if KMP_OS_WINDOWS && KMP_MSVC_COMPAT

#define RSIZE_MAX_STR

// _malloca was suggested, but it is not a drop-in replacement for _alloca
#define KMP_ALLOCA

#define KMP_MEMCPY_S
#define KMP_SNPRINTF
#define KMP_SSCANF
#define KMP_STRCPY_S
#define KMP_STRNCPY_S
#define KMP_STRNCAT_S

// Use this only when buffer size is unknown
#define KMP_MEMCPY

template <typename T, bool B = std::is_array<T>::value>
struct kmp_get_rmax_t {};
template <typename T> struct kmp_get_rmax_t<T, false> {
  static const size_t value = RSIZE_MAX_STR;
};
template <typename T> struct kmp_get_rmax_t<T, true> {
  static const size_t value = sizeof(T);
};
#define KMP_STRLEN

// Use this only when buffer size is unknown
#define KMP_STRNCPY

// _TRUNCATE insures buffer size > max string to print.
#define KMP_VSNPRINTF

#else // KMP_OS_WINDOWS

// For now, these macros use the existing API.

#if KMP_OS_NETBSD
#define KMP_ALLOCA
#else
#define KMP_ALLOCA
#endif
#define KMP_MEMCPY_S(dst, bsz, src, cnt)
#define KMP_SNPRINTF
#define KMP_SSCANF
#define KMP_STRCPY_S(dst, bsz, src)
#define KMP_STRNCPY_S(dst, bsz, src, cnt)
#define KMP_STRNCAT_S(dst, bsz, src, cnt)
#define KMP_VSNPRINTF
#define KMP_STRNCPY
#define KMP_STRLEN
#define KMP_MEMCPY

#endif // KMP_OS_WINDOWS

// Offer truncated version of strncpy
static inline void __kmp_strncpy_truncate(char *buffer, size_t buf_size,
                                          char const *src, size_t src_size) {}

#endif // KMP_SAFE_C_API_H