xref: /dragonfly/sys/sys/_malloc.h (revision d8082429)
1 /*
2  * Copyright (c) 2019 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
20  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef _SYS__MALLOC_H_
31 #define	_SYS__MALLOC_H_
32 
33 /*
34  * Do not include this header outside _KERNEL or _KERNEL_STRUCTURES scopes.
35  * Used in <sys/user.h>.
36  */
37 
38 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
39 #include <sys/cdefs.h>		/* for __cache_align */
40 #include <machine/stdint.h>	/* for __* types */
41 #include <machine/param.h>	/* for SMP_MAXCPU */
42 
43 /*
44  * The malloc tracking structure.  Note that per-cpu entries must be
45  * aggregated for accurate statistics, they do not actually break the
46  * stats down by cpu (e.g. the cpu freeing memory will subtract from
47  * its slot, not the originating cpu's slot).
48  *
49  * SMP_MAXCPU is used so modules which use malloc remain compatible
50  * between UP and SMP.
51  *
52  * Warning: __cachealign typically represents 64 byte alignment, so
53  * this structure may be larger than expected.
54  */
55 struct kmalloc_use {
56 	__size_t	memuse;
57 	__size_t	inuse;
58 	__int64_t	calls;	/* total packets of this type ever allocated */
59 
60 	/*
61 	 * This value will be added to ks_loosememuse and resetted,
62 	 * once it goes above certain threshold (ZoneSize).  This
63 	 * is intended to reduce frequency of ks_loosememuse (global)
64 	 * updates.
65 	 */
66 	__size_t	loosememuse;
67 } __cachealign;
68 
69 struct malloc_type {
70 	struct malloc_type *ks_next;	/* next in list */
71 	__size_t	ks_loosememuse;	/* (inaccurate) aggregate memuse */
72 	__size_t	ks_limit;	/* most that are allowed to exist */
73 	__uint64_t	ks_mtflags;	/* MTF_x flags */
74 
75 	__uint32_t	ks_unused1;
76 	__uint32_t	ks_magic;	/* if it's not magic, don't touch it */
77 	const char	*ks_shortdesc;	/* short description */
78 	__uint64_t	ks_unused2;
79 	struct kmalloc_use *ks_use;
80 	struct kmalloc_use ks_use0;	/* dummy prior to SMP startup */
81 };
82 
83 typedef	struct malloc_type	*malloc_type_t;
84 
85 #define	MALLOC_DECLARE(type)		\
86 	extern struct malloc_type type[1]	/* ref as ptr */
87 
88 #endif
89 
90 #endif /* !_SYS__MALLOC_H_ */
91