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