]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/malloc.h
Merge libc++ trunk r321017 to contrib/libc++.
[FreeBSD/FreeBSD.git] / sys / sys / malloc.h
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1987, 1993
5  *      The Regents of the University of California.
6  * Copyright (c) 2005, 2009 Robert N. M. Watson
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)malloc.h    8.5 (Berkeley) 5/3/95
34  * $FreeBSD$
35  */
36
37 #ifndef _SYS_MALLOC_H_
38 #define _SYS_MALLOC_H_
39
40 #include <sys/param.h>
41 #include <sys/queue.h>
42 #include <sys/_lock.h>
43 #include <sys/_mutex.h>
44
45 #define MINALLOCSIZE    UMA_SMALLEST_UNIT
46
47 /*
48  * flags to malloc.
49  */
50 #define M_NOWAIT        0x0001          /* do not block */
51 #define M_WAITOK        0x0002          /* ok to block */
52 #define M_ZERO          0x0100          /* bzero the allocation */
53 #define M_NOVM          0x0200          /* don't ask VM for pages */
54 #define M_USE_RESERVE   0x0400          /* can alloc out of reserve memory */
55 #define M_NODUMP        0x0800          /* don't dump pages in this allocation */
56 #define M_FIRSTFIT      0x1000          /* Only for vmem, fast fit. */
57 #define M_BESTFIT       0x2000          /* Only for vmem, low fragmentation. */
58
59 #define M_MAGIC         877983977       /* time when first defined :-) */
60
61 /*
62  * Two malloc type structures are present: malloc_type, which is used by a
63  * type owner to declare the type, and malloc_type_internal, which holds
64  * malloc-owned statistics and other ABI-sensitive fields, such as the set of
65  * malloc statistics indexed by the compile-time MAXCPU constant.
66  * Applications should avoid introducing dependence on the allocator private
67  * data layout and size.
68  *
69  * The malloc_type ks_next field is protected by malloc_mtx.  Other fields in
70  * malloc_type are static after initialization so unsynchronized.
71  *
72  * Statistics in malloc_type_stats are written only when holding a critical
73  * section and running on the CPU associated with the index into the stat
74  * array, but read lock-free resulting in possible (minor) races, which the
75  * monitoring app should take into account.
76  */
77 struct malloc_type_stats {
78         uint64_t        mts_memalloced; /* Bytes allocated on CPU. */
79         uint64_t        mts_memfreed;   /* Bytes freed on CPU. */
80         uint64_t        mts_numallocs;  /* Number of allocates on CPU. */
81         uint64_t        mts_numfrees;   /* number of frees on CPU. */
82         uint64_t        mts_size;       /* Bitmask of sizes allocated on CPU. */
83         uint64_t        _mts_reserved1; /* Reserved field. */
84         uint64_t        _mts_reserved2; /* Reserved field. */
85         uint64_t        _mts_reserved3; /* Reserved field. */
86 };
87
88 /*
89  * Index definitions for the mti_probes[] array.
90  */
91 #define DTMALLOC_PROBE_MALLOC           0
92 #define DTMALLOC_PROBE_FREE             1
93 #define DTMALLOC_PROBE_MAX              2
94
95 struct malloc_type_internal {
96         uint32_t        mti_probes[DTMALLOC_PROBE_MAX];
97                                         /* DTrace probe ID array. */
98         u_char          mti_zone;
99         struct malloc_type_stats        mti_stats[MAXCPU];
100 };
101
102 /*
103  * Public data structure describing a malloc type.  Private data is hung off
104  * of ks_handle to avoid encoding internal malloc(9) data structures in
105  * modules, which will statically allocate struct malloc_type.
106  */
107 struct malloc_type {
108         struct malloc_type *ks_next;    /* Next in global chain. */
109         u_long           ks_magic;      /* Detect programmer error. */
110         const char      *ks_shortdesc;  /* Printable type name. */
111         void            *ks_handle;     /* Priv. data, was lo_class. */
112 };
113
114 /*
115  * Statistics structure headers for user space.  The kern.malloc sysctl
116  * exposes a structure stream consisting of a stream header, then a series of
117  * malloc type headers and statistics structures (quantity maxcpus).  For
118  * convenience, the kernel will provide the current value of maxcpus at the
119  * head of the stream.
120  */
121 #define MALLOC_TYPE_STREAM_VERSION      0x00000001
122 struct malloc_type_stream_header {
123         uint32_t        mtsh_version;   /* Stream format version. */
124         uint32_t        mtsh_maxcpus;   /* Value of MAXCPU for stream. */
125         uint32_t        mtsh_count;     /* Number of records. */
126         uint32_t        _mtsh_pad;      /* Pad/reserved field. */
127 };
128
129 #define MALLOC_MAX_NAME 32
130 struct malloc_type_header {
131         char                            mth_name[MALLOC_MAX_NAME];
132 };
133
134 #ifdef _KERNEL
135 #define MALLOC_DEFINE(type, shortdesc, longdesc)                        \
136         struct malloc_type type[1] = {                                  \
137                 { NULL, M_MAGIC, shortdesc, NULL }                      \
138         };                                                              \
139         SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_THIRD, malloc_init,  \
140             type);                                                      \
141         SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY,             \
142             malloc_uninit, type)
143
144 #define MALLOC_DECLARE(type) \
145         extern struct malloc_type type[1]
146
147 MALLOC_DECLARE(M_CACHE);
148 MALLOC_DECLARE(M_DEVBUF);
149 MALLOC_DECLARE(M_TEMP);
150
151 /*
152  * Deprecated macro versions of not-quite-malloc() and free().
153  */
154 #define MALLOC(space, cast, size, type, flags) \
155         ((space) = (cast)malloc((u_long)(size), (type), (flags)))
156 #define FREE(addr, type) free((addr), (type))
157
158 /*
159  * XXX this should be declared in <sys/uio.h>, but that tends to fail
160  * because <sys/uio.h> is included in a header before the source file
161  * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
162  */
163 MALLOC_DECLARE(M_IOV);
164
165 extern struct mtx malloc_mtx;
166
167 /*
168  * Function type used when iterating over the list of malloc types.
169  */
170 typedef void malloc_type_list_func_t(struct malloc_type *, void *);
171
172 void    contigfree(void *addr, unsigned long size, struct malloc_type *type);
173 void    *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
174             vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
175             vm_paddr_t boundary) __malloc_like __result_use_check
176             __alloc_size(1) __alloc_align(6);
177 void    free(void *addr, struct malloc_type *type);
178 void    *malloc(unsigned long size, struct malloc_type *type, int flags)
179             __malloc_like __result_use_check __alloc_size(1);
180 void    malloc_init(void *);
181 int     malloc_last_fail(void);
182 void    malloc_type_allocated(struct malloc_type *type, unsigned long size);
183 void    malloc_type_freed(struct malloc_type *type, unsigned long size);
184 void    malloc_type_list(malloc_type_list_func_t *, void *);
185 void    malloc_uninit(void *);
186 void    *realloc(void *addr, unsigned long size, struct malloc_type *type,
187             int flags) __result_use_check __alloc_size(2);
188 void    *reallocf(void *addr, unsigned long size, struct malloc_type *type,
189             int flags) __alloc_size(2);
190
191 struct malloc_type *malloc_desc2type(const char *desc);
192 #endif /* _KERNEL */
193
194 #endif /* !_SYS_MALLOC_H_ */