xref: /freebsd/include/malloc_np.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2006 Jason Evans <jasone@FreeBSD.org>.
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(s), this list of conditions and the following disclaimer as
12  *    the first lines of this file unmodified other than the possible
13  *    addition of one or more copyright notices.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice(s), this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
28  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  * $FreeBSD$
32  */
33 
34 #ifndef _MALLOC_NP_H_
35 #define	_MALLOC_NP_H_
36 
37 #include <sys/types.h>
38 
39 #ifdef __cplusplus
40 #define	__MyBool	bool
41 #else
42 #define	__MyBool	_Bool
43 #endif
44 
45 __BEGIN_DECLS
46 typedef struct extent_hooks_s extent_hooks_t;
47 typedef void *(extent_alloc_t)(extent_hooks_t *, void *, size_t, size_t,
48     __MyBool *, __MyBool *, unsigned);
49 typedef __MyBool (extent_dalloc_t)(extent_hooks_t *, void *, size_t, __MyBool,
50     unsigned);
51 typedef void (extent_destroy_t)(extent_hooks_t *, void *, size_t, __MyBool,
52     unsigned);
53 typedef __MyBool (extent_commit_t)(extent_hooks_t *, void *, size_t, size_t,
54     size_t, unsigned);
55 typedef __MyBool (extent_decommit_t)(extent_hooks_t *, void *, size_t, size_t,
56     size_t, unsigned);
57 typedef __MyBool (extent_purge_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
58     unsigned);
59 typedef __MyBool (extent_split_t)(extent_hooks_t *, void *, size_t, size_t, size_t,
60     __MyBool, unsigned);
61 typedef __MyBool (extent_merge_t)(extent_hooks_t *, void *, size_t, void *, size_t,
62     __MyBool, unsigned);
63 struct extent_hooks_s {
64 	extent_alloc_t		*alloc;
65 	extent_dalloc_t		*dalloc;
66 	extent_destroy_t	*destroy;
67 	extent_commit_t		*commit;
68 	extent_decommit_t	*decommit;
69 	extent_purge_t		*purge_lazy;
70 	extent_purge_t		*purge_forced;
71 	extent_split_t		*split;
72 	extent_merge_t		*merge;
73 };
74 
75 size_t	malloc_usable_size(const void *ptr);
76 
77 void	malloc_stats_print(void (*write_cb)(void *, const char *),
78     void *cbopaque, const char *opts);
79 
80 #define	MALLCTL_ARENAS_ALL	4096
81 #define	MALLCTL_ARENAS_DESTROYED	4097
82 
83 int	mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
84     size_t newlen);
85 int	mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp);
86 int	mallctlbymib(const size_t *mib, size_t miblen, void *oldp,
87     size_t *oldlenp, void *newp, size_t newlen);
88 
89 #define	MALLOCX_LG_ALIGN(la)	((int)(la))
90 #define	MALLOCX_ALIGN(a)	((int)(ffsl((int)(a))-1))
91 #define	MALLOCX_ZERO		((int)0x40)
92 #define	MALLOCX_TCACHE(tc)	((int)(((tc)+2) << 8))
93 #define	MALLOCX_TCACHE_NONE	MALLOCX_TCACHE(-1)
94 #define	MALLOCX_ARENA(a)	((((int)(a))+1) << 20)
95 
96 void	*mallocx(size_t size, int flags);
97 void	*rallocx(void *ptr, size_t size, int flags);
98 size_t	xallocx(void *ptr, size_t size, size_t extra, int flags);
99 size_t	sallocx(const void *ptr, int flags);
100 void	dallocx(void *ptr, int flags);
101 void	sdallocx(void *ptr, size_t size, int flags);
102 size_t	nallocx(size_t size, int flags);
103 
104 void *	__calloc(size_t number, size_t size) __malloc_like;
105 void *	__malloc(size_t size) __malloc_like;
106 void *	__realloc(void *ptr, size_t size);
107 void	__free(void *ptr);
108 int	__posix_memalign(void **ptr, size_t alignment, size_t size);
109 void	*__aligned_alloc(size_t alignment, size_t size);
110 size_t	__malloc_usable_size(const void *ptr);
111 void	__malloc_stats_print(void (*write_cb)(void *, const char *),
112     void *cbopaque, const char *opts);
113 int	__mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
114     size_t newlen);
115 int	__mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp);
116 int	__mallctlbymib(const size_t *mib, size_t miblen, void *oldp,
117     size_t *oldlenp, void *newp, size_t newlen);
118 void	*__mallocx(size_t size, int flags);
119 void	*__rallocx(void *ptr, size_t size, int flags);
120 size_t	__xallocx(void *ptr, size_t size, size_t extra, int flags);
121 size_t	__sallocx(const void *ptr, int flags);
122 void	__dallocx(void *ptr, int flags);
123 void	__sdallocx(void *ptr, size_t size, int flags);
124 size_t	__nallocx(size_t size, int flags);
125 
126 void	*memalign(size_t, size_t) __malloc_like __alloc_align(1)
127 	    __alloc_size(2);
128 __END_DECLS
129 
130 #undef __MyBool
131 
132 #endif /* _MALLOC_NP_H_ */
133