📄 mmlist.3
字号:
.\" $Id: mmlist.3,v 1.4 2003/01/08 20:57:46 mmondor Exp $.\".\" Copyright (C) 2002-2003, Matthew Mondor.\" 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 written by Matthew Mondor..\" 4. The name of Matthew Mondor may not be used to endorse or promote.\" products derived from this software without specific prior written.\" permission..\".\" THIS SOFTWARE IS PROVIDED BY MATTHEW MONDOR ``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 MATTHEW MONDOR 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..\".Dd January 7, 2003.Os.Dt MMLIST 3.Sh NAME.Nm mmlist.Nd General purpose doubly linked list library with pool support..Sh SYNOPSIS.Fd #include <sys/types.h>.Fd #include <mmtypes.h>.Fd #include <mmlist.h>.Ft void.Fn INITLIST "list *list".Ft list *.Fn openlist "void *(*malloc)(size_t)" "void (*free)(void *)" \"size_t nodelen" "size_t pagesize" "long maxpages".Ft list *.Fn blocklist "void *(*malloc)(size_t)" "void (*free)(void *)" \"size_t nodelen" "long nodes".Ft list *.Fn closelist "list *list".Ft node *.Fn allocnode "list *list" "bool zero".Ft node *.Fn freenode "node *node".Ft void.Fn APPENDNODE "list *list" "node *node".Ft void.Fn INSERTNODE "list *list" "node *node".Ft void.Fn INSERTNODEAT "list *list" "node *atnode" "node *node".Ft void.Fn SWAPNODE "list *dest" "list *source" "node *node" "bool insert".Ft void.Fn UNLINKNODE "list *list" "node *node".Sh DESCRIPTIONThis library provides a useful set of functions to manipulate doubly linkedlists. We also optionally allow pre-buffering node pools allowing to veryefficiently allocate/free nodes for a particular list.All nodes in a pool are.Ar longword aligned and are thus suitable for.Ar long *referencing. The pools ensure to minimize calls to the supplied allocationfunctions (such as.Xr malloc 3and.Xr free 3 ) ,preparing buffered pages. Even those pages are buffered as required andstatistics are maintained to monitor average current usage so that thenumber of pre-buffered pages for a pool remain available even when mostnodes have been freed back. An application can then alternate size properlyin a very fast manner using these pools. Of course the statistics are updatedwhich will also allow those buffered pages to really be freed back whenit is noticeable that the pool does not grow large again for some while..PpThe following structures and fields of interest which are left for the userto reference are described below (only fields of user interest are shown),declared in.Aq Pa mmfd.h.Bd -literal -offset indenttypedef struct node { struct node *prev, *next;} node;typedef struct list { struct node *top, *bottom; long nodes;} list;.Ed.PpA custom node has to be declared as a structure using the node structure asit's first field, as in the following:.Bd -literal -offset indentstruct mynode { node nod; int i; /*...other custom fields...*/};.Ed.Pp.Fn INITLISTinitializes a.Nm listobject in such a way that it can be used to store nodes. This does notsetup any memory pool and thus.Fn allocnodeand.Fn freenodewill not work with this type of list..Pp.Fn openlistallocates required resources to create a list handle with a node pre-bufferingpool. The supplied.Fa mallocand.Fa freearguments can either be.Fn malloc ,.Fn freerespectively, but custom functions may also be provided..Fa nodelenconsists of sizeof(struct mynode) in the previous example and should bethe size of the node which may then later on be allocated using.Fn allocnodeat will..Fa pagesizespecifies the smallest amount of memory, in bytes, which will be initiallyprepared to be used as buffered nodes for.Fn allocnodecalls..Fa maxpagesspecifies the maximum number of.Fa pagesizebytes blocks that will be allocated for this pool/list..Fn allocnodewill no longer be able to allocate new nodes if this is reached. 0 may alsobe specified for this option to specify that no limit is wanted. Using thispool mechanism minimizes calls to.Fn mallocand optimizes things considerably. Typical.Fa pagesizevalues are 4096, 8192, 16384 or 65536 bytes. Any size may be used as long asat least two nodes may fit in a page. Note that if no pool is wanted it oftencan be useful to simply define a.Fa listobject and clear it to zeros using.Fn bzero ,.Fn memset ,or.Fn mm_memclr .The allocation functions will not be available, and such a list should notbe closed using.Fn closelist ,but it will be available to hold nodes..Pp.Fn blocklistalso prepares a list handle to be used by other functions, but allocates in oneblock the requested number of maximum nodes to be available for.Fn allocnodecalls..Pp.Fn closelistcalls the provided.Fn freefunction to free all resources allocated for the specified.Fa list *handle. Any.Fn allocnodeallocated memory is also freed. You should be sure to not have to referenceor use anymore any node that has been allocated for this list using.Fn allocnodebefore calling this function. If needed, custom allocated nodes may be usedinstead of pre-buffered ones, and unlinked from the list before it's freedusing.Fn UNLINKNODEfunction, so they then remain useable by other lists..Pp.Fn allocnodeattempts to allocate a pre-buffered node from the pool reserved to thespecified.Fa list *and optionally zeros allocated memory like.Fn callocwould if.Fa zerois TRUE. If required a new page of pre-buffered nodes will be allocatedand prepared, if within allowed page limits, to make other nodes availablefor allocation. If the list was created using.Fn blocklistfunction, only a single page is available allowing to allocate the maximumnumber of nodes that was specified..Pp.Fn freenoderestores specified.Fa node *to the list pool.Fn allocnodeallocated node from, making it available for re-allocating later. Of courseif all nodes of a particular page have been freed, the page is internallyreleased into a pool, and eventually back to the system if it is not recycledsoon enough. If the list was created using.Fn blocklistfunction, the buffer will never shrink as it handles a fixed amount of nodesto be available for allocation..Pp.Fn APPENDNODElinks node pointed to by.Fa nodeafter all elements in list specified by.Fa list.Pp.Fn INSERTNODElinks node.Fa nodebefore all elements of list.Fa list.Pp.Fn INSERTNODEATlinks node.Fa nodebefore node.Fa atnodeinto.Fa list.Pp.Fn SWAPNODEunlinks node.Fa nodefrom.Fa source, and appends or inserts it into list.Fa destdepending on weither.Fa insertis TRUE or FALSE..Pp.Fn UNLINKNODEunlinks node.Fa nodefrom.Fa listbut does not free the node in any way..Sh RETURN VALUES.Fn openlistand.Fn blocklistreturn a.Fa list *or NULL pointer on error (out of memory)..Pp.Fn closelistalways returns a NULL pointer of type.Fa list *.Pp.Fn allocnodereturns a.Fa node *or NULL on error (no more memory allowed for this pool, or out of memory)..Pp.Fn freenodealways returns a NULL pointer of type.Fa node *.Pp.Fn INITLIST ,.Fn APPENDNODE ,.Fn INSERTNODE ,.Fn INSERTNODEAT ,.Fn SWAPNODEand.Fn UNLINKNODEdon't return anything..Sh SEE ALSO.Xr malloc 3 ,.Xr free 3.Sh STANDARDSNone.Sh HISTORYmmlist was originally developped on NetBSD 1.5 for the Xisop portable kernelproject from Matthew Mondor, then imported to be used by mmserver, mmftpd andmmmail daemons..Sh AUTHORSThe mmlist library was written by Matthew Mondor and isCopyright (c) 2001-2003, Matthew Mondor, All rights reserved..Sh BUGSPlease report any bug to mmondor@gobot.ca
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -