blob: 5e268c076ae8b22862b7bad9edcaf0c9fb215e84 [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 */
vlm39ba4c42004-09-22 16:06:28 +00005#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00006#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) {
vlmda674682004-08-11 09:07:36 +000016 asn_set *as = (asn_set *)asn_set_of_x;
vlmfa67ddc2004-06-03 03:38:44 +000017
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) {
vlmda674682004-08-11 09:07:36 +000031 as->array = (void **)_new_arr;
vlmfa67ddc2004-06-03 03:38:44 +000032 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) {
vlmda674682004-08-11 09:07:36 +000046 asn_set *as = (asn_set *)asn_set_of_x;
vlmfa67ddc2004-06-03 03:38:44 +000047
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) {
vlmda674682004-08-11 09:07:36 +000074 asn_set *as = (asn_set *)asn_set_of_x;
vlmfa67ddc2004-06-03 03:38:44 +000075
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