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