• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <cstdlib>
17 #include "hilog_realloc.h"
18 #include "securec.h"
19 #include "hilog_vector.h"
20 
21 namespace OHOS {
22 namespace ACELite {
23 static const size_t ORG_VECTOR_SIZE = 2;
24 
HilogVector()25 HilogVector::HilogVector() : size(0), maxSize(0), str(nullptr), type(nullptr) {}
26 
~HilogVector()27 HilogVector::~HilogVector()
28 {
29     if (str != nullptr) {
30         for (size_t i = 0; i < size; i++) {
31             char *tmp = str[i];
32             if (tmp != nullptr) {
33                 free(tmp);
34             }
35         }
36         free(str);
37         str = nullptr;
38     }
39 
40     if (type != nullptr) {
41         free(type);
42         type = nullptr;
43     }
44 }
45 
Size(const HilogVector * vector)46 size_t HilogVector::Size(const HilogVector *vector)
47 {
48     if (vector == nullptr) {
49         return 0;
50     }
51     return vector->size;
52 }
53 
GetStr(const HilogVector * vector,size_t pos)54 char* HilogVector::GetStr(const HilogVector *vector, size_t pos)
55 {
56     if (vector != nullptr && vector->str != nullptr && pos < vector->size) {
57         return vector->str[pos];
58     }
59     return nullptr;
60 }
61 
GetType(const HilogVector * vector,size_t pos)62 ValueType HilogVector::GetType(const HilogVector *vector, size_t pos)
63 {
64     if (vector != nullptr && vector->type != nullptr && pos < vector->size) {
65         return vector->type[pos];
66     }
67     return NONE_TYPE;
68 }
69 
Push(HilogVector * vector,const char * element,ValueType type)70 void HilogVector::Push(HilogVector *vector, const char *element, ValueType type)
71 {
72     if (vector == nullptr || element == nullptr) {
73         return;
74     }
75     if (vector->size == vector->maxSize) {
76         vector->maxSize == 0 ? vector->maxSize = ORG_VECTOR_SIZE : vector->maxSize = vector->maxSize << 1;
77         char **tempStr = static_cast<char**>(
78             HilogRealloc::Realloc(vector->str, sizeof(char*) * vector->maxSize, sizeof(char*) * vector->size));
79         ValueType *tempType = static_cast<ValueType*>(
80             HilogRealloc::Realloc(
81                 vector->type, sizeof(ValueType) * vector->maxSize, sizeof(ValueType) * vector->size));
82         if (tempStr != nullptr && tempType != nullptr) {
83             vector->str = tempStr;
84             vector->type = tempType;
85         } else {
86             return;
87         }
88     }
89     if (vector->str != nullptr && vector->type != nullptr) {
90         size_t length = sizeof(char) * (strlen(element) + 1);
91         char *tempStr = static_cast<char*>(malloc(length));
92         if (tempStr == nullptr) {
93             return;
94         }
95         vector->str[vector->size] = tempStr;
96         memset_s(vector->str[vector->size], length, 0, length);
97         memcpy_s(vector->str[vector->size], length, element, sizeof(char) * strlen(element));
98         vector->type[vector->size] = type;
99         vector->size++;
100     }
101 }
102 } // namespace ACELite
103 } // namespace OHOS