]> git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/si_data.c
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / lookup.subproj / si_data.c
1 /*
2 * Copyright (c) 2008-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include "libinfo_common.h"
25
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <libkern/OSAtomic.h>
30 #include "si_data.h"
31 #include "si_module.h"
32
33 LIBINFO_EXPORT
34 si_list_t *
35 si_list_add(si_list_t *l, si_item_t *e)
36 {
37 size_t size;
38
39 if (e == NULL) return l;
40
41 if (l == NULL)
42 {
43 l = (si_list_t *)calloc(1, sizeof(si_list_t));
44 if (l != NULL) l->refcount = 1;
45 }
46
47 if (l != NULL)
48 {
49 size = (l->count + 1) * sizeof(si_item_t *);
50
51 l->entry = (si_item_t **)reallocf(l->entry, size);
52 if (l->entry != NULL) l->entry[l->count++] = si_item_retain(e);
53 }
54
55 if ((l == NULL) || (l->entry == NULL))
56 {
57 free(l);
58 l = NULL;
59 errno = ENOMEM;
60 }
61
62 return l;
63 }
64
65 LIBINFO_EXPORT
66 si_list_t *
67 si_list_concat(si_list_t *l, si_list_t *x)
68 {
69 si_item_t *item;
70 size_t newcount;
71 size_t size;
72 int i;
73
74 if ((x == NULL) || (x->count == 0)) return l;
75
76 if (l == NULL)
77 {
78 l = (si_list_t *)calloc(1, sizeof(si_list_t));
79 l->refcount = 1;
80 }
81
82 if (l != NULL)
83 {
84 newcount = (size_t)l->count + (size_t)x->count;
85 size = newcount * sizeof(si_item_t *);
86
87 l->entry = (si_item_t **)reallocf(l->entry, size);
88 if (l->entry)
89 {
90 for (i = 0; i < x->count; ++i)
91 {
92 item = x->entry[i];
93 si_item_retain(item);
94 l->entry[l->count + i] = item;
95 }
96
97 l->count += x->count;
98 }
99 else
100 {
101 l->count = 0;
102 free(l);
103 l = NULL;
104 }
105 }
106
107 if (l == NULL) errno = ENOMEM;
108
109 return l;
110 }
111
112 LIBINFO_EXPORT
113 si_item_t *
114 si_list_next(si_list_t *list)
115 {
116 if (list == NULL) return NULL;
117 if (list->curr >= list->count) return NULL;
118
119 return list->entry[list->curr++];
120 }
121
122 LIBINFO_EXPORT
123 void
124 si_list_reset(si_list_t *list)
125 {
126 if (list != NULL) list->curr = 0;
127 }
128
129 LIBINFO_EXPORT
130 si_list_t *
131 si_list_retain(si_list_t *list)
132 {
133 int32_t rc;
134
135 if (list == NULL) return NULL;
136
137 rc = OSAtomicIncrement32Barrier(&list->refcount);
138 assert(rc >= 1);
139
140 return list;
141 }
142
143 LIBINFO_EXPORT
144 void
145 si_list_release(si_list_t *list)
146 {
147 int32_t rc, i;
148
149 if (list == NULL) return;
150
151 rc = OSAtomicDecrement32Barrier(&list->refcount);
152 assert(rc >= 0);
153
154 if (rc == 0)
155 {
156 for (i = 0; i < list->count; i++)
157 {
158 si_item_release(list->entry[i]);
159 }
160
161 free(list->entry);
162 free(list);
163 }
164 }
165
166 LIBINFO_EXPORT
167 si_item_t *
168 si_item_retain(si_item_t *item)
169 {
170 int32_t rc;
171
172 if (item == NULL) return NULL;
173
174 rc = OSAtomicIncrement32Barrier(&item->refcount);
175 assert(rc >= 1);
176
177 return item;
178 }
179
180 LIBINFO_EXPORT
181 void
182 si_item_release(si_item_t *item)
183 {
184 int32_t rc;
185
186 if (item == NULL) return;
187
188 rc = OSAtomicDecrement32Barrier(&item->refcount);
189 assert(rc >= 0);
190
191 if (rc == 0) free(item);
192 }