1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
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 "util.h"
17 #if defined __APPLE__
18 #include "malloc/malloc.h"
19 #else
20 #include "malloc.h"
21 #endif
22
23 #include <err.h>
24 #include <cmath>
25 #include <sched.h>
26 #include <cstdio>
27 #include <cwchar>
28 #include <cstdlib>
29
open_tcache()30 void open_tcache() {
31 #if not defined __APPLE__
32 mallopt(M_OHOS_CONFIG, M_TCACHE_PERFORMANCE_MODE);
33 mallopt(M_OHOS_CONFIG, M_ENABLE_OPT_TCACHE);
34 mallopt(M_SET_THREAD_CACHE, M_THREAD_CACHE_ENABLE);
35 mallopt(M_DELAYED_FREE, M_DELAYED_FREE_ENABLE);
36 #endif
37 }
AlignUpMemoy(char * origPtr,size_t alignment)38 char* AlignUpMemoy(char* origPtr, size_t alignment)
39 {
40 if ((alignment & (alignment - 1)) != 0) {
41 perror("alignment is not a power of two.");
42 }
43
44 uintptr_t ptr = reinterpret_cast<uintptr_t>(origPtr);
45 if (alignment > 0) {
46 ptr = ((ptr + (alignment - 1)) & (~(alignment - 1)));
47 }
48
49 return reinterpret_cast<char*>(ptr);
50 }
51
GetAlignedPtr(std::vector<char> * buf,size_t alignment,size_t nbytes)52 char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes)
53 {
54 buf->resize(nbytes + 2 * alignment);
55 return AlignUpMemoy(buf->data(), alignment);
56 }
57
GetAlignedPtr(std::vector<wchar_t> * buf,size_t alignment,size_t nchars)58 wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nchars)
59 {
60 buf->resize(nchars + ceil((2 * alignment) / sizeof(wchar_t)));
61 return reinterpret_cast<wchar_t*>(AlignUpMemoy(reinterpret_cast<char*>(buf->data()), alignment));
62 }
63
GetAlignedPtrFilled(std::vector<char> * buf,size_t alignment,size_t nbytes,char fillByte)64 char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fillByte)
65 {
66 char* bufAligned = GetAlignedPtr(buf, alignment, nbytes);
67 memset(bufAligned, fillByte, nbytes);
68 return bufAligned;
69 }
70
GetAlignedPtrFilled(std::vector<wchar_t> * buf,size_t alignment,size_t nbytes,wchar_t fillByte)71 wchar_t* GetAlignedPtrFilled(std::vector<wchar_t>* buf, size_t alignment, size_t nbytes, wchar_t fillByte)
72 {
73 wchar_t* bufAligned = GetAlignedPtr(buf, alignment, nbytes);
74 wmemset(bufAligned, fillByte, nbytes);
75 return bufAligned;
76 }
77
ReadJsonFile(const char * fileName)78 char* ReadJsonFile(const char *fileName)
79 {
80 FILE *jsonFile = nullptr;
81 char *contentBuffer = nullptr;
82 long jsonFileLength = 0;
83 size_t readFileContent = 0;
84
85 jsonFile = fopen(fileName, "rb");
86 if (jsonFile == nullptr) {
87 return nullptr;
88 }
89
90 /* get the length */
91 if (fseek(jsonFile, 0, SEEK_END) != 0) {
92 fclose(jsonFile);
93 return nullptr;
94 }
95 jsonFileLength = ftell(jsonFile);
96 if (jsonFileLength < 0) {
97 fclose(jsonFile);
98 return nullptr;
99 }
100 if (fseek(jsonFile, 0, SEEK_SET) != 0) {
101 fclose(jsonFile);
102 return nullptr;
103 }
104
105 contentBuffer = (char*)malloc((size_t)jsonFileLength + sizeof(""));
106 if (contentBuffer == nullptr) {
107 fclose(jsonFile);
108 return nullptr;
109 }
110
111 /* read the json file into memory */
112 readFileContent = fread(contentBuffer, sizeof(char), (size_t)jsonFileLength, jsonFile);
113 if ((long)readFileContent != jsonFileLength) {
114 free(contentBuffer);
115 contentBuffer = nullptr;
116 fclose(jsonFile);
117 return contentBuffer;
118 }
119 contentBuffer[readFileContent] = '\0';
120
121 return contentBuffer;
122 }
123