]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/doc/smm/18.net/5.t
Import OpenCSD -- an ARM CoreSight(tm) Trace Decode Library.
[FreeBSD/FreeBSD.git] / share / doc / smm / 18.net / 5.t
1 .\" Copyright (c) 1983, 1986, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)5.t 8.1 (Berkeley) 6/8/93
29 .\"
30 .nr H2 1
31 .\".ds RH "Memory management
32 .br
33 .ne 2i
34 .NH
35 \s+2Memory management\s0
36 .PP
37 A single mechanism is used for data storage: memory buffers, or
38 \fImbuf\fP's.  An mbuf is a structure of the form:
39 .DS
40 ._f
41 struct mbuf {
42         struct  mbuf *m_next;           /* next buffer in chain */
43         u_long  m_off;                  /* offset of data */
44         short   m_len;                  /* amount of data in this mbuf */
45         short   m_type;                 /* mbuf type (accounting) */
46         u_char  m_dat[MLEN];            /* data storage */
47         struct  mbuf *m_act;            /* link in higher-level mbuf list */
48 };
49 .DE
50 The \fIm_next\fP field is used to chain mbufs together on linked
51 lists, while the \fIm_act\fP field allows lists of mbuf chains to be
52 accumulated.  By convention, the mbufs common to a single object
53 (for example, a packet) are chained together with the \fIm_next\fP
54 field, while groups of objects are linked via the \fIm_act\fP
55 field (possibly when in a queue).
56 .PP
57 Each mbuf has a small data area for storing information, \fIm_dat\fP.
58 The \fIm_len\fP field indicates the amount of data, while the \fIm_off\fP
59 field is an offset to the beginning of the data from the base of the
60 mbuf.  Thus, for example, the macro \fImtod\fP, which converts a pointer
61 to an mbuf to a pointer to the data stored in the mbuf, has the form
62 .DS
63 ._d
64 #define mtod(\fIx\fP,\fIt\fP)   ((\fIt\fP)((int)(\fIx\fP) + (\fIx\fP)->m_off))
65 .DE
66 (note the \fIt\fP parameter, a C type cast, which is used to cast
67 the resultant pointer for proper assignment).
68 .PP
69 In addition to storing data directly in the mbuf's data area, data
70 of page size may be also be stored in a separate area of memory.
71 The mbuf utility routines maintain
72 a pool of pages for this purpose and manipulate a private page map
73 for such pages.
74 An mbuf with an external data area may be recognized by the larger
75 offset to the data area;
76 this is formalized by the macro M_HASCL(\fIm\fP), which is true
77 if the mbuf whose address is \fIm\fP has an external page cluster.
78 An array of reference counts on pages is also maintained
79 so that copies of pages may be made without core to core
80 copying  (copies are created simply by duplicating the reference to the data
81 and incrementing the associated reference counts for the pages).
82 Separate data pages are currently used only
83 when copying data from a user process into the kernel,
84 and when bringing data in at the hardware level.  Routines which
85 manipulate mbufs are not normally aware whether data is stored directly in 
86 the mbuf data array, or if it is kept in separate pages.
87 .PP
88 The following may be used to allocate and free mbufs:
89 .LP
90 m = m_get(wait, type);
91 .br
92 MGET(m, wait, type);
93 .IP
94 The subroutine \fIm_get\fP and the macro \fIMGET\fP
95 each allocate an mbuf, placing its address in \fIm\fP.
96 The argument \fIwait\fP is either M_WAIT or M_DONTWAIT according
97 to whether allocation should block or fail if no mbuf is available.
98 The \fItype\fP is one of the predefined mbuf types for use in accounting
99 of mbuf allocation.
100 .IP "MCLGET(m);"
101 This macro attempts to allocate an mbuf page cluster
102 to associate with the mbuf \fIm\fP.
103 If successful, the length of the mbuf is set to CLSIZE,
104 the size of the page cluster.
105 .LP
106 n = m_free(m);
107 .br
108 MFREE(m,n);
109 .IP
110 The routine \fIm_free\fP and the macro \fIMFREE\fP
111 each free a single mbuf, \fIm\fP, and any associated external storage area,
112 placing a pointer to its successor in the chain it heads, if any, in \fIn\fP.
113 .IP "m_freem(m);"
114 This routine frees an mbuf chain headed by \fIm\fP.
115 .PP
116 The following utility routines are available for manipulating mbuf
117 chains:
118 .IP "m = m_copy(m0, off, len);"
119 .br
120 The \fIm_copy\fP routine create a copy of all, or part, of a
121 list of the mbufs in \fIm0\fP.  \fILen\fP bytes of data, starting 
122 \fIoff\fP bytes from the front of the chain, are copied. 
123 Where possible, reference counts on pages are used instead
124 of core to core copies.  The original mbuf chain must have at
125 least \fIoff\fP + \fIlen\fP bytes of data.  If \fIlen\fP is
126 specified as M_COPYALL, all the data present, offset
127 as before, is copied.  
128 .IP "m_cat(m, n);"
129 .br
130 The mbuf chain, \fIn\fP, is appended to the end of \fIm\fP.
131 Where possible, compaction is performed.
132 .IP "m_adj(m, diff);"
133 .br
134 The mbuf chain, \fIm\fP is adjusted in size by \fIdiff\fP
135 bytes.  If \fIdiff\fP is non-negative, \fIdiff\fP bytes
136 are shaved off the front of the mbuf chain.  If \fIdiff\fP
137 is negative, the alteration is performed from back to front.
138 No space is reclaimed in this operation; alterations are
139 accomplished by changing the \fIm_len\fP and \fIm_off\fP
140 fields of mbufs.
141 .IP "m = m_pullup(m0, size);"
142 .br
143 After a successful call to \fIm_pullup\fP, the mbuf at
144 the head of the returned list, \fIm\fP, is guaranteed
145 to have at least \fIsize\fP
146 bytes of data in contiguous memory within the data area of the mbuf
147 (allowing access via a pointer, obtained using the \fImtod\fP macro,
148 and allowing the mbuf to be located from a pointer to the data area
149 using \fIdtom\fP, defined below).
150 If the original data was less than \fIsize\fP bytes long,
151 \fIlen\fP was greater than the size of an mbuf data
152 area (112 bytes), or required resources were unavailable,
153 \fIm\fP is 0 and the original mbuf chain is deallocated.
154 .IP
155 This routine is particularly useful when verifying packet
156 header lengths on reception.  For example, if a packet is
157 received and only 8 of the necessary 16 bytes required
158 for a valid packet header are present at the head of the list
159 of mbufs representing the packet, the remaining 8 bytes
160 may be ``pulled up'' with a single \fIm_pullup\fP call.
161 If the call fails the invalid packet will have been discarded.
162 .PP
163 By insuring that mbufs always reside on 128 byte boundaries,
164 it is always possible to locate the mbuf associated with a data
165 area by masking off the low bits of the virtual address.
166 This allows modules to store data structures in mbufs and
167 pass them around without concern for locating the original
168 mbuf when it comes time to free the structure.
169 Note that this works only with objects stored in the internal data
170 buffer of the mbuf.
171 The \fIdtom\fP macro is used to convert a pointer into an mbuf's
172 data area to a pointer to the mbuf,
173 .DS
174 #define dtom(x) ((struct mbuf *)((int)x & ~(MSIZE-1)))
175 .DE
176 .PP
177 Mbufs are used for dynamically allocated data structures such as
178 sockets as well as memory allocated for packets and headers.  Statistics are
179 maintained on mbuf usage and can be viewed by users using the
180 \fInetstat\fP\|(1) program.