Libinfo-517.tar.gz
[apple/libinfo.git] / lookup.subproj / si_data.c
1 /*
2 * Copyright (c) 2008-2015 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 <assert.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <libkern/OSAtomic.h>
28 #include "si_data.h"
29 #include "si_module.h"
30
31 si_list_t *
32 si_list_add(si_list_t *l, si_item_t *e)
33 {
34 size_t size;
35
36 if (e == NULL) return l;
37
38 if (l == NULL)
39 {
40 l = (si_list_t *)calloc(1, sizeof(si_list_t));
41 if (l != NULL) l->refcount = 1;
42 }
43
44 if (l != NULL)
45 {
46 size = (l->count + 1) * sizeof(si_item_t *);
47
48 l->entry = (si_item_t **)reallocf(l->entry, size);
49 if (l->entry != NULL) l->entry[l->count++] = si_item_retain(e);
50 }
51
52 if ((l == NULL) || (l->entry == NULL))
53 {
54 free(l);
55 l = NULL;
56 errno = ENOMEM;
57 }
58
59 return l;
60 }
61
62 si_list_t *
63 si_list_concat(si_list_t *l, si_list_t *x)
64 {
65 si_item_t *item;
66 size_t newcount;
67 size_t size;
68 int i;
69
70 if ((x == NULL) || (x->count == 0)) return l;
71
72 if (l == NULL)
73 {
74 l = (si_list_t *)calloc(1, sizeof(si_list_t));
75 l->refcount = 1;
76 }
77
78 if (l != NULL)
79 {
80 newcount = (size_t)l->count + (size_t)x->count;
81 size = newcount * sizeof(si_item_t *);
82
83 l->entry = (si_item_t **)reallocf(l->entry, size);
84 if (l->entry)
85 {
86 for (i = 0; i < x->count; ++i)
87 {
88 item = x->entry[i];
89 si_item_retain(item);
90 l->entry[l->count + i] = item;
91 }
92
93 l->count += x->count;
94 }
95 else
96 {
97 l->count = 0;
98 free(l);
99 l = NULL;
100 }
101 }
102
103 if (l == NULL) errno = ENOMEM;
104
105 return l;
106 }
107
108 si_item_t *
109 si_list_next(si_list_t *list)
110 {
111 if (list == NULL) return NULL;
112 if (list->curr >= list->count) return NULL;
113
114 return list->entry[list->curr++];
115 }
116
117 void
118 si_list_reset(si_list_t *list)
119 {
120 if (list != NULL) list->curr = 0;
121 }
122
123 si_list_t *
124 si_list_retain(si_list_t *list)
125 {
126 int32_t rc;
127
128 if (list == NULL) return NULL;
129
130 rc = OSAtomicIncrement32Barrier(&list->refcount);
131 assert(rc >= 1);
132
133 return list;
134 }
135
136 void
137 si_list_release(si_list_t *list)
138 {
139 int32_t rc, i;
140
141 if (list == NULL) return;
142
143 rc = OSAtomicDecrement32Barrier(&list->refcount);
144 assert(rc >= 0);
145
146 if (rc == 0)
147 {
148 for (i = 0; i < list->count; i++)
149 {
150 si_item_release(list->entry[i]);
151 }
152
153 free(list->entry);
154 free(list);
155 }
156 }
157
158 si_item_t *
159 si_item_retain(si_item_t *item)
160 {
161 int32_t rc;
162
163 if (item == NULL) return NULL;
164
165 rc = OSAtomicIncrement32Barrier(&item->refcount);
166 assert(rc >= 1);
167
168 return item;
169 }
170
171 void
172 si_item_release(si_item_t *item)
173 {
174 int32_t rc;
175
176 if (item == NULL) return;
177
178 rc = OSAtomicDecrement32Barrier(&item->refcount);
179 assert(rc >= 0);
180
181 if (rc == 0) free(item);
182 }