2 * Copyright 2013-2015 Samy Al Bahra
3 * Copyright 2013-2014 AppNexus, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <ck_malloc.h>
34 #include <ck_stdbool.h>
35 #include <ck_stddef.h>
38 unsigned int n_committed;
44 struct ck_malloc *allocator;
45 struct _ck_array *active;
46 unsigned int n_entries;
47 struct _ck_array *transaction;
49 typedef struct ck_array ck_array_t;
51 struct ck_array_iterator {
52 struct _ck_array *snapshot;
54 typedef struct ck_array_iterator ck_array_iterator_t;
56 #define CK_ARRAY_MODE_SPMC 0U
57 #define CK_ARRAY_MODE_MPMC (void) /* Unsupported. */
59 bool ck_array_init(ck_array_t *, unsigned int, struct ck_malloc *, unsigned int);
60 bool ck_array_commit(ck_array_t *);
61 bool ck_array_put(ck_array_t *, void *);
62 int ck_array_put_unique(ck_array_t *, void *);
63 bool ck_array_remove(ck_array_t *, void *);
64 void ck_array_deinit(ck_array_t *, bool);
66 CK_CC_INLINE static unsigned int
67 ck_array_length(struct ck_array *array)
69 struct _ck_array *a = ck_pr_load_ptr(&array->active);
72 return ck_pr_load_uint(&a->n_committed);
75 CK_CC_INLINE static void *
76 ck_array_buffer(struct ck_array *array, unsigned int *length)
78 struct _ck_array *a = ck_pr_load_ptr(&array->active);
81 *length = ck_pr_load_uint(&a->n_committed);
85 CK_CC_INLINE static bool
86 ck_array_initialized(struct ck_array *array)
89 return ck_pr_load_ptr(&array->active) != NULL;
92 #define CK_ARRAY_FOREACH(a, i, b) \
93 (i)->snapshot = ck_pr_load_ptr(&(a)->active); \
95 for (unsigned int _ck_i = 0; \
96 _ck_i < (a)->active->n_committed && \
97 ((*b) = (a)->active->values[_ck_i], 1); \
100 #endif /* CK_ARRAY_H */