]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/si_data.c
2 * Copyright (c) 2008-2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include "libinfo_common.h"
29 #include <libkern/OSAtomic.h>
31 #include "si_module.h"
35 si_list_add(si_list_t
*l
, si_item_t
*e
)
39 if (e
== NULL
) return l
;
43 l
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
44 if (l
!= NULL
) l
->refcount
= 1;
49 size
= (l
->count
+ 1) * sizeof(si_item_t
*);
51 l
->entry
= (si_item_t
**)reallocf(l
->entry
, size
);
52 if (l
->entry
!= NULL
) l
->entry
[l
->count
++] = si_item_retain(e
);
55 if ((l
== NULL
) || (l
->entry
== NULL
))
67 si_list_concat(si_list_t
*l
, si_list_t
*x
)
74 if ((x
== NULL
) || (x
->count
== 0)) return l
;
78 l
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
84 newcount
= (size_t)l
->count
+ (size_t)x
->count
;
85 size
= newcount
* sizeof(si_item_t
*);
87 l
->entry
= (si_item_t
**)reallocf(l
->entry
, size
);
90 for (i
= 0; i
< x
->count
; ++i
)
94 l
->entry
[l
->count
+ i
] = item
;
107 if (l
== NULL
) errno
= ENOMEM
;
114 si_list_next(si_list_t
*list
)
116 if (list
== NULL
) return NULL
;
117 if (list
->curr
>= list
->count
) return NULL
;
119 return list
->entry
[list
->curr
++];
124 si_list_reset(si_list_t
*list
)
126 if (list
!= NULL
) list
->curr
= 0;
131 si_list_retain(si_list_t
*list
)
135 if (list
== NULL
) return NULL
;
137 rc
= OSAtomicIncrement32Barrier(&list
->refcount
);
145 si_list_release(si_list_t
*list
)
149 if (list
== NULL
) return;
151 rc
= OSAtomicDecrement32Barrier(&list
->refcount
);
156 for (i
= 0; i
< list
->count
; i
++)
158 si_item_release(list
->entry
[i
]);
168 si_item_retain(si_item_t
*item
)
172 if (item
== NULL
) return NULL
;
174 rc
= OSAtomicIncrement32Barrier(&item
->refcount
);
182 si_item_release(si_item_t
*item
)
186 if (item
== NULL
) return;
188 rc
= OSAtomicDecrement32Barrier(&item
->refcount
);
191 if (rc
== 0) free(item
);