Moving in e2sim originally from it/test/simulators
[sim/e2-interface.git] / e2sim / ASN1c / asn_SET_OF.c
1 /*****************************************************************************
2 #                                                                            *
3 # Copyright 2019 AT&T Intellectual Property                                  *
4 #                                                                            *
5 # Licensed under the Apache License, Version 2.0 (the "License");            *
6 # you may not use this file except in compliance with the License.           *
7 # You may obtain a copy of the License at                                    *
8 #                                                                            *
9 #      http://www.apache.org/licenses/LICENSE-2.0                            *
10 #                                                                            *
11 # Unless required by applicable law or agreed to in writing, software        *
12 # distributed under the License is distributed on an "AS IS" BASIS,          *
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
14 # See the License for the specific language governing permissions and        *
15 # limitations under the License.                                             *
16 #                                                                            *
17 ******************************************************************************/
18
19 /*-
20  * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
21  * Redistribution and modifications are permitted subject to BSD license.
22  */
23 #include <asn_internal.h>
24 #include <asn_SET_OF.h>
25 #include <errno.h>
26
27 /*
28  * Add another element into the set.
29  */
30 int
31 asn_set_add(void *asn_set_of_x, void *ptr) {
32         asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
33
34         if(as == 0 || ptr == 0) {
35                 errno = EINVAL;         /* Invalid arguments */
36                 return -1;
37         }
38
39         /*
40          * Make sure there's enough space to insert an element.
41          */
42         if(as->count == as->size) {
43                 int _newsize = as->size ? (as->size << 1) : 4;
44                 void *_new_arr;
45                 _new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0]));
46                 if(_new_arr) {
47                         as->array = (void **)_new_arr;
48                         as->size = _newsize;
49                 } else {
50                         /* ENOMEM */
51                         return -1;
52                 }
53         }
54
55         as->array[as->count++] = ptr;
56
57         return 0;
58 }
59
60 void
61 asn_set_del(void *asn_set_of_x, int number, int _do_free) {
62         asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
63
64         if(as) {
65                 void *ptr;
66                 if(number < 0 || number >= as->count)
67                         return;
68
69                 if(_do_free && as->free) {
70                         ptr = as->array[number];
71                 } else {
72                         ptr = 0;
73                 }
74
75                 as->array[number] = as->array[--as->count];
76
77                 /*
78                  * Invoke the third-party function only when the state
79                  * of the parent structure is consistent.
80                  */
81                 if(ptr) as->free(ptr);
82         }
83 }
84
85 /*
86  * Free the contents of the set, do not free the set itself.
87  */
88 void
89 asn_set_empty(void *asn_set_of_x) {
90         asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
91
92         if(as) {
93                 if(as->array) {
94                         if(as->free) {
95                                 while(as->count--)
96                                         as->free(as->array[as->count]);
97                         }
98                         FREEMEM(as->array);
99                         as->array = 0;
100                 }
101                 as->count = 0;
102                 as->size = 0;
103         }
104
105 }
106