00001 00002 // 00003 // Copyright (c) 2000-2003 Intel Corporation 00004 // All rights reserved. 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // * Redistributions of source code must retain the above copyright notice, 00010 // this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above copyright notice, 00012 // this list of conditions and the following disclaimer in the documentation 00013 // and/or other materials provided with the distribution. 00014 // * Neither name of Intel Corporation nor the names of its contributors 00015 // may be used to endorse or promote products derived from this software 00016 // without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 00022 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00025 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00026 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00027 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00028 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00031 00032 #ifndef FREE_LIST_H 00033 #define FREE_LIST_H 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 #include "ithread.h" 00040 #include <errno.h> 00041 00042 /**************************************************************************** 00043 * Name: FreeListNode 00044 * 00045 * Description: 00046 * free list node. points to next free item. 00047 * memory for node is borrowed from allocated items. 00048 * Internal Use Only. 00049 *****************************************************************************/ 00050 typedef struct FREELISTNODE 00051 { 00052 struct FREELISTNODE*next; //pointer to next free node 00053 } FreeListNode; 00054 00055 00056 /**************************************************************************** 00057 * Name: FreeList 00058 * 00059 * Description: 00060 * Stores head and size of free list, as well as mutex for protection. 00061 * Internal Use Only. 00062 *****************************************************************************/ 00063 typedef struct FREELIST 00064 { 00065 FreeListNode *head; //head of free list 00066 size_t element_size; //size of elements in free 00067 //list 00068 int maxFreeListLength; //max size of free structures 00069 //to keep 00070 int freeListLength; //current size of free list 00071 00072 }FreeList; 00073 00074 /**************************************************************************** 00075 * Function: FreeListInit 00076 * 00077 * Description: 00078 * Initializes Free List. Must be called first. 00079 * And only once for FreeList. 00080 * Parameters: 00081 * free_list - must be valid, non null, pointer to a linked list. 00082 * size_t - size of elements to store in free list 00083 * maxFreeListSize - max size that the free list can grow to 00084 * before returning memory to O.S. 00085 * Returns: 00086 * 0 on success. Nonzero on failure. 00087 * Always returns 0. 00088 *****************************************************************************/ 00089 int FreeListInit(FreeList *free_list, 00090 size_t elementSize, 00091 int maxFreeListSize); 00092 00093 /**************************************************************************** 00094 * Function: FreeListAlloc 00095 * 00096 * Description: 00097 * Allocates chunk of set size. 00098 * If a free item is available in the list, returnes the stored item. 00099 * Otherwise calls the O.S. to allocate memory. 00100 * Parameters: 00101 * free_list - must be valid, non null, pointer to a linked list. 00102 * Returns: 00103 * Non NULL on success. NULL on failure. 00104 *****************************************************************************/ 00105 void * FreeListAlloc (FreeList *free_list); 00106 00107 /**************************************************************************** 00108 * Function: FreeListFree 00109 * 00110 * Description: 00111 * Returns an item to the Free List. 00112 * If the free list is smaller than the max size than 00113 * adds the item to the free list. 00114 * Otherwise returns the item to the O.S. 00115 * Parameters: 00116 * free_list - must be valid, non null, pointer to a linked list. 00117 * Returns: 00118 * 0 on success. Nonzero on failure. 00119 * Always returns 0. 00120 *****************************************************************************/ 00121 int FreeListFree (FreeList *free_list,void * element); 00122 00123 /**************************************************************************** 00124 * Function: FreeListDestroy 00125 * 00126 * Description: 00127 * Releases the resources stored with the free list. 00128 * Parameters: 00129 * free_list - must be valid, non null, pointer to a linked list. 00130 * Returns: 00131 * 0 on success. Nonzero on failure. 00132 * Always returns 0. 00133 *****************************************************************************/ 00134 int FreeListDestroy (FreeList *free_list); 00135 00136 00137 #ifdef __cplusplus 00138 } 00139 #endif 00140 00141 #endif // FREE_LIST_H
1.6.1