diff --git a/include/cpp_define.h b/include/cpp_define.h new file mode 100644 index 0000000000000000000000000000000000000000..8bb4ea529a0f898f8fcf1790acac963198398ade --- /dev/null +++ b/include/cpp_define.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * 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. + */ + +#ifndef CPP_DEFINE_H +#define CPP_DEFINE_H + +#ifndef is_ohos_lite +#define STRING_VIEW std::string_view +#else +#include +#define STRING_VIEW std::string +#endif + +#endif diff --git a/include/report.h b/include/report.h index e1a9644a4e46b98c3cd1a79fe7786e3d2e68a2b1..bfa2aa65193b8ebe8c837e868230ee394367e27c 100644 --- a/include/report.h +++ b/include/report.h @@ -25,6 +25,7 @@ #include "debug_logger.h" // remove me latter #include "report_json_file.h" +#include "string_view_util.h" #include "utilities.h" #include "virtual_runtime.h" diff --git a/include/string_view_util.h b/include/string_view_util.h new file mode 100644 index 0000000000000000000000000000000000000000..b44a59ea775b368b93391ce19b440f617c309477 --- /dev/null +++ b/include/string_view_util.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * 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. + */ +#ifndef STRING_VIEW_UTIL_H +#define STRING_VIEW_UTIL_H + +#include +#include +#include +#include +#include +#include "cpp_define.h" + +namespace OHOS { +namespace HiviewDFX { +class StringViewHold { +public: + static StringViewHold &Get() + { + static StringViewHold instance; + return instance; + } + + const char* Hold(STRING_VIEW view) + { + pthread_spin_lock(&spin_lock_); + if (view.size() == 0) { + pthread_spin_unlock(&spin_lock_); + return ""; + } + + char *p = new (std::nothrow) char[view.size() + 1]; + if (p == nullptr) { + pthread_spin_unlock(&spin_lock_); + return ""; + } + if (memset_s(p, view.size() + 1, '\0', view.size() + 1) != 0) { + pthread_spin_unlock(&spin_lock_); + return ""; + } + std::copy(view.data(), view.data() + view.size(), p); + views_.emplace_back(p); + pthread_spin_unlock(&spin_lock_); + return p; + } + + // only use in UT + void Clean() + { + pthread_spin_lock(&spin_lock_); + for (auto &p : views_) { + delete[] p; + } + views_.clear(); + pthread_spin_unlock(&spin_lock_); + } +private: + StringViewHold() + { + pthread_spin_init(&spin_lock_, PTHREAD_PROCESS_PRIVATE); + } + ~StringViewHold() + { + Clean(); + pthread_spin_destroy(&spin_lock_); + } + + std::vector views_; + pthread_spinlock_t spin_lock_; +}; +} // namespace HiviewDFX +} // namespace OHOS +#endif \ No newline at end of file