Home | History | Annotate | Line # | Download | only in src
      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 #ifdef HAVE_CONFIG_H
     29 #include "config.h"
     30 #endif
     31 
     32 #include <stdlib.h>
     33 
     34 #include "xcb.h"
     35 #include "xcbint.h"
     36 
     37 typedef struct node {
     38     struct node *next;
     39     uint64_t key;
     40     void *data;
     41 } node;
     42 
     43 struct _xcb_map {
     44     node *head;
     45     node **tail;
     46 };
     47 
     48 /* Private interface */
     49 
     50 _xcb_map *_xcb_map_new(void)
     51 {
     52     _xcb_map *list;
     53     list = malloc(sizeof(_xcb_map));
     54     if(!list)
     55         return 0;
     56     list->head = 0;
     57     list->tail = &list->head;
     58     return list;
     59 }
     60 
     61 void _xcb_map_delete(_xcb_map *list, xcb_list_free_func_t do_free)
     62 {
     63     if(!list)
     64         return;
     65     while(list->head)
     66     {
     67         node *cur = list->head;
     68         if(do_free)
     69             do_free(cur->data);
     70         list->head = cur->next;
     71         free(cur);
     72     }
     73     free(list);
     74 }
     75 
     76 int _xcb_map_put(_xcb_map *list, uint64_t key, void *data)
     77 {
     78     node *cur = malloc(sizeof(node));
     79     if(!cur)
     80         return 0;
     81     cur->key = key;
     82     cur->data = data;
     83     cur->next = 0;
     84     *list->tail = cur;
     85     list->tail = &cur->next;
     86     return 1;
     87 }
     88 
     89 void *_xcb_map_remove(_xcb_map *list, uint64_t key)
     90 {
     91     node **cur;
     92     for(cur = &list->head; *cur; cur = &(*cur)->next)
     93         if((*cur)->key == key)
     94         {
     95             node *tmp = *cur;
     96             void *ret = (*cur)->data;
     97             *cur = (*cur)->next;
     98             if(!*cur)
     99                 list->tail = cur;
    100 
    101             free(tmp);
    102             return ret;
    103         }
    104     return 0;
    105 }
    106