blob: d89b357f67a885f7a394ccbac0b713a8bf468a8e [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
5#include <asn_types.h> /* for MALLOC/REALLOC/FREEMEM */
6#include <asn_SET_OF.h>
7#include <errno.h>
8
9typedef A_SET_OF(void) asn_set;
10
11/*
12 * Add another element into the set.
13 */
14int
15asn_set_add(void *asn_set_of_x, void *ptr) {
16 asn_set *as = asn_set_of_x;
17
18 if(as == 0 || ptr == 0) {
19 errno = EINVAL; /* Invalid arguments */
20 return -1;
21 }
22
23 /*
24 * Make sure there's enough space to insert an element.
25 */
26 if(as->count == as->size) {
27 int _newsize = as->size ? (as->size << 1) : 4;
28 void *_new_arr;
29 _new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0]));
30 if(_new_arr) {
31 as->array = _new_arr;
32 as->size = _newsize;
33 } else {
34 /* ENOMEM */
35 return -1;
36 }
37 }
38
39 as->array[as->count++] = ptr;
40
41 return 0;
42}
43
44void
45asn_set_del(void *asn_set_of_x, int number, int _do_free) {
46 asn_set *as = asn_set_of_x;
47
48 if(as) {
49 void *ptr;
50 if(number < 0 || number >= as->count)
51 return;
52
53 if(_do_free && as->free) {
54 ptr = as->array[number];
55 } else {
56 ptr = 0;
57 }
58
59 as->array[number] = as->array[--as->count];
60
61 /*
62 * Invoke the third-party function only when the state
63 * of the parent structure is consistent.
64 */
65 if(ptr) as->free(ptr);
66 }
67}
68
69/*
70 * Free the contents of the set, do not free the set itself.
71 */
72void
73asn_set_empty(void *asn_set_of_x) {
74 asn_set *as = asn_set_of_x;
75
76 if(as) {
77 if(as->array) {
78 if(as->free) {
79 while(as->count--)
80 as->free(as->array[as->count]);
81 }
82 free(as->array);
vlm22366632004-07-22 12:17:10 +000083 as->array = 0;
vlmfa67ddc2004-06-03 03:38:44 +000084 }
85 as->count = 0;
86 as->size = 0;
87 }
88
89}
90