2003-11-25 20:29:01 +01:00
|
|
|
/* 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. */
|
2007-02-16 13:37:38 +01:00
|
|
|
#ifdef HAVE_XORG_CONFIG_H
|
|
|
|
#include <xorg-config.h>
|
|
|
|
#endif
|
2003-11-25 20:29:01 +01:00
|
|
|
#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)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
next = lst->next;
|
|
|
|
list_free_1 (lst);
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
x_list_block *b;
|
|
|
|
int i;
|
2003-11-25 20:29:01 +01:00
|
|
|
|
2004-09-17 23:57:26 +02:00
|
|
|
b = malloc (sizeof (x_list_block));
|
2003-11-25 20:29:01 +01:00
|
|
|
|
2004-09-17 23:57:26 +02:00
|
|
|
for (i = 0; i < NODES_PER_BLOCK - 1; i++)
|
|
|
|
b->l[i].next = &(b->l[i+1]);
|
|
|
|
b->l[i].next = NULL;
|
2003-11-25 20:29:01 +01:00
|
|
|
|
2004-09-17 23:57:26 +02:00
|
|
|
freelist = b->l;
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2004-09-17 23:57:26 +02:00
|
|
|
return X_PFX (list_prepend) (NULL, data);
|
2003-11-25 20:29:01 +01:00
|
|
|
|
|
|
|
while (lst->next != NULL)
|
2004-09-17 23:57:26 +02:00
|
|
|
lst = lst->next;
|
2003-11-25 20:29:01 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
next = lst->next;
|
|
|
|
lst->next = head;
|
|
|
|
head = lst;
|
|
|
|
lst = next;
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN x_list *
|
|
|
|
X_PFX (list_find) (x_list *lst, void *data)
|
|
|
|
{
|
|
|
|
for (; lst != NULL; lst = lst->next)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
if (lst->data == data)
|
|
|
|
return lst;
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN x_list *
|
|
|
|
X_PFX (list_nth) (x_list *lst, int n)
|
|
|
|
{
|
|
|
|
while (n-- > 0 && lst != NULL)
|
2004-09-17 23:57:26 +02:00
|
|
|
lst = lst->next;
|
|
|
|
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN x_list *
|
|
|
|
X_PFX (list_pop) (x_list *lst, void **data_ret)
|
|
|
|
{
|
|
|
|
void *data = NULL;
|
|
|
|
|
|
|
|
if (lst != NULL)
|
|
|
|
{
|
|
|
|
x_list *tem = lst;
|
|
|
|
data = lst->data;
|
|
|
|
lst = lst->next;
|
|
|
|
X_PFX (list_free_1) (tem);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data_ret != NULL)
|
|
|
|
*data_ret = data;
|
2003-11-25 20:29:01 +01:00
|
|
|
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN x_list *
|
|
|
|
X_PFX (list_filter) (x_list *lst,
|
2004-09-17 23:57:26 +02:00
|
|
|
int (*pred) (void *item, void *data), void *data)
|
2003-11-25 20:29:01 +01:00
|
|
|
{
|
|
|
|
x_list *ret = NULL, *node;
|
|
|
|
|
|
|
|
for (node = lst; node != NULL; node = node->next)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
if ((*pred) (node->data, data))
|
|
|
|
ret = X_PFX (list_prepend) (ret, node->data);
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return X_PFX (list_reverse) (ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN x_list *
|
|
|
|
X_PFX (list_map) (x_list *lst,
|
2004-09-17 23:57:26 +02:00
|
|
|
void *(*fun) (void *item, void *data), void *data)
|
2003-11-25 20:29:01 +01:00
|
|
|
{
|
|
|
|
x_list *ret = NULL, *node;
|
|
|
|
|
|
|
|
for (node = lst; node != NULL; node = node->next)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
X_PFX (list_prepend) (ret, fun (node->data, data));
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
copy = X_PFX (list_prepend) (copy, lst->data);
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
node = *ptr;
|
|
|
|
|
|
|
|
if (node->data == data)
|
|
|
|
{
|
|
|
|
*ptr = node->next;
|
|
|
|
X_PFX (list_free_1) (node);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ptr = &((*ptr)->next);
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return lst;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN unsigned int
|
|
|
|
X_PFX (list_length) (x_list *lst)
|
|
|
|
{
|
|
|
|
unsigned int n;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
for (; lst != NULL; lst = lst->next)
|
2004-09-17 23:57:26 +02:00
|
|
|
n++;
|
2003-11-25 20:29:01 +01:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
X_EXTERN void
|
|
|
|
X_PFX (list_foreach) (x_list *lst,
|
2004-09-17 23:57:26 +02:00
|
|
|
void (*fun) (void *data, void *user_data),
|
|
|
|
void *user_data)
|
2003-11-25 20:29:01 +01:00
|
|
|
{
|
|
|
|
for (; lst != NULL; lst = lst->next)
|
|
|
|
{
|
2004-09-17 23:57:26 +02:00
|
|
|
(*fun) (lst->data, user_data);
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static x_list *
|
|
|
|
list_sort_1 (x_list *lst, int length,
|
2004-09-17 23:57:26 +02:00
|
|
|
int (*less) (const void *, const void *))
|
2003-11-25 20:29:01 +01:00
|
|
|
{
|
|
|
|
x_list *mid, *ptr;
|
|
|
|
x_list *out_head, *out;
|
|
|
|
int mid_point, i;
|
|
|
|
|
|
|
|
/* This is a standard (stable) list merge sort */
|
|
|
|
|
|
|
|
if (length < 2)
|
2004-09-17 23:57:26 +02:00
|
|
|
return lst;
|
2003-11-25 20:29:01 +01:00
|
|
|
|
|
|
|
/* 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))
|
2004-09-17 23:57:26 +02:00
|
|
|
out = out_head = mid, mid = mid->next;
|
2003-11-25 20:29:01 +01:00
|
|
|
else
|
2004-09-17 23:57:26 +02:00
|
|
|
out = out_head = lst, lst = lst->next;
|
2003-11-25 20:29:01 +01:00
|
|
|
|
|
|
|
while (lst != NULL && mid != NULL)
|
|
|
|
{
|
|
|
|
if ((*less) (mid->data, lst->data))
|
2004-09-17 23:57:26 +02:00
|
|
|
out = out->next = mid, mid = mid->next;
|
2003-11-25 20:29:01 +01:00
|
|
|
else
|
2004-09-17 23:57:26 +02:00
|
|
|
out = out->next = lst, lst = lst->next;
|
2003-11-25 20:29:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|