1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  * Copyright © 2011, 2012 Intel Corporation
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting documentation, and
9  * that the name of the copyright holders not be used in advertising or
10  * publicity pertaining to distribution of the software without specific,
11  * written prior permission.  The copyright holders make no representations
12  * about the suitability of this software for any purpose.  It is provided "as
13  * is" without express or implied warranty.
14  *
15  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21  * OF THIS SOFTWARE.
22  */
23 
24 /* This list implementation is based on the Wayland source code */
25 
26 #ifdef HAVE_CONFIG_H
27 #include "cogl-config.h"
28 #endif
29 
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "cogl-list.h"
34 
35 void
_cogl_list_init(CoglList * list)36 _cogl_list_init (CoglList *list)
37 {
38   list->prev = list;
39   list->next = list;
40 }
41 
42 void
_cogl_list_insert(CoglList * list,CoglList * elm)43 _cogl_list_insert (CoglList *list, CoglList *elm)
44 {
45   elm->prev = list;
46   elm->next = list->next;
47   list->next = elm;
48   elm->next->prev = elm;
49 }
50 
51 void
_cogl_list_remove(CoglList * elm)52 _cogl_list_remove (CoglList *elm)
53 {
54   elm->prev->next = elm->next;
55   elm->next->prev = elm->prev;
56   elm->next = NULL;
57   elm->prev = NULL;
58 }
59 
60 int
_cogl_list_length(CoglList * list)61 _cogl_list_length (CoglList *list)
62 {
63   CoglList *e;
64   int count;
65 
66   count = 0;
67   e = list->next;
68   while (e != list)
69     {
70       e = e->next;
71       count++;
72     }
73 
74   return count;
75 }
76 
77 int
_cogl_list_empty(CoglList * list)78 _cogl_list_empty (CoglList *list)
79 {
80   return list->next == list;
81 }
82 
83 void
_cogl_list_insert_list(CoglList * list,CoglList * other)84 _cogl_list_insert_list (CoglList *list,
85                         CoglList *other)
86 {
87   if (_cogl_list_empty (other))
88     return;
89 
90   other->next->prev = list;
91   other->prev->next = list->next;
92   list->next->prev = other->prev;
93   list->next = other->next;
94 }
95