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 #include <fcntl.h>
21 #include <sys/syscall.h>
22 #include <unistd.h>
23
24 #define API_ATTRIBUTE(attr) __attribute__(attr)
25 #define unlikely(x) __builtin_expect(!!(x), 0)
26 #define likely(x) __builtin_expect(!!(x), 1)
27
GetPid(void)28 static inline unsigned int GetPid(void)
29 {
30 return getpid();
31 }
GetTid(void)32 static inline unsigned int GetTid(void)
33 {
34 return syscall(SYS_gettid);
35 }
GetEnv(const char * name)36 static inline std::string GetEnv(const char* name)
37 {
38 char* val = std::getenv(name);
39 if (val == nullptr) {
40 return "";
41 }
42 return val;
43 }
44
GetProcessName(char * processName,int bufferLength)45 static inline void GetProcessName(char* processName, int bufferLength)
46 {
47 int fd = open("/proc/self/cmdline", O_RDONLY);
48 if (fd != -1) {
49 ssize_t ret = syscall(SYS_read, fd, processName, bufferLength - 1);
50 if (ret != -1) {
51 processName[ret] = 0;
52 }
53
54 syscall(SYS_close, fd);
55 }
56 }
57 #endif