/src/p11-kit/common/array.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2011 Collabora Ltd. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without |
5 | | * modification, are permitted provided that the following conditions |
6 | | * are met: |
7 | | * |
8 | | * * Redistributions of source code must retain the above |
9 | | * copyright notice, this list of conditions and the |
10 | | * following disclaimer. |
11 | | * * Redistributions in binary form must reproduce the |
12 | | * above copyright notice, this list of conditions and |
13 | | * the following disclaimer in the documentation and/or |
14 | | * other materials provided with the distribution. |
15 | | * * The names of contributors to this software may not be |
16 | | * used to endorse or promote products derived from this |
17 | | * software without specific prior written permission. |
18 | | * |
19 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
22 | | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
23 | | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
24 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
25 | | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
26 | | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
27 | | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
28 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
29 | | * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
30 | | * DAMAGE. |
31 | | */ |
32 | | |
33 | | #include "config.h" |
34 | | |
35 | | #include "array.h" |
36 | | #include "debug.h" |
37 | | |
38 | | #include <stdint.h> |
39 | | #include <stdlib.h> |
40 | | #include <string.h> |
41 | | |
42 | | static bool |
43 | | maybe_expand_array (p11_array *array, |
44 | | unsigned int length) |
45 | 0 | { |
46 | 0 | unsigned int new_allocated; |
47 | 0 | void **new_memory; |
48 | |
|
49 | 0 | if (length <= array->allocated) |
50 | 0 | return true; |
51 | | |
52 | | |
53 | 0 | if (array->allocated == 0) |
54 | 0 | new_allocated = 16; |
55 | 0 | else { |
56 | 0 | return_val_if_fail (SIZE_MAX / array->allocated >= 2, false); |
57 | 0 | new_allocated = array->allocated * 2; |
58 | 0 | } |
59 | 0 | if (new_allocated < length) |
60 | 0 | new_allocated = length; |
61 | |
|
62 | 0 | new_memory = reallocarray (array->elem, new_allocated, sizeof (void*)); |
63 | 0 | return_val_if_fail (new_memory != NULL, false); |
64 | | |
65 | 0 | array->elem = new_memory; |
66 | 0 | array->allocated = new_allocated; |
67 | 0 | return true; |
68 | 0 | } |
69 | | |
70 | | p11_array * |
71 | | p11_array_new (p11_destroyer destroyer) |
72 | 0 | { |
73 | 0 | p11_array *array; |
74 | |
|
75 | 0 | array = calloc (1, sizeof (p11_array)); |
76 | 0 | if (array == NULL) |
77 | 0 | return NULL; |
78 | | |
79 | 0 | if (!maybe_expand_array (array, 2)) { |
80 | 0 | p11_array_free (array); |
81 | 0 | return NULL; |
82 | 0 | } |
83 | | |
84 | 0 | array->destroyer = destroyer; |
85 | 0 | return array; |
86 | 0 | } |
87 | | |
88 | | void |
89 | | p11_array_free (p11_array *array) |
90 | 0 | { |
91 | 0 | if (array == NULL) |
92 | 0 | return; |
93 | | |
94 | 0 | p11_array_clear (array); |
95 | 0 | free (array->elem); |
96 | 0 | free (array); |
97 | 0 | } |
98 | | |
99 | | bool |
100 | | p11_array_push (p11_array *array, |
101 | | void *value) |
102 | 0 | { |
103 | 0 | if (!maybe_expand_array (array, array->num + 1)) |
104 | 0 | return_val_if_reached (false); |
105 | | |
106 | 0 | array->elem[array->num] = value; |
107 | 0 | array->num++; |
108 | 0 | return true; |
109 | 0 | } |
110 | | |
111 | | bool |
112 | | p11_array_insert (p11_array *array, |
113 | | unsigned int index, |
114 | | void *value) |
115 | 0 | { |
116 | 0 | return_val_if_fail (index <= array->num, false); |
117 | 0 | if (!maybe_expand_array (array, array->num + 1)) |
118 | 0 | return_val_if_reached (false); |
119 | | |
120 | 0 | memmove (array->elem + index + 1, array->elem + index, |
121 | 0 | (array->num - index) * sizeof (void*)); |
122 | 0 | array->elem[index] = value; |
123 | 0 | array->num++; |
124 | 0 | return true; |
125 | 0 | } |
126 | | |
127 | | void |
128 | | p11_array_remove (p11_array *array, |
129 | | unsigned int index) |
130 | 0 | { |
131 | 0 | if (array->destroyer) |
132 | 0 | (array->destroyer) (array->elem[index]); |
133 | 0 | memmove (array->elem + index, array->elem + index + 1, |
134 | 0 | (array->num - (index + 1)) * sizeof (void*)); |
135 | 0 | array->num--; |
136 | 0 | } |
137 | | |
138 | | void |
139 | | p11_array_clear (p11_array *array) |
140 | 0 | { |
141 | 0 | unsigned int i; |
142 | |
|
143 | 0 | if (array->destroyer) { |
144 | 0 | for (i = 0; i < array->num; i++) |
145 | 0 | (array->destroyer) (array->elem[i]); |
146 | 0 | } |
147 | |
|
148 | 0 | array->num = 0; |
149 | 0 | } |