1 /*****************************************************************************
3 # Copyright 2019 AT&T Intellectual Property *
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 *
9 # http://www.apache.org/licenses/LICENSE-2.0 *
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. *
17 ******************************************************************************/
20 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
21 * Redistribution and modifications are permitted subject to BSD license.
23 #include <asn_internal.h>
24 #include <asn_SET_OF.h>
28 * Add another element into the set.
31 asn_set_add(void *asn_set_of_x, void *ptr) {
32 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
34 if(as == 0 || ptr == 0) {
35 errno = EINVAL; /* Invalid arguments */
40 * Make sure there's enough space to insert an element.
42 if(as->count == as->size) {
43 int _newsize = as->size ? (as->size << 1) : 4;
45 _new_arr = REALLOC(as->array, _newsize * sizeof(as->array[0]));
47 as->array = (void **)_new_arr;
55 as->array[as->count++] = ptr;
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);
66 if(number < 0 || number >= as->count)
69 if(_do_free && as->free) {
70 ptr = as->array[number];
75 as->array[number] = as->array[--as->count];
78 * Invoke the third-party function only when the state
79 * of the parent structure is consistent.
81 if(ptr) as->free(ptr);
86 * Free the contents of the set, do not free the set itself.
89 asn_set_empty(void *asn_set_of_x) {
90 asn_anonymous_set_ *as = _A_SET_FROM_VOID(asn_set_of_x);
96 as->free(as->array[as->count]);