xref: /xnu-12377.81.4/bsd/kern/uipc_domain.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. Please obtain a copy of the License at
10  * http://www.opensource.apple.com/apsl/ and read it before using this
11  * file.
12  *
13  * The Original Code and all software distributed under the License are
14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18  * Please see the License for the specific language governing rights and
19  * limitations under the License.
20  *
21  * @APPLE_LICENSE_HEADER_END@
22  */
23 
24 #ifndef _KERN_UIPC_DOMAIN_H
25 #define _KERN_UIPC_DOMAIN_H
26 
27 #ifdef XNU_KERNEL_PRIVATE
28 
29 
30 #include <kern/mem_acct.h>
31 #include <kern/uipc_socket.h>
32 
33 #include <sys/protosw.h>
34 
35 static inline void
proto_memacct_add(struct protosw * proto,unsigned int size)36 proto_memacct_add(struct protosw *proto, unsigned int size)
37 {
38 	if (proto->pr_mem_acct) {
39 		mem_acct_add(proto->pr_mem_acct, size);
40 	} else {
41 		socket_memacct_add(size);
42 	}
43 }
44 
45 static inline void
proto_memacct_sub(struct protosw * proto,unsigned int size)46 proto_memacct_sub(struct protosw *proto, unsigned int size)
47 {
48 	if (proto->pr_mem_acct) {
49 		mem_acct_sub(proto->pr_mem_acct, size);
50 	} else {
51 		socket_memacct_sub(size);
52 	}
53 }
54 
55 static inline bool
proto_memacct_hardlimit(const struct protosw * proto)56 proto_memacct_hardlimit(const struct protosw *proto)
57 {
58 	if (proto->pr_mem_acct) {
59 		return mem_acct_limited(proto->pr_mem_acct) == MEMACCT_HARDLIMIT;
60 	} else {
61 		return socket_memacct_hardlimit();
62 	}
63 }
64 
65 static inline bool
proto_memacct_limited(const struct protosw * proto)66 proto_memacct_limited(const struct protosw *proto)
67 {
68 	if (proto->pr_mem_acct) {
69 		return mem_acct_limited(proto->pr_mem_acct) != 0;
70 	} else {
71 		return socket_memacct_limited();
72 	}
73 }
74 
75 extern uint64_t _net_uptime;
76 extern uint64_t _net_uptime_ms;
77 extern uint64_t _net_uptime_us;
78 
79 extern void net_update_uptime(void);
80 extern void net_update_uptime_with_time(const struct timeval *);
81 
82 /*
83  * ToDo - we could even replace all callers of net_uptime* by a direct access
84  * to _net_uptime*
85  */
86 static inline uint64_t
net_uptime(void)87 net_uptime(void)
88 {
89 	return _net_uptime;
90 }
91 static inline uint64_t
net_uptime_ms(void)92 net_uptime_ms(void)
93 {
94 	return _net_uptime_ms;
95 }
96 static inline uint64_t
net_uptime_us(void)97 net_uptime_us(void)
98 {
99 	return _net_uptime_us;
100 }
101 
102 extern void net_uptime2timeval(struct timeval *);
103 
104 #endif /* XNU_KERNEL_PRIVATE */
105 
106 #endif /*_KERN_UIPC_DOMAIN_H */
107