From a58afe9c11e2a9bf8352af85fbde1ce9502f8c2d Mon Sep 17 00:00:00 2001 From: Diptorup Deb Date: Tue, 5 Oct 2021 16:22:19 -0500 Subject: [PATCH] Add a helper to convert a C++ string to and a C string. --- .../helper/include/dpctl_string_utils.hpp | 61 +++++++++++++++++++ dpctl-capi/source/dpctl_service.cpp | 16 +---- .../source/dpctl_sycl_device_interface.cpp | 40 +++--------- .../source/dpctl_sycl_device_manager.cpp | 14 +---- .../source/dpctl_sycl_kernel_interface.cpp | 10 +-- .../source/dpctl_sycl_platform_interface.cpp | 46 +++----------- 6 files changed, 85 insertions(+), 102 deletions(-) create mode 100644 dpctl-capi/helper/include/dpctl_string_utils.hpp diff --git a/dpctl-capi/helper/include/dpctl_string_utils.hpp b/dpctl-capi/helper/include/dpctl_string_utils.hpp new file mode 100644 index 0000000000..3b1d0ae73c --- /dev/null +++ b/dpctl-capi/helper/include/dpctl_string_utils.hpp @@ -0,0 +1,61 @@ +//===--- dpctl_string_utils.hpp - C++ to C string converted -*-C++-*- ===// +// +// Data Parallel Control (dpctl) +// +// Copyright 2020-2021 Intel Corporation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// Helper function to convert a C++ string to a C string. +//===----------------------------------------------------------------------===// +#include +#include +#include + +#pragma once + +namespace dpctl +{ +namespace helper +{ +/*! + * @brief Convert a C++ std::string to a const char* and return the string to + * caller. + * + * @param str A C++ string that has to be converted to a C string. + * @return A const char* string representation of the C++ string. + */ +static inline __dpctl_give const char * +cstring_from_string(const std::string &str) +{ + char *cstr = nullptr; + try { + auto cstr_len = str.length() + 1; + cstr = new char[cstr_len]; +#ifdef _WIN32 + strncpy_s(cstr, cstr_len, str.c_str(), cstr_len); +#else + std::strncpy(cstr, str.c_str(), cstr_len); +#endif + } catch (std::bad_alloc const &ba) { + // \todo log error + std::cerr << ba.what() << '\n'; + } + + return cstr; +} +} // namespace helper +} // namespace dpctl diff --git a/dpctl-capi/source/dpctl_service.cpp b/dpctl-capi/source/dpctl_service.cpp index a9b6816f8c..3457980721 100644 --- a/dpctl-capi/source/dpctl_service.cpp +++ b/dpctl-capi/source/dpctl_service.cpp @@ -26,6 +26,7 @@ #include "dpctl_service.h" #include "Config/dpctl_config.h" +#include "../helper/include/dpctl_string_utils.hpp" #include #include #include @@ -33,18 +34,5 @@ __dpctl_give const char *DPCTLService_GetDPCPPVersion(void) { std::string version = DPCTL_DPCPP_VERSION; - char *version_cstr = nullptr; - try { - auto cstr_len = version.length() + 1; - version_cstr = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(version_cstr, cstr_len, version.c_str(), cstr_len); -#else - std::strncpy(version_cstr, version.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; - } - return version_cstr; + return dpctl::helper::cstring_from_string(version); } diff --git a/dpctl-capi/source/dpctl_sycl_device_interface.cpp b/dpctl-capi/source/dpctl_sycl_device_interface.cpp index 8ef71b65c4..092d5ea273 100644 --- a/dpctl-capi/source/dpctl_sycl_device_interface.cpp +++ b/dpctl-capi/source/dpctl_sycl_device_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_device_interface.h" +#include "../helper/include/dpctl_string_utils.hpp" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" #include "dpctl_sycl_device_manager.h" @@ -307,21 +308,12 @@ DPCTLDevice_GetPlatform(__dpctl_keep const DPCTLSyclDeviceRef DRef) __dpctl_give const char * DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef) { - char *cstr_name = nullptr; + const char *cstr_name = nullptr; auto D = unwrap(DRef); if (D) { try { auto name = D->get_info(); - auto cstr_len = name.length() + 1; - cstr_name = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_name, cstr_len, name.c_str(), cstr_len); -#else - std::strncpy(cstr_name, name.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + cstr_name = dpctl::helper::cstring_from_string(name); } catch (runtime_error const &re) { // \todo log error std::cerr << re.what() << '\n'; @@ -333,21 +325,12 @@ DPCTLDevice_GetName(__dpctl_keep const DPCTLSyclDeviceRef DRef) __dpctl_give const char * DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef) { - char *cstr_vendor = nullptr; + const char *cstr_vendor = nullptr; auto D = unwrap(DRef); if (D) { try { auto vendor = D->get_info(); - auto cstr_len = vendor.length() + 1; - cstr_vendor = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_vendor, cstr_len, vendor.c_str(), cstr_len); -#else - std::strncpy(cstr_vendor, vendor.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + cstr_vendor = dpctl::helper::cstring_from_string(vendor); } catch (runtime_error const &re) { // \todo log error std::cerr << re.what() << '\n'; @@ -359,21 +342,12 @@ DPCTLDevice_GetVendor(__dpctl_keep const DPCTLSyclDeviceRef DRef) __dpctl_give const char * DPCTLDevice_GetDriverVersion(__dpctl_keep const DPCTLSyclDeviceRef DRef) { - char *cstr_driver = nullptr; + const char *cstr_driver = nullptr; auto D = unwrap(DRef); if (D) { try { auto driver = D->get_info(); - auto cstr_len = driver.length() + 1; - cstr_driver = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_driver, cstr_len, driver.c_str(), cstr_len); -#else - std::strncpy(cstr_driver, driver.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + cstr_driver = dpctl::helper::cstring_from_string(driver); } catch (runtime_error const &re) { // \todo log error std::cerr << re.what() << '\n'; diff --git a/dpctl-capi/source/dpctl_sycl_device_manager.cpp b/dpctl-capi/source/dpctl_sycl_device_manager.cpp index 8fce8609c4..06bf3cf16a 100644 --- a/dpctl-capi/source/dpctl_sycl_device_manager.cpp +++ b/dpctl-capi/source/dpctl_sycl_device_manager.cpp @@ -24,6 +24,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_device_manager.h" +#include "../helper/include/dpctl_string_utils.hpp" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" #include "dpctl_sycl_enum_types.h" @@ -171,21 +172,12 @@ DPCTLDeviceMgr_GetDevices(int device_identifier) __dpctl_give const char * DPCTLDeviceMgr_GetDeviceInfoStr(__dpctl_keep const DPCTLSyclDeviceRef DRef) { - char *cstr_info = nullptr; + const char *cstr_info = nullptr; auto D = unwrap(DRef); if (D) { try { auto infostr = get_device_info_str(*D); - auto cstr_len = infostr.length() + 1; - cstr_info = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_info, cstr_len, infostr.c_str(), cstr_len); -#else - std::strncpy(cstr_info, infostr.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + cstr_info = dpctl::helper::cstring_from_string(infostr); } catch (runtime_error const &re) { // \todo log error std::cerr << re.what() << '\n'; diff --git a/dpctl-capi/source/dpctl_sycl_kernel_interface.cpp b/dpctl-capi/source/dpctl_sycl_kernel_interface.cpp index 2663659d55..b25d73ada0 100644 --- a/dpctl-capi/source/dpctl_sycl_kernel_interface.cpp +++ b/dpctl-capi/source/dpctl_sycl_kernel_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_kernel_interface.h" +#include "../helper/include/dpctl_string_utils.hpp" #include "Support/CBindingWrapping.h" #include /* Sycl headers */ @@ -49,14 +50,7 @@ DPCTLKernel_GetFunctionName(__dpctl_keep const DPCTLSyclKernelRef Kernel) auto kernel_name = SyclKernel->get_info(); if (kernel_name.empty()) return nullptr; - auto cstr_len = kernel_name.length() + 1; - auto cstr_name = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_name, cstr_len, kernel_name.c_str(), cstr_len); -#else - std::strncpy(cstr_name, kernel_name.c_str(), cstr_len); -#endif - return cstr_name; + return dpctl::helper::cstring_from_string(kernel_name); } size_t DPCTLKernel_GetNumArgs(__dpctl_keep const DPCTLSyclKernelRef Kernel) diff --git a/dpctl-capi/source/dpctl_sycl_platform_interface.cpp b/dpctl-capi/source/dpctl_sycl_platform_interface.cpp index 56e3fd4a50..3eb3592f79 100644 --- a/dpctl-capi/source/dpctl_sycl_platform_interface.cpp +++ b/dpctl-capi/source/dpctl_sycl_platform_interface.cpp @@ -25,6 +25,7 @@ //===----------------------------------------------------------------------===// #include "dpctl_sycl_platform_interface.h" +#include "../helper/include/dpctl_string_utils.hpp" #include "../helper/include/dpctl_utils_helper.h" #include "Support/CBindingWrapping.h" #include @@ -121,88 +122,61 @@ DPCTLPlatform_GetBackend(__dpctl_keep const DPCTLSyclPlatformRef PRef) __dpctl_give const char * DPCTLPlatform_GetName(__dpctl_keep const DPCTLSyclPlatformRef PRef) { - char *cstr_name = nullptr; auto P = unwrap(PRef); if (P) { try { auto name = P->get_info(); - auto cstr_len = name.length() + 1; - cstr_name = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_name, cstr_len, name.c_str(), cstr_len); -#else - std::strncpy(cstr_name, name.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + return dpctl::helper::cstring_from_string(name); } catch (runtime_error const &re) { // \todo log error std::cerr << re.what() << '\n'; + return nullptr; } } else { std::cerr << "Name cannot be looked up for a NULL platform\n"; + return nullptr; } - return cstr_name; } __dpctl_give const char * DPCTLPlatform_GetVendor(__dpctl_keep const DPCTLSyclPlatformRef PRef) { - char *cstr_vendor = nullptr; auto P = unwrap(PRef); if (P) { try { auto vendor = P->get_info(); - auto cstr_len = vendor.length() + 1; - cstr_vendor = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_vendor, cstr_len, vendor.c_str(), cstr_len); -#else - std::strncpy(cstr_vendor, vendor.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + return dpctl::helper::cstring_from_string(vendor); } catch (runtime_error const &re) { // \todo log error std::cerr << re.what() << '\n'; + return nullptr; } } else { std::cerr << "Vendor cannot be looked up for a NULL platform\n"; + return nullptr; } - return cstr_vendor; } __dpctl_give const char * DPCTLPlatform_GetVersion(__dpctl_keep const DPCTLSyclPlatformRef PRef) { - char *cstr_driver = nullptr; auto P = unwrap(PRef); if (P) { try { auto driver = P->get_info(); - auto cstr_len = driver.length() + 1; - cstr_driver = new char[cstr_len]; -#ifdef _WIN32 - strncpy_s(cstr_driver, cstr_len, driver.c_str(), cstr_len); -#else - std::strncpy(cstr_driver, driver.c_str(), cstr_len); -#endif - } catch (std::bad_alloc const &ba) { - // \todo log error - std::cerr << ba.what() << '\n'; + return dpctl::helper::cstring_from_string(driver); } catch (runtime_error const &re) { // \todo log error std::cerr << re.what() << '\n'; + return nullptr; } } else { std::cerr << "Driver version cannot be looked up for a NULL platform\n"; + return nullptr; } - return cstr_driver; } __dpctl_give DPCTLPlatformVectorRef DPCTLPlatform_GetPlatforms()