libUPnP 1.8.0
FreeList.h
Go to the documentation of this file.
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  *
00030  ******************************************************************************/
00031 
00032 
00033 #ifndef FREE_LIST_H
00034 #define FREE_LIST_H
00035 
00036 
00042 #ifdef __cplusplus
00043 extern "C" {
00044 #endif
00045 
00046 
00047 #include "ithread.h"
00048 
00049 
00050 #include <errno.h>
00051 
00052 /****************************************************************************
00053  * Name: FreeListNode
00054  *
00055  *  Description:
00056  *      free list node. points to next free item.
00057  *      memory for node is borrowed from allocated items.
00058  *      Internal Use Only.
00059  *****************************************************************************/
00060 typedef struct FREELISTNODE
00061 {
00062         struct FREELISTNODE *next;
00063 } FreeListNode;
00064 
00065 
00066 /****************************************************************************
00067  * Name: FreeList
00068  *
00069  *  Description:
00070  *      Stores head and size of free list, as well as mutex for protection.
00071  *      Internal Use Only.
00072  *****************************************************************************/
00073 typedef struct FREELIST
00074 {
00075         FreeListNode *head;
00076         size_t element_size;
00077         int maxFreeListLength;
00078         int freeListLength;
00079         
00080 }FreeList;
00081 
00082 /****************************************************************************
00083  * Function: FreeListInit
00084  *
00085  *  Description:
00086  *      Initializes Free List. Must be called first.
00087  *      And only once for FreeList.
00088  *  Parameters:
00089  *      free_list  - must be valid, non null, pointer to a linked list.
00090  *      size_t -     size of elements to store in free list
00091  *      maxFreeListSize - max size that the free list can grow to
00092  *                        before returning memory to O.S.
00093  *  Returns:
00094  *      0 on success. Nonzero on failure.
00095  *      Always returns 0.
00096  *****************************************************************************/
00097 int FreeListInit(FreeList *free_list, 
00098                                  size_t elementSize, 
00099                                  int maxFreeListSize);
00100 
00101 /****************************************************************************
00102  * Function: FreeListAlloc
00103  *
00104  *  Description:
00105  *      Allocates chunk of set size.
00106  *      If a free item is available in the list, returnes the stored item.
00107  *      Otherwise calls the O.S. to allocate memory.
00108  *  Parameters:
00109  *      free_list  - must be valid, non null, pointer to a linked list.
00110  *  Returns:
00111  *      Non NULL on success. NULL on failure.
00112  *****************************************************************************/
00113 void * FreeListAlloc (FreeList *free_list);
00114 
00115 /****************************************************************************
00116  * Function: FreeListFree
00117  *
00118  *  Description:
00119  *      Returns an item to the Free List.
00120  *      If the free list is smaller than the max size than
00121  *      adds the item to the free list.
00122  *      Otherwise returns the item to the O.S.
00123  *  Parameters:
00124  *      free_list  - must be valid, non null, pointer to a linked list.
00125  *  Returns:
00126  *      0 on success. Nonzero on failure.
00127  *      Always returns 0.
00128  *****************************************************************************/
00129 int FreeListFree (FreeList *free_list,void * element);
00130 
00131 /****************************************************************************
00132  * Function: FreeListDestroy
00133  *
00134  *  Description:
00135  *      Releases the resources stored with the free list.
00136  *  Parameters:
00137  *      free_list  - must be valid, non null, pointer to a linked list.
00138  *  Returns:
00139  *      0 on success. Nonzero on failure.
00140  *      Always returns 0.
00141  *****************************************************************************/
00142 int FreeListDestroy (FreeList *free_list);
00143 
00144 
00145 #ifdef __cplusplus
00146 }
00147 #endif
00148 
00149 #endif /* FREE_LIST_H */
00150