aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils/list.h
blob: a4171aed41e76c3e25ec255d0d08f833bac75a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Copyright (C) 2011 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#ifndef _ANDROID_UTILS_LIST_H
#define _ANDROID_UTILS_LIST_H

#include <inttypes.h>

/* Encapsulates a double-linked, circular list.
 * The list is organized in the following way:
 * - List entries contain references to the next, and the previous entry in the
 *   list.
 * - The list is circular, i.e. the "last" list entry references the "list head"
 *   in its 'next' reference, and the "list head" references the "last" entry in
 *   its 'previous' reference.
 * - The list is empty if its 'next' and 'previous' references are addressing the
 *   head of the list.
 */
typedef struct ACList ACList;
struct ACList {
    /* Next entry in the list */
    ACList*  next;
    /* Previous entry in the list */
    ACList*  prev;
};

/* Initializes the list. */
AINLINED void
alist_init(ACList* list)
{
    list->next = list->prev = list;
}

/* Checks if the list is empty. */
AINLINED int
alist_is_empty(const ACList* list)
{
    return list->next == list;
}

/* Inserts an entry to the head of the list */
AINLINED void
alist_insert_head(ACList* list, ACList* entry)
{
    ACList* const next = list->next;
    entry->next = next;
    entry->prev = list;
    next->prev = entry;
    list->next = entry;
}
/* Inserts an entry to the tail of the list */
AINLINED void
alist_insert_tail(ACList* list, ACList* entry)
{
    ACList* const prev = list->prev;
    entry->next = list;
    entry->prev = prev;
    prev->next = entry;
    list->prev = entry;
}

/* Removes an entry from the list. NOTE: Entry must be in the list when this
 * routine is called. */
AINLINED void
alist_remove(ACList* entry)
{
    ACList* const next = entry->next;
    ACList* const prev = entry->prev;
    prev->next = next;
    next->prev = prev;
    entry->next = entry->prev = entry;
}

/* Returns an entry removed from the head of the list. If the list was empty,
 * this routine returns NULL. */
AINLINED ACList*
alist_remove_head(ACList* list)
{
    ACList* entry = NULL;
    if (!alist_is_empty(list)) {
        entry = list->next;
        list->next = entry->next;
        entry->next->prev = list;
        entry->next = entry->prev = entry;
    }
    return entry;
}

/* Returns an entry removed from the tail of the list. If the list was empty,
 * this routine returns NULL. */
AINLINED ACList*
alist_remove_tail(ACList* list)
{
    ACList* entry = NULL;
    if (!alist_is_empty(list)) {
        entry = list->prev;
        list->prev = entry->prev;
        entry->prev->next = list;
        entry->next = entry->prev = entry;
    }
    return entry;
}

#endif  /* _ANDROID_UTILS_LIST_H */