summaryrefslogtreecommitdiff
path: root/hw/darwin/quartz/xpr/x-list.c
blob: 17843362a655e337840641592ba1dbd7dbb1e792 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* x-list.c
   $Id$

   Copyright (c) 2002 Apple Computer, Inc. All rights reserved.

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation files
   (the "Software"), to deal in the Software without restriction,
   including without limitation the rights to use, copy, modify, merge,
   publish, distribute, sublicense, and/or sell copies of the Software,
   and to permit persons to whom the Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
   HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.

   Except as contained in this notice, the name(s) of the above
   copyright holders shall not be used in advertising or otherwise to
   promote the sale, use or other dealings in this Software without
   prior written authorization. */
/* $XFree86: xc/programs/Xserver/hw/darwin/quartz/xpr/x-list.c,v 1.1 2003/04/30 23:15:42 torrey Exp $ */

#include "x-list.h"
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>

/* Allocate in ~4k blocks */
#define NODES_PER_BLOCK 508

typedef struct x_list_block_struct x_list_block;

struct x_list_block_struct {
    x_list l[NODES_PER_BLOCK];
};

static x_list *freelist;

static pthread_mutex_t freelist_lock = PTHREAD_MUTEX_INITIALIZER;

static inline void
list_free_1 (x_list *node)
{
    node->next = freelist;
    freelist = node;
}

X_EXTERN void
X_PFX (list_free_1) (x_list *node)
{
    assert (node != NULL);

    pthread_mutex_lock (&freelist_lock);

    list_free_1 (node);

    pthread_mutex_unlock (&freelist_lock);
}

X_EXTERN void
X_PFX (list_free) (x_list *lst)
{
    x_list *next;

    pthread_mutex_lock (&freelist_lock);

    for (; lst != NULL; lst = next)
    {
	next = lst->next;
	list_free_1 (lst);
    }

    pthread_mutex_unlock (&freelist_lock);
}

X_EXTERN x_list *
X_PFX (list_prepend) (x_list *lst, void *data)
{
    x_list *node;

    pthread_mutex_lock (&freelist_lock);

    if (freelist == NULL)
    {
	x_list_block *b;
	int i;

	b = malloc (sizeof (x_list_block));

	for (i = 0; i < NODES_PER_BLOCK - 1; i++)
	    b->l[i].next = &(b->l[i+1]);
	b->l[i].next = NULL;

	freelist = b->l;
    }

    node = freelist;
    freelist = node->next;

    pthread_mutex_unlock (&freelist_lock);

    node->next = lst;
    node->data = data;

    return node;
}

X_EXTERN x_list *
X_PFX (list_append) (x_list *lst, void *data)
{
    x_list *head = lst;

    if (lst == NULL)
	return X_PFX (list_prepend) (NULL, data);

    while (lst->next != NULL)
	lst = lst->next;

    lst->next = X_PFX (list_prepend) (NULL, data);

    return head;
}

X_EXTERN x_list *
X_PFX (list_reverse) (x_list *lst)
{
    x_list *head = NULL, *next;
    
    while (lst != NULL)
    {
	next = lst->next;
	lst->next = head;
	head = lst;
	lst = next;
    }

    return head;
}

X_EXTERN x_list *
X_PFX (list_find) (x_list *lst, void *data)
{
    for (; lst != NULL; lst = lst->next)
    {
	if (lst->data == data)
	    return lst;
    }

    return NULL;
}

X_EXTERN x_list *
X_PFX (list_nth) (x_list *lst, int n)
{
    while (n-- > 0 && lst != NULL)
	lst = lst->next;

    return lst;
}

X_EXTERN x_list *
X_PFX (list_filter) (x_list *lst,
		     int (*pred) (void *item, void *data), void *data)
{
    x_list *ret = NULL, *node;

    for (node = lst; node != NULL; node = node->next)
    {
	if ((*pred) (node->data, data))
	    ret = X_PFX (list_prepend) (ret, node->data);
    }

    return X_PFX (list_reverse) (ret);
}

X_EXTERN x_list *
X_PFX (list_map) (x_list *lst,
		  void *(*fun) (void *item, void *data), void *data)
{
    x_list *ret = NULL, *node;

    for (node = lst; node != NULL; node = node->next)
    {
	X_PFX (list_prepend) (ret, fun (node->data, data));
    }

    return X_PFX (list_reverse) (ret);
}

X_EXTERN x_list *
X_PFX (list_copy) (x_list *lst)
{
    x_list *copy = NULL;

    for (; lst != NULL; lst = lst->next)
    {
	copy = X_PFX (list_prepend) (copy, lst->data);
    }

    return X_PFX (list_reverse) (copy);
}

X_EXTERN x_list *
X_PFX (list_remove) (x_list *lst, void *data)
{
    x_list **ptr, *node;

    for (ptr = &lst; *ptr != NULL;)
    {
	node = *ptr;

	if (node->data == data)
	{
	    *ptr = node->next;
	    X_PFX (list_free_1) (node);
	}
	else
	    ptr = &((*ptr)->next);
    }

    return lst;
}

X_EXTERN unsigned int
X_PFX (list_length) (x_list *lst)
{
    unsigned int n;

    n = 0;
    for (; lst != NULL; lst = lst->next)
	n++;

    return n;
}

X_EXTERN void
X_PFX (list_foreach) (x_list *lst,
		      void (*fun) (void *data, void *user_data),
		      void *user_data)
{
    for (; lst != NULL; lst = lst->next)
    {
	(*fun) (lst->data, user_data);
    }
}

static x_list *
list_sort_1 (x_list *lst, int length,
	     int (*less) (const void *, const void *))
{
    x_list *mid, *ptr;
    x_list *out_head, *out;
    int mid_point, i;

    /* This is a standard (stable) list merge sort */

    if (length < 2)
	return lst;

    /* Calculate the halfway point. Split the list into two sub-lists. */

    mid_point = length / 2;
    ptr = lst;
    for (i = mid_point - 1; i > 0; i--)
        ptr = ptr->next;
    mid = ptr->next;
    ptr->next = NULL;

    /* Sort each sub-list. */

    lst = list_sort_1 (lst, mid_point, less);
    mid = list_sort_1 (mid, length - mid_point, less);

    /* Then merge them back together. */

    assert (lst != NULL && mid != NULL);

    if ((*less) (mid->data, lst->data))
	out = out_head = mid, mid = mid->next;
    else
	out = out_head = lst, lst = lst->next;

    while (lst != NULL && mid != NULL)
    {
        if ((*less) (mid->data, lst->data))
	    out = out->next = mid, mid = mid->next;
        else
	    out = out->next = lst, lst = lst->next;
    }

    if (lst != NULL)
        out->next = lst;
    else
        out->next = mid;

    return out_head;
}

X_EXTERN x_list *
X_PFX (list_sort) (x_list *lst, int (*less) (const void *, const void *))
{
    int length;

    length = X_PFX (list_length) (lst);

    return list_sort_1 (lst, length, less);
}