libUPnP  1.8.0
FreeList.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither name of Intel Corporation nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  ******************************************************************************/
31 
32 
33 #ifndef FREE_LIST_H
34 #define FREE_LIST_H
35 
36 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 
47 #include "ithread.h"
48 
49 
50 #include <errno.h>
51 
52 /****************************************************************************
53  * Name: FreeListNode
54  *
55  * Description:
56  * free list node. points to next free item.
57  * memory for node is borrowed from allocated items.
58  * Internal Use Only.
59  *****************************************************************************/
60 typedef struct FREELISTNODE
61 {
62  struct FREELISTNODE *next;
63 } FreeListNode;
64 
65 
66 /****************************************************************************
67  * Name: FreeList
68  *
69  * Description:
70  * Stores head and size of free list, as well as mutex for protection.
71  * Internal Use Only.
72  *****************************************************************************/
73 typedef struct FREELIST
74 {
75  FreeListNode *head;
76  size_t element_size;
77  int maxFreeListLength;
78  int freeListLength;
79 
80 }FreeList;
81 
82 /****************************************************************************
83  * Function: FreeListInit
84  *
85  * Description:
86  * Initializes Free List. Must be called first.
87  * And only once for FreeList.
88  * Parameters:
89  * free_list - must be valid, non null, pointer to a linked list.
90  * size_t - size of elements to store in free list
91  * maxFreeListSize - max size that the free list can grow to
92  * before returning memory to O.S.
93  * Returns:
94  * 0 on success. Nonzero on failure.
95  * Always returns 0.
96  *****************************************************************************/
97 int FreeListInit(FreeList *free_list,
98  size_t elementSize,
99  int maxFreeListSize);
100 
101 /****************************************************************************
102  * Function: FreeListAlloc
103  *
104  * Description:
105  * Allocates chunk of set size.
106  * If a free item is available in the list, returnes the stored item.
107  * Otherwise calls the O.S. to allocate memory.
108  * Parameters:
109  * free_list - must be valid, non null, pointer to a linked list.
110  * Returns:
111  * Non NULL on success. NULL on failure.
112  *****************************************************************************/
113 void * FreeListAlloc (FreeList *free_list);
114 
115 /****************************************************************************
116  * Function: FreeListFree
117  *
118  * Description:
119  * Returns an item to the Free List.
120  * If the free list is smaller than the max size than
121  * adds the item to the free list.
122  * Otherwise returns the item to the O.S.
123  * Parameters:
124  * free_list - must be valid, non null, pointer to a linked list.
125  * Returns:
126  * 0 on success. Nonzero on failure.
127  * Always returns 0.
128  *****************************************************************************/
129 int FreeListFree (FreeList *free_list,void * element);
130 
131 /****************************************************************************
132  * Function: FreeListDestroy
133  *
134  * Description:
135  * Releases the resources stored with the free list.
136  * Parameters:
137  * free_list - must be valid, non null, pointer to a linked list.
138  * Returns:
139  * 0 on success. Nonzero on failure.
140  * Always returns 0.
141  *****************************************************************************/
142 int FreeListDestroy (FreeList *free_list);
143 
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif /* FREE_LIST_H */
150