Libinfo-476.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 uint32_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 = l->count + 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 l = NULL;
99 }
100 }
101
102 if (l == NULL) errno = ENOMEM;
103
104 return l;
105 }
106
107 si_item_t *
108 si_list_next(si_list_t *list)
109 {
110 if (list == NULL) return NULL;
111 if (list->curr >= list->count) return NULL;
112
113 return list->entry[list->curr++];
114 }
115
116 void
117 si_list_reset(si_list_t *list)
118 {
119 if (list != NULL) list->curr = 0;
120 }
121
122 si_list_t *
123 si_list_retain(si_list_t *list)
124 {
125 int32_t rc;
126
127 if (list == NULL) return NULL;
128
129 rc = OSAtomicIncrement32Barrier(&list->refcount);
130 assert(rc >= 1);
131
132 return list;
133 }
134
135 void
136 si_list_release(si_list_t *list)
137 {
138 int32_t rc, i;
139
140 if (list == NULL) return;
141
142 rc = OSAtomicDecrement32Barrier(&list->refcount);
143 assert(rc >= 0);
144
145 if (rc == 0)
146 {
147 for (i = 0; i < list->count; i++)
148 {
149 si_item_release(list->entry[i]);
150 }
151
152 free(list->entry);
153 free(list);
154 }
155 }
156
157 si_item_t *
158 si_item_retain(si_item_t *item)
159 {
160 int32_t rc;
161
162 if (item == NULL) return NULL;
163
164 rc = OSAtomicIncrement32Barrier(&item->refcount);
165 assert(rc >= 1);
166
167 return item;
168 }
169
170 void
171 si_item_release(si_item_t *item)
172 {
173 int32_t rc;
174
175 if (item == NULL) return;
176
177 rc = OSAtomicDecrement32Barrier(&item->refcount);
178 assert(rc >= 0);
179
180 if (rc == 0) free(item);
181 }