VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/list.c@ 49216

最後變更 在這個檔案從49216是 16915,由 vboxsync 提交於 16 年 前

crOpenGL: use chromium functions for malloc/free

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 5.5 KB
 
1#include "cr_list.h"
2#include "cr_error.h"
3#include "cr_mem.h"
4
5#if CR_TESTING_LIST
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#endif
10
11struct CRListIterator {
12 void *element;
13 CRListIterator *prev;
14 CRListIterator *next;
15};
16
17struct CRList {
18 CRListIterator *head;
19 CRListIterator *tail;
20 unsigned size;
21};
22
23CRList *crAllocList( void )
24{
25 CRList *l = crAlloc( sizeof( CRList ) );
26 CRASSERT( l );
27
28 l->head = crAlloc( sizeof( CRListIterator ) );
29 CRASSERT( l->head );
30
31 l->tail = crAlloc( sizeof( CRListIterator ) );
32 CRASSERT( l->tail );
33
34 l->head->prev = NULL;
35 l->head->next = l->tail;
36
37 l->tail->prev = l->head;
38 l->tail->next = NULL;
39
40 l->size = 0;
41
42 return l;
43}
44
45void crFreeList( CRList *l )
46{
47 CRListIterator *t1;
48
49 CRASSERT( l != NULL );
50 t1 = l->head;
51 while ( t1 != NULL )
52 {
53 CRListIterator *t2 = t1;
54 t1 = t1->next;
55 t2->prev = NULL;
56 t2->next = NULL;
57 t2->element = NULL;
58 crFree( t2 );
59 }
60 l->size = 0;
61 crFree( l );
62}
63
64unsigned crListSize( const CRList *l )
65{
66 return l->size;
67}
68
69int crListIsEmpty( const CRList *l )
70{
71 CRASSERT( l != NULL );
72 return l->size == 0;
73}
74
75void crListInsert( CRList *l, CRListIterator *iter, void *elem )
76{
77 CRListIterator *p;
78
79 CRASSERT( l != NULL );
80 CRASSERT( iter != NULL );
81 CRASSERT( iter != l->head );
82
83 p = crAlloc( sizeof( CRListIterator ) );
84 CRASSERT( p != NULL );
85 p->prev = iter->prev;
86 p->next = iter;
87 p->prev->next = p;
88 iter->prev = p;
89
90 p->element = elem;
91 l->size++;
92}
93
94void crListErase( CRList *l, CRListIterator *iter )
95{
96 CRASSERT( l != NULL );
97 CRASSERT( iter != NULL );
98 CRASSERT( iter != l->head );
99 CRASSERT( iter != l->tail );
100 CRASSERT( l->size > 0 );
101
102 iter->next->prev = iter->prev;
103 iter->prev->next = iter->next;
104
105 iter->prev = NULL;
106 iter->next = NULL;
107 iter->element = NULL;
108 crFree( iter );
109
110 l->size--;
111}
112
113void crListClear( CRList *l )
114{
115 CRASSERT( l != NULL );
116 while ( !crListIsEmpty( l ) )
117 {
118 crListPopFront( l );
119 }
120}
121
122void crListPushBack( CRList *l, void *elem )
123{
124 CRASSERT( l != NULL );
125 crListInsert( l, l->tail, elem );
126}
127
128void crListPushFront( CRList *l, void *elem )
129{
130 CRASSERT( l != NULL );
131 crListInsert( l, l->head->next, elem );
132}
133
134void crListPopBack( CRList *l )
135{
136 CRASSERT( l != NULL );
137 CRASSERT( l->size > 0 );
138 crListErase( l, l->tail->prev );
139}
140
141void crListPopFront( CRList *l )
142{
143 CRASSERT( l != NULL );
144 CRASSERT( l->size > 0 );
145 crListErase( l, l->head->next );
146}
147
148void *crListFront( CRList *l )
149{
150 CRASSERT( l != NULL );
151 CRASSERT( l->size > 0 );
152 CRASSERT( l->head != NULL );
153 CRASSERT( l->head->next != NULL );
154 return l->head->next->element;
155}
156
157void *crListBack( CRList *l )
158{
159 CRASSERT( l != NULL );
160 CRASSERT( l->size > 0 );
161 CRASSERT( l->tail != NULL );
162 CRASSERT( l->tail->prev != NULL );
163 return l->tail->prev->element;
164}
165
166CRListIterator *crListBegin( CRList *l )
167{
168 CRASSERT( l != NULL );
169 CRASSERT( l->head != NULL );
170 CRASSERT( l->head->next != NULL );
171 return l->head->next;
172}
173
174CRListIterator *crListEnd( CRList *l )
175{
176 CRASSERT( l != NULL );
177 CRASSERT( l->tail != NULL );
178 return l->tail;
179}
180
181CRListIterator *crListNext( CRListIterator *iter )
182{
183 CRASSERT( iter != NULL );
184 CRASSERT( iter->next != NULL );
185 return iter->next;
186}
187
188CRListIterator *crListPrev( CRListIterator *iter )
189{
190 CRASSERT( iter != NULL );
191 CRASSERT( iter->prev != NULL );
192 return iter->prev;
193}
194
195void *crListElement( CRListIterator *iter )
196{
197 CRASSERT( iter != NULL );
198 return iter->element;
199}
200
201CRListIterator *crListFind( CRList *l, void *element, CRListCompareFunc compare )
202{
203 CRListIterator *iter;
204
205 CRASSERT( l != NULL );
206 CRASSERT( compare );
207
208 for ( iter = crListBegin( l ); iter != crListEnd( l ); iter = crListNext( iter ) )
209 {
210 if ( compare( element, iter->element ) == 0 )
211 {
212 return iter;
213 }
214 }
215 return NULL;
216}
217
218void crListApply( CRList *l, CRListApplyFunc apply, void *arg )
219{
220 CRListIterator *iter;
221
222 CRASSERT( l != NULL );
223 for ( iter = crListBegin( l ); iter != crListEnd( l ); iter = crListNext( iter ) )
224 {
225 apply( iter->element, arg );
226 }
227}
228
229#if CR_TESTING_LIST
230
231static void printElement( void *elem, void *arg )
232{
233 char *s = elem;
234 FILE *fp = arg;
235
236 CRASSERT( s != NULL );
237 CRASSERT( fp != NULL );
238 fprintf( fp, "%s ", s );
239}
240
241static void printList( CRList *l )
242{
243 CRASSERT( l != NULL );
244 crListApply( l, printElement, stderr );
245 fprintf( stderr, "\n" );
246}
247
248static int elementCompare( void *a, void *b )
249{
250 return strcmp( a, b );
251}
252
253int main( void )
254{
255 char *names[] = { "a", "b", "c", "d", "e", "f" };
256 CRList *l;
257 CRListIterator *iter;
258 int i, n;
259
260 n = sizeof( names ) / sizeof( names[0] );
261 fprintf( stderr, "n=%d\n", n );
262
263 l = crAllocList( );
264 for ( i = 0; i < n; ++i )
265 {
266 crListPushBack( l, names[i] );
267 }
268 printList( l );
269
270 crListPushFront( l, "x" );
271 printList( l );
272
273 crListPushBack( l, "y" );
274 printList( l );
275
276 crListPopFront( l );
277 printList( l );
278
279 crListPopBack( l );
280 printList( l );
281
282 iter = crListFind( l, "c", elementCompare );
283 CRASSERT( iter != NULL );
284 crListInsert( l, iter, "z" );
285 printList( l );
286
287 iter = crListFind( l, "d", elementCompare );
288 CRASSERT( iter != NULL );
289 crListErase( l, iter );
290 printList( l );
291
292 crListClear( l );
293 printList( l );
294 fprintf( stderr, "size: %d\n", crListSize( l ) );
295 fprintf( stderr, "is empty: %d\n", crListIsEmpty( l ) );
296
297 crListPushBack( l, "w" );
298 crListPushBack( l, "t" );
299 crListPushBack( l, "c" );
300 printList( l );
301
302 fprintf( stderr, "front: %s\n", ( char * ) crListFront( l ) );
303 fprintf( stderr, "back: %s\n", ( char * ) crListBack( l ) );
304 fprintf( stderr, "size: %d\n", crListSize( l ) );
305 fprintf( stderr, "is empty: %d\n", crListIsEmpty( l ) );
306
307 crFreeList( l );
308 return 0;
309}
310
311#endif // CR_TESTING_LIST
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette