1 /* 2 * Copyright 2013-2015 Samy Al Bahra 3 * Copyright 2013-2014 AppNexus, Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28 #ifndef CK_ARRAY_H 29 #define CK_ARRAY_H 30 31 #include <ck_cc.h> 32 #include <ck_malloc.h> 33 #include <ck_pr.h> 34 #include <ck_stdbool.h> 35 #include <ck_stddef.h> 36 37 struct _ck_array { 38 unsigned int n_committed; 39 unsigned int length; 40 void *values[]; 41 }; 42 43 struct ck_array { 44 struct ck_malloc *allocator; 45 struct _ck_array *active; 46 unsigned int n_entries; 47 struct _ck_array *transaction; 48 }; 49 typedef struct ck_array ck_array_t; 50 51 struct ck_array_iterator { 52 struct _ck_array *snapshot; 53 }; 54 typedef struct ck_array_iterator ck_array_iterator_t; 55 56 #define CK_ARRAY_MODE_SPMC 0U 57 #define CK_ARRAY_MODE_MPMC (void) /* Unsupported. */ 58 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); 65 66 CK_CC_INLINE static unsigned int 67 ck_array_length(struct ck_array *array) 68 { 69 struct _ck_array *a = ck_pr_load_ptr(&array->active); 70 71 ck_pr_fence_load(); 72 return ck_pr_load_uint(&a->n_committed); 73 } 74 75 CK_CC_INLINE static void * 76 ck_array_buffer(struct ck_array *array, unsigned int *length) 77 { 78 struct _ck_array *a = ck_pr_load_ptr(&array->active); 79 80 ck_pr_fence_load(); 81 *length = ck_pr_load_uint(&a->n_committed); 82 return a->values; 83 } 84 85 CK_CC_INLINE static bool 86 ck_array_initialized(struct ck_array *array) 87 { 88 89 return ck_pr_load_ptr(&array->active) != NULL; 90 } 91 92 #define CK_ARRAY_FOREACH(a, i, b) \ 93 (i)->snapshot = ck_pr_load_ptr(&(a)->active); \ 94 ck_pr_fence_load(); \ 95 for (unsigned int _ck_i = 0; \ 96 _ck_i < (a)->active->n_committed && \ 97 ((*b) = (a)->active->values[_ck_i], 1); \ 98 _ck_i++) 99 100 #endif /* CK_ARRAY_H */ 101