xcb_list.c revision 602e473d
1/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * Except as contained in this notice, the names of the authors or their
21 * institutions shall not be used in advertising or otherwise to promote the
22 * sale, use or other dealings in this Software without prior written
23 * authorization from the authors.
24 */
25
26/* A generic implementation of a list of void-pointers. */
27
28#include <stdlib.h>
29
30#include "xcb.h"
31#include "xcbint.h"
32
33typedef struct node {
34    struct node *next;
35    unsigned int key;
36    void *data;
37} node;
38
39struct _xcb_map {
40    node *head;
41    node **tail;
42};
43
44/* Private interface */
45
46_xcb_map *_xcb_map_new()
47{
48    _xcb_map *list;
49    list = malloc(sizeof(_xcb_map));
50    if(!list)
51        return 0;
52    list->head = 0;
53    list->tail = &list->head;
54    return list;
55}
56
57void _xcb_map_delete(_xcb_map *list, xcb_list_free_func_t do_free)
58{
59    if(!list)
60        return;
61    while(list->head)
62    {
63        node *cur = list->head;
64        if(do_free)
65            do_free(cur->data);
66        list->head = cur->next;
67        free(cur);
68    }
69    free(list);
70}
71
72int _xcb_map_put(_xcb_map *list, unsigned int key, void *data)
73{
74    node *cur = malloc(sizeof(node));
75    if(!cur)
76        return 0;
77    cur->key = key;
78    cur->data = data;
79    cur->next = 0;
80    *list->tail = cur;
81    list->tail = &cur->next;
82    return 1;
83}
84
85void *_xcb_map_remove(_xcb_map *list, unsigned int key)
86{
87    node **cur;
88    for(cur = &list->head; *cur; cur = &(*cur)->next)
89        if((*cur)->key == key)
90        {
91            node *tmp = *cur;
92            void *ret = (*cur)->data;
93            *cur = (*cur)->next;
94            if(!*cur)
95                list->tail = cur;
96
97            free(tmp);
98            return ret;
99        }
100    return 0;
101}
102