xref: /illumos-gate/usr/src/lib/libtecla/common/freelist.h (revision ddb365bfc9e868ad24ccdcb0dc91af18b10df082)
1 #ifndef freelist_h
2 #define freelist_h
3 
4 /*
5  * Copyright (c) 2000, 2001, 2002, 2003, 2004 by Martin C. Shepherd.
6  *
7  * All rights reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, and/or sell copies of the Software, and to permit persons
14  * to whom the Software is furnished to do so, provided that the above
15  * copyright notice(s) and this permission notice appear in all copies of
16  * the Software and that both the above copyright notice(s) and this
17  * permission notice appear in supporting documentation.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
22  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
24  * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
25  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
26  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
27  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28  *
29  * Except as contained in this notice, the name of a copyright holder
30  * shall not be used in advertising or otherwise to promote the sale, use
31  * or other dealings in this Software without prior written authorization
32  * of the copyright holder.
33  */
34 
35 /*
36  * This module provides a memory allocation scheme that helps to
37  * prevent memory fragmentation by allocating large blocks of
38  * fixed sized objects and forming them into a free-list for
39  * subsequent allocations. The free-list is expanded as needed.
40  */
41 typedef struct FreeList FreeList;
42 
43 /*
44  * Allocate a new free-list from blocks of 'blocking_factor' objects of size
45  * node_size. The node_size argument should be determined by applying
46  * the sizeof() operator to the object type that you intend to allocate from
47  * the freelist.
48  */
49 FreeList *_new_FreeList(size_t node_size, unsigned blocking_factor);
50 
51 /*
52  * If it is known that none of the nodes currently allocated from
53  * a freelist are still in use, the following function can be called
54  * to return all nodes to the freelist without the overhead of
55  * having to call del_FreeListNode() for every allocated node. The
56  * nodes of the freelist can then be reused by future callers to
57  * new_FreeListNode().
58  */
59 void _rst_FreeList(FreeList *fl);
60 
61 /*
62  * Delete a free-list.
63  */
64 FreeList *_del_FreeList(FreeList *fl, int force);
65 
66 /*
67  * Determine the number of nodes that are currently in use.
68  */
69 long _busy_FreeListNodes(FreeList *fl);
70 
71 /*
72  * Query the number of allocated nodes in the freelist which are
73  * currently unused.
74  */
75 long _idle_FreeListNodes(FreeList *fl);
76 
77 /*
78  * Allocate a new object from a free-list.
79  */
80 void *_new_FreeListNode(FreeList *fl);
81 
82 /*
83  * Return an object to the free-list that it was allocated from.
84  */
85 void *_del_FreeListNode(FreeList *fl, void *object);
86 
87 #endif
88