blob: 73904ca5b47bbb58b8fb6b9f6bbc444fd6d22119 [file] [log] [blame]
James Kuszmaul4cb043c2021-01-17 11:25:51 -08001/*-
2 * Copyright (c) 1987, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2005 Robert N. M. Watson
5 * All rights reserved.
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 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33/* This file has been renamed user_malloc.h for Userspace */
34#ifndef _USER_MALLOC_H_
35#define _USER_MALLOC_H_
36
37/*__Userspace__*/
38#include <stdlib.h>
39#include <sys/types.h>
40#if !defined (__Userspace_os_Windows)
41#include <strings.h>
42#include <stdint.h>
43#else
44#if defined(_MSC_VER) && _MSC_VER >= 1600
45#include <stdint.h>
46#elif defined(SCTP_STDINT_INCLUDE)
47#include SCTP_STDINT_INCLUDE
48#else
49#define uint32_t unsigned __int32
50#define uint64_t unsigned __int64
51#endif
52#include <winsock2.h>
53#endif
54
55#define MINALLOCSIZE UMA_SMALLEST_UNIT
56
57/*
58 * flags to malloc.
59 */
60#define M_NOWAIT 0x0001 /* do not block */
61#define M_WAITOK 0x0002 /* ok to block */
62#define M_ZERO 0x0100 /* bzero the allocation */
63#define M_NOVM 0x0200 /* don't ask VM for pages */
64#define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */
65
66#define M_MAGIC 877983977 /* time when first defined :-) */
67
68/*
69 * Two malloc type structures are present: malloc_type, which is used by a
70 * type owner to declare the type, and malloc_type_internal, which holds
71 * malloc-owned statistics and other ABI-sensitive fields, such as the set of
72 * malloc statistics indexed by the compile-time MAXCPU constant.
73 * Applications should avoid introducing dependence on the allocator private
74 * data layout and size.
75 *
76 * The malloc_type ks_next field is protected by malloc_mtx. Other fields in
77 * malloc_type are static after initialization so unsynchronized.
78 *
79 * Statistics in malloc_type_stats are written only when holding a critical
80 * section and running on the CPU associated with the index into the stat
81 * array, but read lock-free resulting in possible (minor) races, which the
82 * monitoring app should take into account.
83 */
84struct malloc_type_stats {
85 uint64_t mts_memalloced; /* Bytes allocated on CPU. */
86 uint64_t mts_memfreed; /* Bytes freed on CPU. */
87 uint64_t mts_numallocs; /* Number of allocates on CPU. */
88 uint64_t mts_numfrees; /* number of frees on CPU. */
89 uint64_t mts_size; /* Bitmask of sizes allocated on CPU. */
90 uint64_t _mts_reserved1; /* Reserved field. */
91 uint64_t _mts_reserved2; /* Reserved field. */
92 uint64_t _mts_reserved3; /* Reserved field. */
93};
94
95#ifndef MAXCPU /* necessary on Linux */
96#define MAXCPU 4 /* arbitrary? */
97#endif
98
99struct malloc_type_internal {
100 struct malloc_type_stats mti_stats[MAXCPU];
101};
102
103/*
104 * ABI-compatible version of the old 'struct malloc_type', only all stats are
105 * now malloc-managed in malloc-owned memory rather than in caller memory, so
106 * as to avoid ABI issues. The ks_next pointer is reused as a pointer to the
107 * internal data handle.
108 */
109struct malloc_type {
110 struct malloc_type *ks_next; /* Next in global chain. */
111 u_long _ks_memuse; /* No longer used. */
112 u_long _ks_size; /* No longer used. */
113 u_long _ks_inuse; /* No longer used. */
114 uint64_t _ks_calls; /* No longer used. */
115 u_long _ks_maxused; /* No longer used. */
116 u_long ks_magic; /* Detect programmer error. */
117 const char *ks_shortdesc; /* Printable type name. */
118
119 /*
120 * struct malloc_type was terminated with a struct mtx, which is no
121 * longer required. For ABI reasons, continue to flesh out the full
122 * size of the old structure, but reuse the _lo_class field for our
123 * internal data handle.
124 */
125 void *ks_handle; /* Priv. data, was lo_class. */
126 const char *_lo_name;
127 const char *_lo_type;
128 u_int _lo_flags;
129 void *_lo_list_next;
130 struct witness *_lo_witness;
131 uintptr_t _mtx_lock;
132 u_int _mtx_recurse;
133};
134
135/*
136 * Statistics structure headers for user space. The kern.malloc sysctl
137 * exposes a structure stream consisting of a stream header, then a series of
138 * malloc type headers and statistics structures (quantity maxcpus). For
139 * convenience, the kernel will provide the current value of maxcpus at the
140 * head of the stream.
141 */
142#define MALLOC_TYPE_STREAM_VERSION 0x00000001
143struct malloc_type_stream_header {
144 uint32_t mtsh_version; /* Stream format version. */
145 uint32_t mtsh_maxcpus; /* Value of MAXCPU for stream. */
146 uint32_t mtsh_count; /* Number of records. */
147 uint32_t _mtsh_pad; /* Pad/reserved field. */
148};
149
150#define MALLOC_MAX_NAME 32
151struct malloc_type_header {
152 char mth_name[MALLOC_MAX_NAME];
153};
154
155/* __Userspace__
156Notice that at places it uses ifdef _KERNEL. That line cannot be
157removed because it causes conflicts with malloc definition in
158/usr/include/malloc.h, which essentially says that malloc.h has
159been overridden by stdlib.h. We will need to use names like
160user_malloc.h for isolating kernel interface headers. using
161original names like malloc.h in a user_include header can be
162confusing, All userspace header files are being placed in ./user_include
163Better still to remove from user_include.h all irrelevant code such
164as that in the block starting with #ifdef _KERNEL. I am only leaving
165it in for the time being to see what functionality is in this file
166that kernel uses.
167
168Start copy: Copied code for __Userspace__ */
169#define MALLOC_DEFINE(type, shortdesc, longdesc) \
170 struct malloc_type type[1] = { \
171 { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL, \
172 NULL, 0, NULL, NULL, 0, 0 } \
173 }
174
175/* Removed "extern" in __Userspace__ code */
176/* If we need to use MALLOC_DECLARE before using MALLOC then
177 we have to remove extern.
178 In /usr/include/sys/malloc.h there is this definition:
179 #define MALLOC_DECLARE(type) \
180 extern struct malloc_type type[1]
181 and loader is unable to find the extern malloc_type because
182 it may be defined in one of kernel object files.
183 It seems that MALLOC_DECLARE and MALLOC_DEFINE cannot be used at
184 the same time for same "type" variable. Also, in Randall's architecture
185 document, where it specifies O/S specific macros and functions, it says
186 that the name in SCTP_MALLOC does not have to be used.
187*/
188#define MALLOC_DECLARE(type) \
189 extern struct malloc_type type[1]
190
191#define FREE(addr, type) free((addr))
192
193/* changed definitions of MALLOC and FREE */
194/* Using memset if flag M_ZERO is specified. Todo: M_WAITOK and M_NOWAIT */
195#define MALLOC(space, cast, size, type, flags) \
196 ((space) = (cast)malloc((u_long)(size))); \
197 do { \
198 if (flags & M_ZERO) { \
199 memset(space,0,size); \
200 } \
201 } while (0);
202
203
204/* End copy: Copied code for __Userspace__ */
205
206#if 0
207#ifdef _KERNEL
208#define MALLOC_DEFINE(type, shortdesc, longdesc) \
209 struct malloc_type type[1] = { \
210 { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL, \
211 NULL, 0, NULL, NULL, 0, 0 } \
212 }; \
213 SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init, \
214 type); \
215 SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, \
216 malloc_uninit, type)
217
218
219#define MALLOC_DECLARE(type) \
220 extern struct malloc_type type[1]
221
222MALLOC_DECLARE(M_CACHE);
223MALLOC_DECLARE(M_DEVBUF);
224MALLOC_DECLARE(M_TEMP);
225
226MALLOC_DECLARE(M_IP6OPT); /* for INET6 */
227MALLOC_DECLARE(M_IP6NDP); /* for INET6 */
228
229/*
230 * Deprecated macro versions of not-quite-malloc() and free().
231 */
232#define MALLOC(space, cast, size, type, flags) \
233 ((space) = (cast)malloc((u_long)(size), (type), (flags)))
234#define FREE(addr, type) free((addr), (type))
235
236/*
237 * XXX this should be declared in <sys/uio.h>, but that tends to fail
238 * because <sys/uio.h> is included in a header before the source file
239 * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
240 */
241MALLOC_DECLARE(M_IOV);
242
243extern struct mtx malloc_mtx;
244
245/* XXX struct malloc_type is unused for contig*(). */
246void contigfree(void *addr, unsigned long size, struct malloc_type *type);
247void *contigmalloc(unsigned long size, struct malloc_type *type, int flags,
248 vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
249 unsigned long boundary);
250void free(void *addr, struct malloc_type *type);
251void *malloc(unsigned long size, struct malloc_type *type, int flags);
252void malloc_init(void *);
253int malloc_last_fail(void);
254void malloc_type_allocated(struct malloc_type *type, unsigned long size);
255void malloc_type_freed(struct malloc_type *type, unsigned long size);
256void malloc_uninit(void *);
257void *realloc(void *addr, unsigned long size, struct malloc_type *type,
258 int flags);
259void *reallocf(void *addr, unsigned long size, struct malloc_type *type,
260 int flags);
261
262
263#endif /* _KERNEL */
264#endif
265
266#endif /* !_SYS_MALLOC_H_ */