/*1* Copyright 2013-2015 Samy Al Bahra2* Copyright 2013-2014 AppNexus, Inc.3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#ifndef CK_ARRAY_H28#define CK_ARRAY_H2930#include <ck_cc.h>31#include <ck_malloc.h>32#include <ck_pr.h>33#include <ck_stdbool.h>34#include <ck_stddef.h>3536struct _ck_array {37unsigned int n_committed;38unsigned int length;39void *values[];40};4142struct ck_array {43struct ck_malloc *allocator;44struct _ck_array *active;45unsigned int n_entries;46struct _ck_array *transaction;47};48typedef struct ck_array ck_array_t;4950struct ck_array_iterator {51struct _ck_array *snapshot;52};53typedef struct ck_array_iterator ck_array_iterator_t;5455#define CK_ARRAY_MODE_SPMC 0U56#define CK_ARRAY_MODE_MPMC (void) /* Unsupported. */5758bool ck_array_init(ck_array_t *, unsigned int, struct ck_malloc *, unsigned int);59bool ck_array_commit(ck_array_t *);60bool ck_array_put(ck_array_t *, void *);61int ck_array_put_unique(ck_array_t *, void *);62bool ck_array_remove(ck_array_t *, void *);63void ck_array_deinit(ck_array_t *, bool);6465CK_CC_INLINE static unsigned int66ck_array_length(struct ck_array *array)67{68struct _ck_array *a = ck_pr_load_ptr(&array->active);6970ck_pr_fence_load();71return ck_pr_load_uint(&a->n_committed);72}7374CK_CC_INLINE static void *75ck_array_buffer(struct ck_array *array, unsigned int *length)76{77struct _ck_array *a = ck_pr_load_ptr(&array->active);7879ck_pr_fence_load();80*length = ck_pr_load_uint(&a->n_committed);81return a->values;82}8384CK_CC_INLINE static bool85ck_array_initialized(struct ck_array *array)86{8788return ck_pr_load_ptr(&array->active) != NULL;89}9091#define CK_ARRAY_FOREACH(a, i, b) \92(i)->snapshot = ck_pr_load_ptr(&(a)->active); \93ck_pr_fence_load(); \94for (unsigned int _ck_i = 0; \95_ck_i < (a)->active->n_committed && \96((*b) = (a)->active->values[_ck_i], 1); \97_ck_i++)9899#endif /* CK_ARRAY_H */100101102