xref: /freebsd/share/man/man9/vmem.9 (revision ae1f3df43466466a21c7da0df93ecb58a3e53d74)
1.\"	$NetBSD: vmem.9,v 1.15 2013/01/29 22:02:17 wiz Exp $
2.\"
3.\" Copyright (c)2006 YAMAMOTO Takashi,
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.\" $FreeBSD$
28.\"
29.\" ------------------------------------------------------------
30.Dd July 12, 2013
31.Dt VMEM 9
32.Os
33.\" ------------------------------------------------------------
34.Sh NAME
35.Nm vmem
36.Nd general purpose resource allocator
37.\" ------------------------------------------------------------
38.Sh SYNOPSIS
39.In sys/vmem.h
40.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41.Ft vmem_t *
42.Fn vmem_create \
43"const char *name" "vmem_addr_t base" "vmem_size_t size" "vmem_size_t quantum" \
44"vmem_size_t qcache_max" "int flags"
45.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
46.Ft int
47.Fn vmem_add \
48"vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size" "int flags"
49.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50.Ft int
51.Fn vmem_xalloc \
52"vmem_t *vm" "const vmem_size_t size" "vmem_size_t align" \
53"const vmem_size_t phase" "const vmem_size_t nocross" \
54"const vmem_addr_t minaddr" "const vmem_addr_t maxaddr" "int flags" \
55"vmem_addr_t *addrp"
56.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
57.Ft void
58.Fn vmem_xfree "vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size"
59.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60.Ft int
61.Fn vmem_alloc "vmem_t *vm" "vmem_size_t size" "int flags" "vmem_addr_t *addrp"
62.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63.Ft void
64.Fn vmem_free "vmem_t *vm" "vmem_addr_t addr" "vmem_size_t size"
65.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
66.Ft void
67.Fn vmem_destroy "vmem_t *vm"
68.\" ------------------------------------------------------------
69.Sh DESCRIPTION
70The
71.Nm
72is a general purpose resource allocator.
73Despite its name, it can be used for arbitrary resources
74other than virtual memory.
75.Pp
76.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
77.Fn vmem_create
78creates a new vmem arena.
79.Bl -tag -width qcache_max
80.It Fa name
81The string to describe the vmem.
82.It Fa base
83The start address of the initial span.
84Pass
85.Dv 0
86if no initial span is required.
87.It Fa size
88The size of the initial span.
89Pass
90.Dv 0
91if no initial span is required.
92.It Fa quantum
93The smallest unit of allocation.
94.It Fa qcache_max
95The largest size of allocations which can be served by quantum cache.
96It is merely a hint and can be ignored.
97.It Fa flags
98Combination of
99.Xr malloc 9
100wait flag and
101.Nm
102allocation strategy flag:
103.Bl -tag -width M_FIRSTFIT
104.It Dv M_FIRSTFIT
105Prefer allocation performance.
106.It Dv M_BESTFIT
107Prefer space efficiency.
108.El
109.El
110.Pp
111.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112.Fn vmem_add
113adds a span of size
114.Fa size
115starting at
116.Fa addr
117to the arena.
118Returns
1190
120on success,
121.Dv ENOMEM
122on failure.
123.Fa flags
124is
125.Xr malloc 9
126wait flag.
127.Pp
128.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
129.Fn vmem_xalloc
130allocates a resource from the arena.
131.Bl -tag -width nocross
132.It Fa vm
133The arena which we allocate from.
134.It Fa size
135Specify the size of the allocation.
136.It Fa align
137If zero, don't care about the alignment of the allocation.
138Otherwise, request a resource segment starting at
139offset
140.Fa phase
141from an
142.Fa align
143aligned boundary.
144.It Fa phase
145See the above description of
146.Fa align .
147If
148.Fa align
149is zero,
150.Fa phase
151should be zero.
152Otherwise,
153.Fa phase
154should be smaller than
155.Fa align .
156.It Fa nocross
157Request a resource which doesn't cross
158.Fa nocross
159aligned boundary.
160.It Fa minaddr
161Specify the minimum address which can be allocated, or
162.Dv VMEM_ADDR_MIN
163if the caller does not care.
164.It Fa maxaddr
165Specify the maximum address which can be allocated, or
166.Dv VMEM_ADDR_MAX
167if the caller does not care.
168.It Fa flags
169A bitwise OR of an allocation strategy and a
170.Xr malloc 9
171wait flag.
172The allocation strategy is one of
173.Dv M_FIRSTFIT
174and
175.Dv M_BESTFIT .
176.It Fa addrp
177On success, if
178.Fa addrp
179is not
180.Dv NULL ,
181.Fn vmem_xalloc
182overwrites it with the start address of the allocated span.
183.El
184.Pp
185.\" ------------------------------------------------------------
186.Fn vmem_xfree
187frees resource allocated by
188.Fn vmem_xalloc
189to the arena.
190.Bl -tag -width addr
191.It Fa vm
192The arena which we free to.
193.It Fa addr
194The resource being freed.
195It must be the one returned by
196.Fn vmem_xalloc .
197Notably, it must not be the one from
198.Fn vmem_alloc .
199Otherwise, the behaviour is undefined.
200.It Fa size
201The size of the resource being freed.
202It must be the same as the
203.Fa size
204argument used for
205.Fn vmem_xalloc .
206.El
207.Pp
208.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
209.Fn vmem_alloc
210allocates a resource from the arena.
211.Bl -tag -width flags
212.It Fa vm
213The arena which we allocate from.
214.It Fa size
215Specify the size of the allocation.
216.It Fa flags
217A bitwise OR of an
218.Nm
219allocation strategy flag (see above) and a
220.Xr malloc 9
221sleep flag.
222.It Fa addrp
223On success, if
224.Fa addrp
225is not
226.Dv NULL ,
227.Fn vmem_alloc
228overwrites it with the start address of the allocated span.
229.El
230.Pp
231.\" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
232.Fn vmem_free
233frees resource allocated by
234.Fn vmem_alloc
235to the arena.
236.Bl -tag -width addr
237.It Fa vm
238The arena which we free to.
239.It Fa addr
240The resource being freed.
241It must be the one returned by
242.Fn vmem_alloc .
243Notably, it must not be the one from
244.Fn vmem_xalloc .
245Otherwise, the behaviour is undefined.
246.It Fa size
247The size of the resource being freed.
248It must be the same as the
249.Fa size
250argument used for
251.Fn vmem_alloc .
252.El
253.Pp
254.\" ------------------------------------------------------------
255.Fn vmem_destroy
256destroys a vmem arena.
257.Bl -tag -width vm
258.It Fa vm
259The vmem arena being destroyed.
260The caller should ensure that no one will use it anymore.
261.El
262.\" ------------------------------------------------------------
263.Sh RETURN VALUES
264.Fn vmem_create
265returns a pointer to the newly allocated vmem_t.
266Otherwise, it returns
267.Dv NULL .
268.Pp
269On success,
270.Fn vmem_xalloc
271and
272.Fn vmem_alloc
273return 0.
274Otherwise,
275.Dv ENOMEM
276is returned.
277.\" ------------------------------------------------------------
278.Sh CODE REFERENCES
279The
280.Nm
281subsystem is implemented within the file
282.Pa sys/kern/subr_vmem.c .
283.\" ------------------------------------------------------------
284.Sh SEE ALSO
285.Xr malloc 9
286.Rs
287.%A Jeff Bonwick
288.%A Jonathan Adams
289.%T "Magazines and Vmem: Extending the Slab Allocator to Many CPUs and Arbitrary Resources"
290.%J "2001 USENIX Annual Technical Conference"
291.%D 2001
292.Re
293.\" ------------------------------------------------------------
294.Sh HISTORY
295The
296.Nm
297allocator was originally implemented in
298.Nx .
299It was introduced in
300.Fx 10.0 .
301.Sh AUTHORS
302.An -nosplit
303Original implementation of
304.Nm
305was written by
306.An "YAMAMOTO Takashi" .
307The
308.Fx
309port was made by
310.An "Jeff Roberson" .
311.Sh BUGS
312.Nm
313relies on
314.Xr malloc 9 ,
315so it cannot be used as early during system bootstrap.
316