1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci#ifndef STRING_VIEW_UTIL_H
16800b99b8Sopenharmony_ci#define STRING_VIEW_UTIL_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <cstdio>
19800b99b8Sopenharmony_ci#include <string>
20800b99b8Sopenharmony_ci#include <securec.h>
21800b99b8Sopenharmony_ci#include <vector>
22800b99b8Sopenharmony_ci#include <pthread.h>
23800b99b8Sopenharmony_ci#include "cpp_define.h"
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_cinamespace OHOS {
26800b99b8Sopenharmony_cinamespace HiviewDFX {
27800b99b8Sopenharmony_ciclass StringViewHold {
28800b99b8Sopenharmony_cipublic:
29800b99b8Sopenharmony_ci    static StringViewHold &Get()
30800b99b8Sopenharmony_ci    {
31800b99b8Sopenharmony_ci        static StringViewHold instance;
32800b99b8Sopenharmony_ci        return instance;
33800b99b8Sopenharmony_ci    }
34800b99b8Sopenharmony_ci
35800b99b8Sopenharmony_ci    const char* Hold(STRING_VIEW view)
36800b99b8Sopenharmony_ci    {
37800b99b8Sopenharmony_ci        pthread_spin_lock(&spin_lock_);
38800b99b8Sopenharmony_ci        if (view.size() == 0) {
39800b99b8Sopenharmony_ci            pthread_spin_unlock(&spin_lock_);
40800b99b8Sopenharmony_ci            return "";
41800b99b8Sopenharmony_ci        }
42800b99b8Sopenharmony_ci
43800b99b8Sopenharmony_ci        char *p = new (std::nothrow) char[view.size() + 1];
44800b99b8Sopenharmony_ci        if (p == nullptr) {
45800b99b8Sopenharmony_ci            pthread_spin_unlock(&spin_lock_);
46800b99b8Sopenharmony_ci            return "";
47800b99b8Sopenharmony_ci        }
48800b99b8Sopenharmony_ci        if (memset_s(p, view.size() + 1, '\0', view.size() + 1) != 0) {
49800b99b8Sopenharmony_ci            pthread_spin_unlock(&spin_lock_);
50800b99b8Sopenharmony_ci            return "";
51800b99b8Sopenharmony_ci        }
52800b99b8Sopenharmony_ci        std::copy(view.data(), view.data() + view.size(), p);
53800b99b8Sopenharmony_ci        views_.emplace_back(p);
54800b99b8Sopenharmony_ci        pthread_spin_unlock(&spin_lock_);
55800b99b8Sopenharmony_ci        return p;
56800b99b8Sopenharmony_ci    }
57800b99b8Sopenharmony_ci
58800b99b8Sopenharmony_ci    // only use in UT
59800b99b8Sopenharmony_ci    void Clean()
60800b99b8Sopenharmony_ci    {
61800b99b8Sopenharmony_ci        pthread_spin_lock(&spin_lock_);
62800b99b8Sopenharmony_ci        for (auto &p : views_) {
63800b99b8Sopenharmony_ci            delete[] p;
64800b99b8Sopenharmony_ci        }
65800b99b8Sopenharmony_ci        views_.clear();
66800b99b8Sopenharmony_ci        pthread_spin_unlock(&spin_lock_);
67800b99b8Sopenharmony_ci    }
68800b99b8Sopenharmony_ciprivate:
69800b99b8Sopenharmony_ci    StringViewHold()
70800b99b8Sopenharmony_ci    {
71800b99b8Sopenharmony_ci        pthread_spin_init(&spin_lock_, PTHREAD_PROCESS_PRIVATE);
72800b99b8Sopenharmony_ci    }
73800b99b8Sopenharmony_ci    ~StringViewHold()
74800b99b8Sopenharmony_ci    {
75800b99b8Sopenharmony_ci        Clean();
76800b99b8Sopenharmony_ci        pthread_spin_destroy(&spin_lock_);
77800b99b8Sopenharmony_ci    }
78800b99b8Sopenharmony_ci
79800b99b8Sopenharmony_ci    std::vector<char *> views_;
80800b99b8Sopenharmony_ci    pthread_spinlock_t spin_lock_;
81800b99b8Sopenharmony_ci};
82800b99b8Sopenharmony_ci} // namespace HiviewDFX
83800b99b8Sopenharmony_ci} // namespace OHOS
84800b99b8Sopenharmony_ci#endif