]>
git.saurik.com Git - apple/libinfo.git/blob - lookup.subproj/si_data.c
2 * Copyright (c) 2008-2009 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@
27 #include <libkern/OSAtomic.h>
29 #include "si_module.h"
32 si_list_add(si_list_t
*l
, si_item_t
*e
)
36 if (e
== NULL
) return l
;
40 l
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
46 size
= (l
->count
+ 1) * sizeof(si_item_t
*);
48 l
->entry
= (si_item_t
**)reallocf(l
->entry
, size
);
51 l
->entry
[l
->count
++] = si_item_retain(e
);
55 if ((l
== NULL
) || (l
->entry
== NULL
))
66 si_list_concat(si_list_t
*l
, si_list_t
*x
)
73 if ((x
== NULL
) || (x
->count
== 0)) return l
;
77 l
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
83 newcount
= l
->count
+ x
->count
;
84 size
= newcount
* sizeof(si_item_t
*);
86 l
->entry
= (si_item_t
**)reallocf(l
->entry
, size
);
89 for (i
= 0; i
< x
->count
; ++i
)
93 l
->entry
[l
->count
+ i
] = item
;
105 if (l
== NULL
) errno
= ENOMEM
;
111 si_list_next(si_list_t
*list
)
113 if (list
== NULL
) return NULL
;
114 if (list
->curr
>= list
->count
) return NULL
;
116 return list
->entry
[list
->curr
++];
120 si_list_reset(si_list_t
*list
)
122 if (list
!= NULL
) list
->curr
= 0;
126 si_list_retain(si_list_t
*list
)
130 if (list
== NULL
) return NULL
;
132 rc
= OSAtomicIncrement32Barrier(&list
->refcount
);
139 si_list_release(si_list_t
*list
)
143 if (list
== NULL
) return;
145 rc
= OSAtomicDecrement32Barrier(&list
->refcount
);
150 for (i
= 0; i
< list
->count
; i
++)
152 si_item_release(list
->entry
[i
]);
161 si_item_retain(si_item_t
*item
)
165 if (item
== NULL
) return NULL
;
167 rc
= OSAtomicIncrement32Barrier(&item
->refcount
);
174 si_item_release(si_item_t
*item
)
178 if (item
== NULL
) return;
180 rc
= OSAtomicDecrement32Barrier(&item
->refcount
);
183 if (rc
== 0) free(item
);