1 /*
2 * Copyright (c) 2023 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 #ifndef __OSAL_HPP__
17 #define __OSAL_HPP__
18
19 #include <string>
20
21 #ifdef _MSC_VER
22 #define API_ATTRIBUTE(attr)
23 #define unlikely(x) (x)
24 #define likely(x) (x)
25 #else
26 #define API_ATTRIBUTE(attr) __attribute__(attr)
27 #define unlikely(x) __builtin_expect(!!(x), 0)
28 #define likely(x) __builtin_expect(!!(x), 1)
29 #endif
30
31 #ifdef __linux__
32 #include <sys/syscall.h>
33 #include <unistd.h>
GetPid(void)34 static inline unsigned int GetPid(void)
35 {
36 return getpid();
37 }
GetTid(void)38 static inline unsigned int GetTid(void)
39 {
40 return syscall(SYS_gettid);
41 }
GetEnv(const char * name)42 static inline std::string GetEnv(const char* name)
43 {
44 char* val = std::getenv(name);
45 if (val == nullptr) {
46 return "";
47 }
48 return val;
49 }
50
51 #elif defined(_MSC_VER)
52 #include <windows.h>
53
GetPid(void)54 static inline unsigned int GetPid(void)
55 {
56 return GetCurrentProcessId();
57 }
GetTid(void)58 static inline unsigned int GetTid(void)
59 {
60 return GetCurrentThreadId();
61 }
GetEnv(const char * name)62 static inline std::string GetEnv(const char* name)
63 {
64 char* r = nullptr;
65 size_t len = 0;
66
67 if (_dupenv_s(&r, &len, name) == 0 && r != nullptr) {
68 std::string val(r, len);
69 std::free(r);
70 return val;
71 }
72 return "";
73 }
74 #endif
75 #endif