Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / eglib / gqueue.c
1 /*
2  * gqueue.c: Queue
3  *
4  * Author:
5  *   Duncan Mak (duncan@novell.com)
6  *   Gonzalo Paniagua Javier (gonzalo@novell.com)
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  * 
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  * 
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Copyright (c) 2006-2009 Novell, Inc.
28  *
29  */
30
31 #include <stdio.h>
32 #include <glib.h>
33
34 gpointer
35 g_queue_pop_head (GQueue *queue)
36 {
37         gpointer result;
38         GList *old_head;
39
40         if (!queue || queue->length == 0)
41                 return NULL;
42
43         result = queue->head->data;
44         old_head = queue->head;
45         queue->head = old_head->next;
46         g_list_free_1 (old_head);
47
48         if (--queue->length)
49                 queue->head->prev = NULL;
50         else
51                 queue->tail = NULL;
52
53         return result;
54 }
55
56 gboolean
57 g_queue_is_empty (GQueue *queue)
58 {
59         if (!queue)
60                 return TRUE;
61         
62         return queue->length == 0;
63 }
64
65 void
66 g_queue_push_head (GQueue *queue, gpointer head)
67 {
68         if (!queue)
69                 return;
70         
71         queue->head = g_list_prepend (queue->head, head);
72         
73         if (!queue->tail)
74                 queue->tail = queue->head;
75
76         queue->length ++;
77 }
78
79 void
80 g_queue_push_tail (GQueue *queue, gpointer data)
81 {
82         if (!queue)
83                 return;
84
85         queue->tail = g_list_append (queue->tail, data);
86         if (queue->head == NULL)
87                 queue->head = queue->tail;
88         else
89                 queue->tail = queue->tail->next;
90         queue->length++;
91 }
92
93 GQueue *
94 g_queue_new (void)
95 {
96         return g_new0 (GQueue, 1);
97 }
98
99 void
100 g_queue_free (GQueue *queue)
101 {
102         if (!queue)
103                 return;
104         
105         g_list_free (queue->head);
106         g_free (queue);
107 }
108
109 void 
110 g_queue_foreach (GQueue *queue, GFunc func, gpointer user_data)
111 {
112         g_list_foreach (queue->head, func, user_data);
113 }