aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils/reflist.c
blob: bc604cd3894706b9f5c7a82db9da92219a83e286 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Copyright (C) 2007-2008 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.
*/
#include "android/utils/reflist.h"
#include <stdlib.h>
#include <string.h>

void
areflist_setEmpty(ARefList*  l)
{
    if (l->iteration > 0) {
        /* deferred empty, set all items to NULL
         * to stop iterations */
        void**  items = areflist_items(l);
        memset(items, 0, l->count*sizeof(items[0]));
        l->iteration |= 1;
    } else {
        /* direct empty */
        if (l->max > 1) {
            free(l->u.items);
            l->max = 1;
        }
    }
    l->count = 0;
}

int
areflist_indexOf(ARefList*  l, void*  item)
{
    if (item) {
        void**  items = (l->max == 1) ? &l->u.item0 : l->u.items;
        void**  end   = items + l->count;
        void**  ii    = items;

        for ( ; ii < end; ii += 1 )
            if (*ii == item)
                return (ii - items);
    }
    return -1;
}

static void
areflist_grow(ARefList*  l, int  count)
{
    int   newcount = l->count + count;
    if (newcount > l->max) {
        int  oldmax = l->max == 1 ? 0 : l->max;
        int  newmax = oldmax;
        void**  olditems = l->max == 1 ? NULL : l->u.items;
        void**  newitems;

        while (oldmax < newcount)
            oldmax += (oldmax >> 1) + 4;

        newitems = realloc(olditems, newmax*sizeof(newitems[0]));

        l->u.items = newitems;
        l->max     = (uint16_t) newmax;
    }
}


void
areflist_add(ARefList*  l, void*  item)
{
    if (item) {
        void**  items;

        if (l->count >= l->max) {
            areflist_grow(l, 1);
        }
        items = areflist_items(l);
        items[l->count] = item;
        l->count += 1;
    }
}

void*
areflist_pop(ARefList*  l)
{
    void*   item = NULL;
    void**  items = areflist_items(l);

    if (l->count > 0) {
        if (l->iteration > 0) {
            /* deferred deletion */
            int  nn;
            for (nn = l->count-1; nn > 0; nn--) {
                item = items[nn];
                if (item != NULL) {
                    l->count -= 1;
                    return item;
                }
            }
        } else {
            /* normal pop */
            item = items[--l->count];
            if (l->count <= 0 && l->max > 1) {
                free(l->u.items);
                l->max = 1;
            }
        }
    }
    return item;
}

ABool
areflist_del(ARefList*  l, void*  item)
{
    if (item) {
        int  index = areflist_indexOf(l, item);
        if (index >= 0) {
            void** items = areflist_items(l);

            if (l->iteration > 0) {
                /* deferred deletion */
                items[index]  = NULL;
                l->iteration |= 1;
            } else {
                /* direct deletion */
                if (l->max > 1) {
                    memmove(items + index, items + index + 1, l->count - index - 1);
                    if (--l->count == 0) {
                        free(l->u.items);
                        l->max = 1;
                    }
                } else {
                    l->u.item0 = NULL;
                    l->count   = 0;
                }
            }
            return 1;
        }
    }
    return 0;
}

void
_areflist_remove_deferred(ARefList*  l)
{
    if (l->iteration & 1) {
        /* remove all NULL elements from the array */
        void**  items = areflist_items(l);
        int     rr = 0;
        int     ww = 0;
        for ( ; rr < l->count; rr++ ) {
            if (items[rr] != NULL)
                items[ww++] = items[rr];
        }
        l->count = (int16_t) ww;

        /* if the list is empty, release its array */
        if (l->count == 0 && l->max > 1) {
            free(l->u.items);
            l->max = 1;
        }
    }
    l->iteration = 0;
}

void  
areflist_copy(ARefList*  dst, ARefList*  src)
{
    dst[0] = src[0];

    if (src->max > 1) {
        dst->u.items = malloc( dst->count*sizeof(void*) );
        dst->max     = dst->count;
    }
}

void*
areflist_get(ARefList*  l, int  n)
{
    if ((unsigned)n >= (unsigned)l->count)
        return NULL;

    if (l->max == 1)
        return l->u.item0;

    return l->u.items[n];
}

void**
areflist_at(ARefList*  l, int  n)
{
    void**  items;

    if ((unsigned)n >= (unsigned)l->count)
        return NULL;

    items = (l->max == 1) ? &l->u.item0 : l->u.items;

    return items + n;
}