Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <asn_SET_OF.h> |
| 7 | #include <errno.h> |
| 8 | |
| 9 | typedef A_SET_OF(void) asn_set; |
| 10 | |
| 11 | /* |
| 12 | * Add another element into the set. |
| 13 | */ |
| 14 | int |
| 15 | asn_set_add(void *asn_set_of_x, void *ptr) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 16 | asn_set *as = (asn_set *)asn_set_of_x; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 31 | as->array = (void **)_new_arr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | |
| 44 | void |
| 45 | asn_set_del(void *asn_set_of_x, int number, int _do_free) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 46 | asn_set *as = (asn_set *)asn_set_of_x; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | */ |
| 72 | void |
| 73 | asn_set_empty(void *asn_set_of_x) { |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 74 | asn_set *as = (asn_set *)asn_set_of_x; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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); |
Lev Walkin | 1473c6d | 2004-07-22 12:17:10 +0000 | [diff] [blame] | 83 | as->array = 0; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | } |
| 85 | as->count = 0; |
| 86 | as->size = 0; |
| 87 | } |
| 88 | |
| 89 | } |
| 90 | |