1 /*
2  * Copyright (C) 2021 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 SYS_UTIL_H
17 #define SYS_UTIL_H
18 
19 #include "nstackx_common_header.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #if defined(MIPS)
26 #include <stdatomic.h>
27 #endif
28 
29 #define PATH_SEPARATOR '/'
30 #define INVALID_TID (pthread_t)(-1)
31 
32 typedef uint64_t atomic_t;
33 
34 #define NSTACKX_ATOM_FETCH(ptr) (*ptr)
35 #define NSTACKX_ATOM_SET(ptr, i) ((*ptr) = (i))
36 #if defined(MIPS)
37 #define NSTACKX_ATOM_FETCH_INC(ptr) __atomic_fetch_add((ptr), 1, __ATOMIC_SEQ_CST)
38 #define NSTACKX_ATOM_FETCH_DEC(ptr) __atomic_fetch_sub((ptr), 1, __ATOMIC_SEQ_CST)
39 #define NSTACKX_ATOM_ADD_RETURN(ptr, i) __atomic_add_fetch((ptr), i, __ATOMIC_SEQ_CST)
40 #define NSTACKX_ATOM_FETCH_ADD(ptr, val) __atomic_fetch_add((ptr), (val), __ATOMIC_SEQ_CST)
41 #define NSTACKX_ATOM_FETCH_SUB(ptr, val) __atomic_fetch_sub((ptr), (val), __ATOMIC_SEQ_CST)
42 #else
43 #define NSTACKX_ATOM_FETCH_INC(ptr) __sync_fetch_and_add((ptr), 1)
44 #define NSTACKX_ATOM_FETCH_DEC(ptr) __sync_fetch_and_sub((ptr), 1)
45 #define NSTACKX_ATOM_ADD_RETURN(ptr, i) __sync_add_and_fetch((ptr), i)
46 #define NSTACKX_ATOM_FETCH_ADD(ptr, val) __sync_fetch_and_add((ptr), (val))
47 #define NSTACKX_ATOM_FETCH_SUB(ptr, val) __sync_fetch_and_sub((ptr), (val))
48 #endif
49 
GetErrno(void)50 static inline int32_t GetErrno(void)
51 {
52     return errno;
53 }
54 
55 #define CloseSocketInner CloseDesc
56 #define gettid() (pid_t)syscall(__NR_gettid)
57 NSTACKX_EXPORT void CloseDesc(int32_t desc);
58 NSTACKX_EXPORT int32_t GetInterfaceList(struct ifconf *ifc, struct ifreq *buf, uint32_t size);
59 NSTACKX_EXPORT int32_t GetInterfaceIP(int32_t fd, struct ifreq *interface);
60 NSTACKX_EXPORT int32_t GetTargetInterface(const struct sockaddr_in *dstAddr, struct ifreq *localDev);
61 
62 #ifdef __cplusplus
63 }
64 #endif
65 
66 #endif // NSTACKX_UTIL_H
67