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