1157236Sjasone/*-
2157236Sjasone * Copyright (C) 2006 Jason Evans <jasone@FreeBSD.org>.
3157236Sjasone * All rights reserved.
4157236Sjasone *
5157236Sjasone * Redistribution and use in source and binary forms, with or without
6157236Sjasone * modification, are permitted provided that the following conditions
7157236Sjasone * are met:
8157236Sjasone * 1. Redistributions of source code must retain the above copyright
9157236Sjasone *    notice(s), this list of conditions and the following disclaimer as
10157236Sjasone *    the first lines of this file unmodified other than the possible
11157236Sjasone *    addition of one or more copyright notices.
12157236Sjasone * 2. Redistributions in binary form must reproduce the above copyright
13157236Sjasone *    notice(s), this list of conditions and the following disclaimer in
14157236Sjasone *    the documentation and/or other materials provided with the
15157236Sjasone *    distribution.
16157236Sjasone *
17157236Sjasone * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18157236Sjasone * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19157236Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20157236Sjasone * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
21157236Sjasone * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22157236Sjasone * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23157236Sjasone * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24157236Sjasone * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25157236Sjasone * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26157236Sjasone * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27157236Sjasone * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28157236Sjasone *
29157236Sjasone * $FreeBSD$
30157236Sjasone */
31157236Sjasone
32157236Sjasone#ifndef _MALLOC_NP_H_
33157236Sjasone#define	_MALLOC_NP_H_
34160056Sjasone#include <sys/cdefs.h>
35160056Sjasone#include <sys/types.h>
36286934Sjasone#include <stdbool.h>
37234370Sjasone#include <strings.h>
38157236Sjasone
39160056Sjasone__BEGIN_DECLS
40286934Sjasonetypedef void *(chunk_alloc_t)(void *, size_t, size_t, bool *, bool *, unsigned);
41286934Sjasonetypedef bool (chunk_dalloc_t)(void *, size_t, bool, unsigned);
42286934Sjasonetypedef bool (chunk_commit_t)(void *, size_t, size_t, size_t, unsigned);
43286934Sjasonetypedef bool (chunk_decommit_t)(void *, size_t, size_t, size_t, unsigned);
44286934Sjasonetypedef bool (chunk_purge_t)(void *, size_t, size_t, size_t, unsigned);
45286934Sjasonetypedef bool (chunk_split_t)(void *, size_t, size_t, size_t, bool, unsigned);
46286934Sjasonetypedef bool (chunk_merge_t)(void *, size_t, void *, size_t, bool, unsigned);
47286866Sjasonetypedef struct {
48286866Sjasone	chunk_alloc_t		*alloc;
49286866Sjasone	chunk_dalloc_t		*dalloc;
50286866Sjasone	chunk_commit_t		*commit;
51286866Sjasone	chunk_decommit_t	*decommit;
52286866Sjasone	chunk_purge_t		*purge;
53286866Sjasone	chunk_split_t		*split;
54286866Sjasone	chunk_merge_t		*merge;
55286866Sjasone} chunk_hooks_t;
56286866Sjasone
57157236Sjasonesize_t	malloc_usable_size(const void *ptr);
58234370Sjasone
59234370Sjasonevoid	malloc_stats_print(void (*write_cb)(void *, const char *),
60234370Sjasone    void *cbopaque, const char *opts);
61234370Sjasoneint	mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
62234370Sjasone    size_t newlen);
63234370Sjasoneint	mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp);
64234370Sjasoneint	mallctlbymib(const size_t *mib, size_t miblen, void *oldp,
65234370Sjasone    size_t *oldlenp, void *newp, size_t newlen);
66234370Sjasone
67261071Sjasone#define	MALLOCX_LG_ALIGN(la)	(la)
68261071Sjasone#define	MALLOCX_ALIGN(a)	(ffsl(a)-1)
69261071Sjasone#define	MALLOCX_ZERO		((int)0x40)
70286866Sjasone#define	MALLOCX_TCACHE(tc)	((int)(((tc)+2) << 8))
71286866Sjasone#define	MALLOCX_TCACHE_NONE	MALLOCX_TCACHE(-1)
72286866Sjasone#define	MALLOCX_ARENA(a)	((int)(((a)+1) << 20))
73261071Sjasone
74261071Sjasonevoid	*mallocx(size_t size, int flags);
75261071Sjasonevoid	*rallocx(void *ptr, size_t size, int flags);
76261071Sjasonesize_t	xallocx(void *ptr, size_t size, size_t extra, int flags);
77261071Sjasonesize_t	sallocx(const void *ptr, int flags);
78261071Sjasonevoid	dallocx(void *ptr, int flags);
79286866Sjasonevoid	sdallocx(void *ptr, size_t size, int flags);
80261071Sjasonesize_t	nallocx(size_t size, int flags);
81261071Sjasone
82286866Sjasonevoid *	__calloc(size_t number, size_t size) __malloc_like;
83286866Sjasonevoid *	__malloc(size_t size) __malloc_like;
84286866Sjasonevoid *	__realloc(void *ptr, size_t size);
85286866Sjasonevoid	__free(void *ptr);
86286866Sjasoneint	__posix_memalign(void **ptr, size_t alignment, size_t size);
87286866Sjasonevoid	*__aligned_alloc(size_t alignment, size_t size);
88286866Sjasonesize_t	__malloc_usable_size(const void *ptr);
89296221Sjasonevoid	__malloc_stats_print(void (*write_cb)(void *, const char *),
90296221Sjasone    void *cbopaque, const char *opts);
91296221Sjasoneint	__mallctl(const char *name, void *oldp, size_t *oldlenp, void *newp,
92296221Sjasone    size_t newlen);
93296221Sjasoneint	__mallctlnametomib(const char *name, size_t *mibp, size_t *miblenp);
94296221Sjasoneint	__mallctlbymib(const size_t *mib, size_t miblen, void *oldp,
95296221Sjasone    size_t *oldlenp, void *newp, size_t newlen);
96261071Sjasonevoid	*__mallocx(size_t size, int flags);
97261071Sjasonevoid	*__rallocx(void *ptr, size_t size, int flags);
98261071Sjasonesize_t	__xallocx(void *ptr, size_t size, size_t extra, int flags);
99261071Sjasonesize_t	__sallocx(const void *ptr, int flags);
100261071Sjasonevoid	__dallocx(void *ptr, int flags);
101286866Sjasonevoid	__sdallocx(void *ptr, size_t size, int flags);
102261071Sjasonesize_t	__nallocx(size_t size, int flags);
103160056Sjasone__END_DECLS
104157236Sjasone
105157236Sjasone#endif /* _MALLOC_NP_H_ */
106