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_MEM_ACCT_H
25 #define _KERN_MEM_ACCT_H
26
27 #ifdef XNU_KERNEL_PRIVATE
28
29 #include <sys/mem_acct_private.h>
30
31 struct mem_acct;
32 /*
33 * Add "size" to the memory accounting module of "type".
34 */
35 __private_extern__ void _mem_acct_add(struct mem_acct *macct, int size);
36 __private_extern__ struct mem_acct *mem_acct_register(
37 const char *__null_terminated name, uint64_t hardlimit, uint8_t percent);
38
39 /*
40 * pre-softlimit means we are getting close to the softlimit (about 80% of it).
41 * The subsystem should start taking preventive actions.
42 */
43 #define MEMACCT_PRESOFTLIMIT 1
44 /*
45 * We are at the softlimit. Take actions to reduce memory usage, but don't take
46 * fully destructive actions yet.
47 */
48 #define MEMACCT_SOFTLIMIT 2
49 /*
50 * We are above the hardlimit. Prevent holding on to memory in this subsystem.
51 */
52 #define MEMACCT_HARDLIMIT 3
53
54 extern int mem_acct_limited(const struct mem_acct *macct);
55
56 static inline void
mem_acct_add(struct mem_acct * macct,unsigned int size)57 mem_acct_add(struct mem_acct *macct, unsigned int size)
58 {
59 _mem_acct_add(macct, size);
60 }
61
62 static inline void
mem_acct_sub(struct mem_acct * macct,unsigned int size)63 mem_acct_sub(struct mem_acct *macct, unsigned int size)
64 {
65 _mem_acct_add(macct, -size);
66 }
67
68
69 #endif /* XNU_KERNEL_PRIVATE */
70
71 #endif /*_KERN_MEM_ACCT_H */
72