malloc.h revision 95198
1139823Simp/*
2122702Sandre * Copyright (c) 1987, 1993
3122702Sandre *	The Regents of the University of California.  All rights reserved.
4122702Sandre *
5122702Sandre * Redistribution and use in source and binary forms, with or without
6122702Sandre * modification, are permitted provided that the following conditions
7122702Sandre * are met:
8122702Sandre * 1. Redistributions of source code must retain the above copyright
9122702Sandre *    notice, this list of conditions and the following disclaimer.
10122702Sandre * 2. Redistributions in binary form must reproduce the above copyright
11122702Sandre *    notice, this list of conditions and the following disclaimer in the
12122702Sandre *    documentation and/or other materials provided with the distribution.
13122702Sandre * 3. All advertising materials mentioning features or use of this software
14122702Sandre *    must display the following acknowledgement:
15122702Sandre *	This product includes software developed by the University of
16122702Sandre *	California, Berkeley and its contributors.
17122702Sandre * 4. Neither the name of the University nor the names of its contributors
18122702Sandre *    may be used to endorse or promote products derived from this software
19122702Sandre *    without specific prior written permission.
20122702Sandre *
21122702Sandre * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22122702Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23122702Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24122702Sandre * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25122702Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26122702Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27122702Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28122702Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29122702Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30122702Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31122702Sandre * SUCH DAMAGE.
32122702Sandre *
33122702Sandre *	@(#)malloc.h	8.5 (Berkeley) 5/3/95
34122702Sandre * $FreeBSD: head/sys/sys/malloc.h 95198 2002-04-21 11:08:52Z markm $
35122702Sandre */
36122702Sandre
37122702Sandre#ifndef _SYS_MALLOC_H_
38122702Sandre#define	_SYS_MALLOC_H_
39122759Sandre
40122759Sandre#include <vm/uma.h>
41122759Sandre
42122759Sandre#define splmem splhigh
43122702Sandre
44122702Sandre#define MINALLOCSIZE	UMA_SMALLEST_UNIT
45122702Sandre
46122702Sandre/*
47122702Sandre * flags to malloc.
48122702Sandre */
49122702Sandre#define	M_WAITOK	0x0000
50122702Sandre#define	M_NOWAIT	0x0001		/* do not block */
51122702Sandre#define	M_USE_RESERVE	0x0002		/* can alloc out of reserve memory */
52122702Sandre#define	M_ZERO		0x0004		/* bzero the allocation */
53122702Sandre
54122702Sandre#define	M_MAGIC		877983977	/* time when first defined :-) */
55122702Sandre
56122702Sandrestruct malloc_type {
57122702Sandre	struct malloc_type *ks_next;	/* next in list */
58122702Sandre	long 	ks_memuse;	/* total memory held in bytes */
59122702Sandre	long	ks_size;	/* sizes of this thing that are allocated */
60122702Sandre	long	ks_inuse;	/* # of packets of this type currently in use */
61122702Sandre	uint64_t ks_calls;	/* total packets of this type ever allocated */
62122702Sandre	long	ks_maxused;	/* maximum number ever used */
63122702Sandre	u_long	ks_magic;	/* if it's not magic, don't touch it */
64122702Sandre	const char *ks_shortdesc;	/* short description */
65122702Sandre};
66122702Sandre
67122702Sandre#ifdef _KERNEL
68122702Sandre#define	MALLOC_DEFINE(type, shortdesc, longdesc) \
69122702Sandre	struct malloc_type type[1] = { \
70122702Sandre		{ NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc } \
71122702Sandre	}; \
72122702Sandre	SYSINIT(type##_init, SI_SUB_KMEM, SI_ORDER_SECOND, malloc_init, type); \
73122702Sandre	SYSUNINIT(type##_uninit, SI_SUB_KMEM, SI_ORDER_ANY, malloc_uninit, type)
74122702Sandre
75122702Sandre#define	MALLOC_DECLARE(type) \
76122702Sandre	extern struct malloc_type type[1]
77122702Sandre
78122702SandreMALLOC_DECLARE(M_CACHE);
79122702SandreMALLOC_DECLARE(M_DEVBUF);
80122702SandreMALLOC_DECLARE(M_TEMP);
81122702Sandre
82122702SandreMALLOC_DECLARE(M_IP6OPT); /* for INET6 */
83122702SandreMALLOC_DECLARE(M_IP6NDP); /* for INET6 */
84122702Sandre
85122702Sandre/*
86122702Sandre * Deprecated macro versions of not-quite-malloc() and free().
87122702Sandre */
88122702Sandre#define	MALLOC(space, cast, size, type, flags) \
89122702Sandre	((space) = (cast)malloc((u_long)(size), (type), (flags)))
90122702Sandre#define	FREE(addr, type) free((addr), (type))
91122702Sandre
92122702Sandre/*
93122702Sandre * XXX this should be declared in <sys/uio.h>, but that tends to fail
94122702Sandre * because <sys/uio.h> is included in a header before the source file
95122702Sandre * has a chance to include <sys/malloc.h> to get MALLOC_DECLARE() defined.
96122702Sandre */
97122702SandreMALLOC_DECLARE(M_IOV);
98122702Sandre
99122702Sandre/* XXX struct malloc_type is unused for contig*(). */
100122702Sandrevoid	contigfree(void *addr, unsigned long size, struct malloc_type *type);
101122702Sandrevoid	*contigmalloc(unsigned long size, struct malloc_type *type, int flags,
102122702Sandre	    unsigned long low, unsigned long high, unsigned long alignment,
103122702Sandre	    unsigned long boundary);
104122702Sandrevoid	free(void *addr, struct malloc_type *type);
105122702Sandrevoid	*malloc(unsigned long size, struct malloc_type *type, int flags);
106122702Sandrevoid	malloc_init(void *);
107122702Sandrevoid	malloc_uninit(void *);
108122702Sandrevoid	*realloc(void *addr, unsigned long size, struct malloc_type *type,
109122702Sandre	    int flags);
110128872Sandrevoid	*reallocf(void *addr, unsigned long size, struct malloc_type *type,
111133497Sandre	    int flags);
112128872Sandre#endif /* _KERNEL */
113128872Sandre
114128872Sandre#endif /* !_SYS_MALLOC_H_ */
115128872Sandre