|
Packit |
071ada |
/* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
|
|
Packit |
071ada |
*
|
|
Packit |
071ada |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
Packit |
071ada |
* copy of this software and associated documentation files (the "Software"),
|
|
Packit |
071ada |
* to deal in the Software without restriction, including without limitation
|
|
Packit |
071ada |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
Packit |
071ada |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
Packit |
071ada |
* Software is furnished to do so, subject to the following conditions:
|
|
Packit |
071ada |
*
|
|
Packit |
071ada |
* The above copyright notice and this permission notice shall be included in
|
|
Packit |
071ada |
* all copies or substantial portions of the Software.
|
|
Packit |
071ada |
*
|
|
Packit |
071ada |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit |
071ada |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit |
071ada |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit |
071ada |
* AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
Packit |
071ada |
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
Packit |
071ada |
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
Packit |
071ada |
*
|
|
Packit |
071ada |
* Except as contained in this notice, the names of the authors or their
|
|
Packit |
071ada |
* institutions shall not be used in advertising or otherwise to promote the
|
|
Packit |
071ada |
* sale, use or other dealings in this Software without prior written
|
|
Packit |
071ada |
* authorization from the authors.
|
|
Packit |
071ada |
*/
|
|
Packit |
071ada |
|
|
Packit |
071ada |
/* A generic implementation of a list of void-pointers. */
|
|
Packit |
071ada |
|
|
Packit |
071ada |
#ifdef HAVE_CONFIG_H
|
|
Packit |
071ada |
#include "config.h"
|
|
Packit |
071ada |
#endif
|
|
Packit |
071ada |
|
|
Packit |
071ada |
#include <stdlib.h>
|
|
Packit |
071ada |
|
|
Packit |
071ada |
#include "xcb.h"
|
|
Packit |
071ada |
#include "xcbint.h"
|
|
Packit |
071ada |
|
|
Packit |
071ada |
typedef struct node {
|
|
Packit |
071ada |
struct node *next;
|
|
Packit |
071ada |
unsigned int key;
|
|
Packit |
071ada |
void *data;
|
|
Packit |
071ada |
} node;
|
|
Packit |
071ada |
|
|
Packit |
071ada |
struct _xcb_map {
|
|
Packit |
071ada |
node *head;
|
|
Packit |
071ada |
node **tail;
|
|
Packit |
071ada |
};
|
|
Packit |
071ada |
|
|
Packit |
071ada |
/* Private interface */
|
|
Packit |
071ada |
|
|
Packit |
071ada |
_xcb_map *_xcb_map_new(void)
|
|
Packit |
071ada |
{
|
|
Packit |
071ada |
_xcb_map *list;
|
|
Packit |
071ada |
list = malloc(sizeof(_xcb_map));
|
|
Packit |
071ada |
if(!list)
|
|
Packit |
071ada |
return 0;
|
|
Packit |
071ada |
list->head = 0;
|
|
Packit |
071ada |
list->tail = &list->head;
|
|
Packit |
071ada |
return list;
|
|
Packit |
071ada |
}
|
|
Packit |
071ada |
|
|
Packit |
071ada |
void _xcb_map_delete(_xcb_map *list, xcb_list_free_func_t do_free)
|
|
Packit |
071ada |
{
|
|
Packit |
071ada |
if(!list)
|
|
Packit |
071ada |
return;
|
|
Packit |
071ada |
while(list->head)
|
|
Packit |
071ada |
{
|
|
Packit |
071ada |
node *cur = list->head;
|
|
Packit |
071ada |
if(do_free)
|
|
Packit |
071ada |
do_free(cur->data);
|
|
Packit |
071ada |
list->head = cur->next;
|
|
Packit |
071ada |
free(cur);
|
|
Packit |
071ada |
}
|
|
Packit |
071ada |
free(list);
|
|
Packit |
071ada |
}
|
|
Packit |
071ada |
|
|
Packit |
071ada |
int _xcb_map_put(_xcb_map *list, unsigned int key, void *data)
|
|
Packit |
071ada |
{
|
|
Packit |
071ada |
node *cur = malloc(sizeof(node));
|
|
Packit |
071ada |
if(!cur)
|
|
Packit |
071ada |
return 0;
|
|
Packit |
071ada |
cur->key = key;
|
|
Packit |
071ada |
cur->data = data;
|
|
Packit |
071ada |
cur->next = 0;
|
|
Packit |
071ada |
*list->tail = cur;
|
|
Packit |
071ada |
list->tail = &cur->next;
|
|
Packit |
071ada |
return 1;
|
|
Packit |
071ada |
}
|
|
Packit |
071ada |
|
|
Packit |
071ada |
void *_xcb_map_remove(_xcb_map *list, unsigned int key)
|
|
Packit |
071ada |
{
|
|
Packit |
071ada |
node **cur;
|
|
Packit |
071ada |
for(cur = &list->head; *cur; cur = &(*cur)->next)
|
|
Packit |
071ada |
if((*cur)->key == key)
|
|
Packit |
071ada |
{
|
|
Packit |
071ada |
node *tmp = *cur;
|
|
Packit |
071ada |
void *ret = (*cur)->data;
|
|
Packit |
071ada |
*cur = (*cur)->next;
|
|
Packit |
071ada |
if(!*cur)
|
|
Packit |
071ada |
list->tail = cur;
|
|
Packit |
071ada |
|
|
Packit |
071ada |
free(tmp);
|
|
Packit |
071ada |
return ret;
|
|
Packit |
071ada |
}
|
|
Packit |
071ada |
return 0;
|
|
Packit |
071ada |
}
|