⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5.t

📁 早期freebsd实现
💻 T
字号:
.\" Copyright (c) 1983, 1986, 1993.\"	The Regents of the University of California.  All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\"    must display the following acknowledgement:.\"	This product includes software developed by the University of.\"	California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\"    may be used to endorse or promote products derived from this software.\"    without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\"	@(#)5.t	8.1 (Berkeley) 6/8/93.\".nr H2 1.\".ds RH "Memory management.br.ne 2i.NH\s+2Memory management\s0.PPA single mechanism is used for data storage: memory buffers, or\fImbuf\fP's.  An mbuf is a structure of the form:.DS._fstruct mbuf {	struct	mbuf *m_next;		/* next buffer in chain */	u_long	m_off;			/* offset of data */	short	m_len;			/* amount of data in this mbuf */	short	m_type;			/* mbuf type (accounting) */	u_char	m_dat[MLEN];		/* data storage */	struct	mbuf *m_act;		/* link in higher-level mbuf list */};.DEThe \fIm_next\fP field is used to chain mbufs together on linkedlists, while the \fIm_act\fP field allows lists of mbuf chains to beaccumulated.  By convention, the mbufs common to a single object(for example, a packet) are chained together with the \fIm_next\fPfield, while groups of objects are linked via the \fIm_act\fPfield (possibly when in a queue)..PPEach mbuf has a small data area for storing information, \fIm_dat\fP.The \fIm_len\fP field indicates the amount of data, while the \fIm_off\fPfield is an offset to the beginning of the data from the base of thembuf.  Thus, for example, the macro \fImtod\fP, which converts a pointerto an mbuf to a pointer to the data stored in the mbuf, has the form.DS._d#define	mtod(\fIx\fP,\fIt\fP)	((\fIt\fP)((int)(\fIx\fP) + (\fIx\fP)->m_off)).DE(note the \fIt\fP parameter, a C type cast, which is used to castthe resultant pointer for proper assignment)..PPIn addition to storing data directly in the mbuf's data area, dataof page size may be also be stored in a separate area of memory.The mbuf utility routines maintaina pool of pages for this purpose and manipulate a private page mapfor such pages.An mbuf with an external data area may be recognized by the largeroffset to the data area;this is formalized by the macro M_HASCL(\fIm\fP), which is trueif the mbuf whose address is \fIm\fP has an external page cluster.An array of reference counts on pages is also maintainedso that copies of pages may be made without core to corecopying  (copies are created simply by duplicating the reference to the dataand incrementing the associated reference counts for the pages).Separate data pages are currently used onlywhen copying data from a user process into the kernel,and when bringing data in at the hardware level.  Routines whichmanipulate mbufs are not normally aware whether data is stored directly in the mbuf data array, or if it is kept in separate pages..PPThe following may be used to allocate and free mbufs:.LPm = m_get(wait, type);.brMGET(m, wait, type);.IPThe subroutine \fIm_get\fP and the macro \fIMGET\fPeach allocate an mbuf, placing its address in \fIm\fP.The argument \fIwait\fP is either M_WAIT or M_DONTWAIT accordingto whether allocation should block or fail if no mbuf is available.The \fItype\fP is one of the predefined mbuf types for use in accountingof mbuf allocation..IP "MCLGET(m);"This macro attempts to allocate an mbuf page clusterto associate with the mbuf \fIm\fP.If successful, the length of the mbuf is set to CLSIZE,the size of the page cluster..LPn = m_free(m);.brMFREE(m,n);.IPThe routine \fIm_free\fP and the macro \fIMFREE\fPeach free a single mbuf, \fIm\fP, and any associated external storage area,placing a pointer to its successor in the chain it heads, if any, in \fIn\fP..IP "m_freem(m);"This routine frees an mbuf chain headed by \fIm\fP..PPThe following utility routines are available for manipulating mbufchains:.IP "m = m_copy(m0, off, len);".brThe \fIm_copy\fP routine create a copy of all, or part, of alist of the mbufs in \fIm0\fP.  \fILen\fP bytes of data, starting \fIoff\fP bytes from the front of the chain, are copied. Where possible, reference counts on pages are used insteadof core to core copies.  The original mbuf chain must have atleast \fIoff\fP + \fIlen\fP bytes of data.  If \fIlen\fP isspecified as M_COPYALL, all the data present, offsetas before, is copied.  .IP "m_cat(m, n);".brThe mbuf chain, \fIn\fP, is appended to the end of \fIm\fP.Where possible, compaction is performed..IP "m_adj(m, diff);".brThe mbuf chain, \fIm\fP is adjusted in size by \fIdiff\fPbytes.  If \fIdiff\fP is non-negative, \fIdiff\fP bytesare shaved off the front of the mbuf chain.  If \fIdiff\fPis negative, the alteration is performed from back to front.No space is reclaimed in this operation; alterations areaccomplished by changing the \fIm_len\fP and \fIm_off\fPfields of mbufs..IP "m = m_pullup(m0, size);".brAfter a successful call to \fIm_pullup\fP, the mbuf atthe head of the returned list, \fIm\fP, is guaranteedto have at least \fIsize\fPbytes of data in contiguous memory within the data area of the mbuf(allowing access via a pointer, obtained using the \fImtod\fP macro,and allowing the mbuf to be located from a pointer to the data areausing \fIdtom\fP, defined below).If the original data was less than \fIsize\fP bytes long,\fIlen\fP was greater than the size of an mbuf dataarea (112 bytes), or required resources were unavailable,\fIm\fP is 0 and the original mbuf chain is deallocated..IPThis routine is particularly useful when verifying packetheader lengths on reception.  For example, if a packet isreceived and only 8 of the necessary 16 bytes requiredfor a valid packet header are present at the head of the listof mbufs representing the packet, the remaining 8 bytesmay be ``pulled up'' with a single \fIm_pullup\fP call.If the call fails the invalid packet will have been discarded..PPBy insuring that mbufs always reside on 128 byte boundaries,it is always possible to locate the mbuf associated with a dataarea by masking off the low bits of the virtual address.This allows modules to store data structures in mbufs andpass them around without concern for locating the originalmbuf when it comes time to free the structure.Note that this works only with objects stored in the internal databuffer of the mbuf.The \fIdtom\fP macro is used to convert a pointer into an mbuf'sdata area to a pointer to the mbuf,.DS#define	dtom(x)	((struct mbuf *)((int)x & ~(MSIZE-1))).DE.PPMbufs are used for dynamically allocated data structures such assockets as well as memory allocated for packets and headers.  Statistics aremaintained on mbuf usage and can be viewed by users using the\fInetstat\fP\|(1) program.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -