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#pragma ident	"%Z%%M%	%I%	%E% SMI"
36
37/*
38 * This module provides a memory allocation scheme that helps to
39 * prevent memory fragmentation by allocating large blocks of
40 * fixed sized objects and forming them into a free-list for
41 * subsequent allocations. The free-list is expanded as needed.
42 */
43typedef struct FreeList FreeList;
44
45/*
46 * Allocate a new free-list from blocks of 'blocking_factor' objects of size
47 * node_size. The node_size argument should be determined by applying
48 * the sizeof() operator to the object type that you intend to allocate from
49 * the freelist.
50 */
51FreeList *_new_FreeList(size_t node_size, unsigned blocking_factor);
52
53/*
54 * If it is known that none of the nodes currently allocated from
55 * a freelist are still in use, the following function can be called
56 * to return all nodes to the freelist without the overhead of
57 * having to call del_FreeListNode() for every allocated node. The
58 * nodes of the freelist can then be reused by future callers to
59 * new_FreeListNode().
60 */
61void _rst_FreeList(FreeList *fl);
62
63/*
64 * Delete a free-list.
65 */
66FreeList *_del_FreeList(FreeList *fl, int force);
67
68/*
69 * Determine the number of nodes that are currently in use.
70 */
71long _busy_FreeListNodes(FreeList *fl);
72
73/*
74 * Query the number of allocated nodes in the freelist which are
75 * currently unused.
76 */
77long _idle_FreeListNodes(FreeList *fl);
78
79/*
80 * Allocate a new object from a free-list.
81 */
82void *_new_FreeListNode(FreeList *fl);
83
84/*
85 * Return an object to the free-list that it was allocated from.
86 */
87void *_del_FreeListNode(FreeList *fl, void *object);
88
89#endif
90