source: libabac/abac_list.c @ 7764378

abac0-leak
Last change on this file since 7764378 was 15200be, checked in by Mike Ryan <mikeryan@…>, 14 years ago

move libabac into its own directory

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <stdlib.h>
2
3#include "abac_list.h"
4#include "abac_util.h"
5
6abac_list_t *abac_list_new(void) {
7    abac_list_t *ret = abac_xmalloc(sizeof(abac_list_t));
8    ret->elts = NULL;
9    ret->size = 0;
10    return ret;
11}
12
13void abac_list_add(abac_list_t *list, void *elt) {
14    abac_list_element_t *new_element = abac_xmalloc(sizeof(abac_list_element_t));
15
16    new_element->ptr = elt;
17    DL_APPEND(list->elts, new_element);
18    ++list->size;
19}
20
21int abac_list_remove(abac_list_t *list, void *elt) {
22    abac_list_element_t *cur;
23
24    // iterate the list, remove the item if we find it
25    DL_FOREACH(list->elts, cur) {
26        if (cur->ptr == elt) {
27            DL_DELETE(list->elts, cur);
28            free(cur);
29            --list->size;
30            return 1;
31        }
32    }
33
34    // reutrn false if we don't
35    return 0;
36}
37
38int abac_list_size(abac_list_t *list) {
39    return list->size;
40}
41
42void abac_list_free(abac_list_t *list) {
43    abac_list_element_t *elt, *tmp;
44
45    // free everthing in the list
46    DL_FOREACH_SAFE(list->elts, elt, tmp) {
47        DL_DELETE(list->elts, elt);
48        free(elt);
49    }
50
51    // free the list
52    free(list);
53}
Note: See TracBrowser for help on using the repository browser.