xref: /freebsd/share/man/man9/malloc.9 (revision a8445737e740901f5f2c8d24c12ef7fc8b00134e)
1.\"	$NetBSD: malloc.9,v 1.3 1996/11/11 00:05:11 lukem Exp $
2.\"
3.\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Paul Kranenburg.
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. All advertising materials mentioning features or use of this software
18.\"    must display the following acknowledgement:
19.\"        This product includes software developed by the NetBSD
20.\"        Foundation, Inc. and its contributors.
21.\" 4. Neither the name of The NetBSD Foundation nor the names of its
22.\"    contributors may be used to endorse or promote products derived
23.\"    from this software without specific prior written permission.
24.\"
25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
29.\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35.\" POSSIBILITY OF SUCH DAMAGE.
36.\"
37.Dd June 16, 1996
38.Dt MALLOC 9
39.Os FreeBSD
40.Sh NAME
41.Nm malloc ,
42.Nm MALLOC ,
43.Nm free ,
44.Nm FREE
45.Nd kernel memory management routines
46.Sh SYNOPSIS
47.Fd #include <sys/types.h>
48.Fd #include <sys/malloc.h>
49.Ft void *
50.Fn malloc "unsigned long size" "struct malloc_type *type" "int flags"
51.Fn MALLOC "space" "cast" "unsigned long size" "struct malloc_type  *type" "int flags"
52.Ft void
53.Fn free "void *addr" "struct malloc_type *type"
54.Fn FREE "void *addr" "struct malloc_type *type"
55.Sh DESCRIPTION
56The
57.Fn malloc
58function allocates uninitialized memory in kernel address space for an
59object whose size is specified by
60.Fa size .
61.Fn free
62releases memory at address
63.Fa addr
64that was previously allocated by
65.Fn malloc
66for re-use.
67The
68.Fn MALLOC
69macro variant is functionally equivalent to
70.Bd -literal -offset indent
71(space) = (cast)malloc((u_long)(size), type, flags)
72.Ed
73.Pp
74and the
75.Fn FREE
76macro variant is equivalent to
77.Bd -literal -offset indent
78free((addr), type)
79.Ed
80.Pp
81Unlike its standard C library counterpart
82.Pq Xr malloc 3 ,
83the kernel version takes two more arguments.  The
84.Fa flags
85argument further qualifies
86.Fn malloc No Ns 's
87operational characteristics as follows:
88.Bl -tag -offset indent
89.It Dv M_NOWAIT
90Causes
91.Fn malloc
92to return
93.Dv NULL
94if the request cannot be immediately fulfilled due to resource shortage.
95Otherwise,
96.Fn malloc
97may call sleep to wait for resources to be released by other processes.
98If this flag is not set,
99.Fn malloc
100will never return
101.Dv NULL .
102Note that
103.Dv M_WAITOK
104is conveniently defined to be 0, and hence may be or'ed into the
105.Fa flags
106argument to indicate that it's Ok to wait for resources.
107.El
108.Pp
109Currently, only one flag is defined.
110.Pp
111The
112.Fa type
113argument is used to perform statistics on memory usage, and for
114basic sanity checks.
115The statistics can be examined by
116.Sq vmstat -m .
117.Pp
118A
119.Fa type
120is defined using the
121.Va malloc_type_t
122typedef like this:
123.Bd -literal -offset indent
124/* sys/something/foo_extern.h */
125
126extern malloc_type_t M_FOOBUF;
127
128/* sys/something/foo_main.c */
129
130malloc_type_t M_FOOBUF = {
131	"Foo Buffers",
132	"Buffers for foo data in transit to the InfImpDrive"
133};
134
135/* sys/something/foo_subr.c */
136
137...
138MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
139
140.Be
141.Sh RETURN VALUES
142.Fn malloc
143returns a kernel virtual address that is suitably aligned for storage of
144any type of object.
145.Sh SEE ALSO
146.Xr vmstat 8
147.Sh DIAGNOSTICS
148A kernel compiled with the
149.Dv DIAGNOSTIC
150configuration option attempts to detect detect memory corruption caused by
151such things as writing outside the allocated area and imbalanced calls to the
152.Fn malloc
153and
154.Fn free
155functions. Failing consistency checks will cause a panic or a system console
156message:
157.Bl -bullet -offset indent -compact
158.Pp
159.It
160panic:
161.Dq malloc: bogus type
162.It
163panic:
164.Dq malloc: allocation too large
165.It
166panic:
167.Dq malloc: wrong bucket
168.It
169panic:
170.Dq malloc: lost data
171.It
172panic:
173.Dq free: address 0x%x out of range
174.It
175panic:
176.Dq free: type %d out of range
177.It
178panic:
179.Dq free: unaligned addr Aq description of object
180.It
181panic:
182.Dq free: item modified
183.It
184panic:
185.Dq free: multiple free[s]
186.It
187.Dq Data modified on freelist: Aq description of object
188.El
189