2 * Copyright (c) 2008-2015 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
));
41 if (l
!= NULL
) l
->refcount
= 1;
46 size
= (l
->count
+ 1) * sizeof(si_item_t
*);
48 l
->entry
= (si_item_t
**)reallocf(l
->entry
, size
);
49 if (l
->entry
!= NULL
) l
->entry
[l
->count
++] = si_item_retain(e
);
52 if ((l
== NULL
) || (l
->entry
== NULL
))
63 si_list_concat(si_list_t
*l
, si_list_t
*x
)
70 if ((x
== NULL
) || (x
->count
== 0)) return l
;
74 l
= (si_list_t
*)calloc(1, sizeof(si_list_t
));
80 newcount
= (size_t)l
->count
+ (size_t)x
->count
;
81 size
= newcount
* sizeof(si_item_t
*);
83 l
->entry
= (si_item_t
**)reallocf(l
->entry
, size
);
86 for (i
= 0; i
< x
->count
; ++i
)
90 l
->entry
[l
->count
+ i
] = item
;
103 if (l
== NULL
) errno
= ENOMEM
;
109 si_list_next(si_list_t
*list
)
111 if (list
== NULL
) return NULL
;
112 if (list
->curr
>= list
->count
) return NULL
;
114 return list
->entry
[list
->curr
++];
118 si_list_reset(si_list_t
*list
)
120 if (list
!= NULL
) list
->curr
= 0;
124 si_list_retain(si_list_t
*list
)
128 if (list
== NULL
) return NULL
;
130 rc
= OSAtomicIncrement32Barrier(&list
->refcount
);
137 si_list_release(si_list_t
*list
)
141 if (list
== NULL
) return;
143 rc
= OSAtomicDecrement32Barrier(&list
->refcount
);
148 for (i
= 0; i
< list
->count
; i
++)
150 si_item_release(list
->entry
[i
]);
159 si_item_retain(si_item_t
*item
)
163 if (item
== NULL
) return NULL
;
165 rc
= OSAtomicIncrement32Barrier(&item
->refcount
);
172 si_item_release(si_item_t
*item
)
176 if (item
== NULL
) return;
178 rc
= OSAtomicDecrement32Barrier(&item
->refcount
);
181 if (rc
== 0) free(item
);