Cyclone Scheme  0.28.0
ck-polyfill.h
Go to the documentation of this file.
1 #ifndef CYCLONE_CK_POLYFILL_H
2 #define CYCLONE_CK_POLYFILL_H
3 
4 #include "cyclone/types.h"
5 #include "cyclone/hashset.h"
6 #include <stdbool.h>
7 #include <stdint.h>
8 
9 void ck_polyfill_init();
10 
11 struct ck_malloc {
12  void *(*malloc)(size_t);
13  void *(*realloc)(void *, size_t, size_t, bool);
14  void (*free)(void *, size_t, bool);
15 };
16 
18 // Simple hashset (hashset with string support)
19  /* hash function */
20  typedef size_t(*hash_func_t)(const char*, size_t);
21 
23  size_t hash;
25  };
26 
28  size_t nbits;
29  size_t mask;
30 
31  size_t capacity;
33  size_t nitems;
35 
37  };
38 // struct simple_hashset_st;
40 
41 
42  struct hashmap_st;
43  typedef struct hashmap_st *hashmap_t;
44 
45  /*
46  * HASHSET FUNCTIONS
47  */
48 
49  /* create hashset instance */
51 
52  /* destroy hashset instance */
54 
55  /* set hash function */
57 
58  /* add item into the hashset.
59  *
60  * @note 0 and 1 is special values, meaning nil and deleted items. the
61  * function will return -1 indicating error.
62  *
63  * returns zero if the item already in the set and non-zero otherwise
64  */
66 
67  /* check if existence of the item
68  *
69  * returns non-zero if the item exists and zero otherwise
70  */
72 
73 static inline uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
74 {
75  return 0;
76 }
77 
79 // CK Hashset section
80 
81 #define CK_HS_MODE_OBJECT 0
82 #define CK_HS_MODE_SPMC 0
83 
84 struct ck_hs {
85  pthread_mutex_t lock;
87 };
88 
89 typedef struct ck_hs ck_hs_t;
90 
91 /*
92  * Hash callback function.
93  */
94 typedef unsigned long ck_hs_hash_cb_t(const void *, unsigned long);
95 
96 /*
97  * Returns pointer to object if objects are equivalent.
98  */
99 typedef bool ck_hs_compare_cb_t(const void *, const void *);
100 
101 #define CK_HS_HASH(hs, hs_hash, value) 0
102 
103 bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *,
104  ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long);
105 
106 void *ck_hs_get(ck_hs_t *, unsigned long, const void *);
107 bool ck_hs_put(ck_hs_t *, unsigned long, const void *);
108 
109 /*
110 struct ck_hs {
111  struct ck_malloc *m;
112  struct ck_hs_map *map;
113  unsigned int mode;
114  unsigned long seed;
115  ck_hs_hash_cb_t *hf;
116  ck_hs_compare_cb_t *compare;
117 };
118 typedef struct ck_hs ck_hs_t;
119 
120 
121 */
122 
124 // CK Array section
125 struct ck_array {
126  pthread_mutex_t lock;
128 };
129 typedef struct ck_array ck_array_t;
130 
132  int unused;
133 };
135 
136 #define CK_ARRAY_MODE_SPMC 0
137 
138 // DESCRIPTION
139 // The ck_array_init(3) function initializes the array pointed to by the
140 // argument array. The mode value must be CK_ARRAY_MODE_SPMC. The
141 // allocator argument must point to a ck_malloc data structure with valid
142 // non-NULL function pointers initialized for malloc, free and realloc. The
143 // initial_length specifies the initial length of the array. The value of
144 // initial_length must be greater than or equal to 2. An array allows for
145 // one concurrent put or remove operations in the presence of any number of
146 // concurrent CK_ARRAY_FOREACH operations.
147 //
148 // RETURN VALUES
149 // This function returns true if the array was successfully created. It
150 // returns false if the creation failed. Failure may occur due to internal
151 // memory allocation failures or invalid arguments.
152 bool
153 ck_array_init(ck_array_t *array, unsigned int mode,
154  struct ck_malloc *allocator, unsigned int initial_length);
155 
156 // DESCRIPTION
157 // The ck_array_put_unique(3) function will attempt to insert the value of
158 // pointer into the array pointed to by array. This function may incur
159 // additional memory allocations if not enough memory has been allocated in
160 // the array for a new entry. The operation is also free to apply the opera-
161 // tion immediately if there is an opportunity for elimination with a pend-
162 // ing (uncommitted) remove operation. The function will not make any modi-
163 // fications if the pointer already exists in the array.
164 //
165 // RETURN VALUES
166 // This function returns 1 if the pointer already exists in the array. It
167 // returns 0 if the put operation succeeded. It returns -1 on error due to
168 // internal memory allocation failures.
169 int
170 ck_array_put_unique(ck_array_t *array, void *pointer);
171 
172 // DESCRIPTION
173 // The ck_array_remove(3) function will attempt to remove the value of
174 // pointer into the array pointed to by array. The operation is also free to
175 // apply the operation immediately if there is an opportunity for elimina-
176 // tion with a pending (uncommitted) put operation. If no elimination was
177 // possible, the function may require to allocate more memory.
178 //
179 // RETURN VALUES
180 // This function returns true if the remove operation succeeded. It will
181 // return false otherwise due to internal allocation failures or because the
182 // value did not exist.
183 bool
184 ck_array_remove(ck_array_t *array, void *pointer);
185 
186 
187 // DESCRIPTION
188 // The ck_array_commit(3) function will commit any pending put or remove
189 // operations associated with the array. The function may end up requesting
190 // the safe reclamation of memory actively being iterated upon by other
191 // threads.
192 //
193 // RETURN VALUES
194 // This function returns true if the commit operation succeeded. It will
195 // return false otherwise, and pending operations will not be applied.
196 bool
198 
199 
200  // TODO:
201 
202 // Can we safely lock the array, make a copy, and interate over that????
203 #define CK_ARRAY_FOREACH(a, i, b) \
204  pthread_mutex_lock(&((a)->lock)); \
205  int tmpc = (a)->hs->nitems; \
206  void **tmp = alloca(sizeof(void *) * tmpc); \
207  hashset_to_array((a)->hs, tmp); \
208  pthread_mutex_unlock(&((a)->lock)); \
209  if (tmpc > 0) { (*b) = tmp[0]; } \
210  for (unsigned int _ck_i = 0; \
211  _ck_i < tmpc; \
212  _ck_i++, (*b) = tmp[_ck_i])
213 
215 // CK PR section
216 bool
217 ck_pr_cas_ptr(void *target, void *old_value, void *new_value);
218 
219 bool
220 ck_pr_cas_int(int *target, int old_value, int new_value);
221 
222 bool
223 ck_pr_cas_8(uint8_t *target, uint8_t old_value, uint8_t new_value);
224 
225 
226 void
227 ck_pr_add_ptr(void *target, uintptr_t delta);
228 
229 void
230 ck_pr_add_int(int *target, int delta);
231 
232 void
233 ck_pr_add_8(uint8_t *target, uint8_t delta);
234 
235 void *
236 ck_pr_load_ptr(const void *target);
237 
238 int
239 ck_pr_load_int(const int *target);
240 
241 uint8_t
242 ck_pr_load_8(const uint8_t *target);
243 
244 void ck_pr_store_ptr(void *target, void *value);
245 #endif /* CYCLONE_CK_POLYFILL_H */
ck_pr_cas_int
bool ck_pr_cas_int(int *target, int old_value, int new_value)
Definition: ck-polyfill.c:148
ck_hs_get
void * ck_hs_get(ck_hs_t *, unsigned long, const void *)
Definition: ck-polyfill.c:41
simple_hashset_add
int simple_hashset_add(simple_hashset_t set, symbol_type *key)
Definition: ck-polyfill.c:352
simple_hashset_st
Definition: ck-polyfill.h:27
types.h
ck_hs_put
bool ck_hs_put(ck_hs_t *, unsigned long, const void *)
Definition: ck-polyfill.c:58
ck_array::hs
hashset_t hs
Definition: ck-polyfill.h:127
ck_array_put_unique
int ck_array_put_unique(ck_array_t *array, void *pointer)
Definition: ck-polyfill.c:102
simple_hashset_st::nbits
size_t nbits
Definition: ck-polyfill.h:28
simple_hashset_is_member
int simple_hashset_is_member(simple_hashset_t set, symbol_type *key)
Definition: ck-polyfill.c:361
ck_polyfill_init
void ck_polyfill_init()
Definition: ck-polyfill.c:20
ck_pr_add_ptr
void ck_pr_add_ptr(void *target, uintptr_t delta)
Definition: ck-polyfill.c:186
ck_hs
Definition: ck-polyfill.h:84
simple_hashset_st::capacity
size_t capacity
Definition: ck-polyfill.h:31
ck_hs_init
bool ck_hs_init(ck_hs_t *, unsigned int, ck_hs_hash_cb_t *, ck_hs_compare_cb_t *, struct ck_malloc *, unsigned long, unsigned long)
Definition: ck-polyfill.c:30
ck_pr_load_ptr
void * ck_pr_load_ptr(const void *target)
Definition: ck-polyfill.c:214
simple_hashset_t
struct simple_hashset_st * simple_hashset_t
Definition: ck-polyfill.h:39
hash_func_t
size_t(* hash_func_t)(const char *, size_t)
Definition: ck-polyfill.h:20
hashset_st
Definition: hashset.h:27
simple_hashset_set_hash_function
void simple_hashset_set_hash_function(simple_hashset_t set, hash_func_t func)
Definition: ck-polyfill.c:294
hashset.h
simple_hashset_st::mask
size_t mask
Definition: ck-polyfill.h:29
simple_hashset_st::n_deleted_items
size_t n_deleted_items
Definition: ck-polyfill.h:34
ck_hs::hs
simple_hashset_t hs
Definition: ck-polyfill.h:86
ck_pr_store_ptr
void ck_pr_store_ptr(void *target, void *value)
Definition: ck-polyfill.c:243
simple_hashset_st::nitems
size_t nitems
Definition: ck-polyfill.h:33
ck_array_init
bool ck_array_init(ck_array_t *array, unsigned int mode, struct ck_malloc *allocator, unsigned int initial_length)
Definition: ck-polyfill.c:77
simple_hashset_st::items
struct simple_hashset_item_st * items
Definition: ck-polyfill.h:32
ck_array_iterator
Definition: ck-polyfill.h:131
symbol_type
Symbols are similar to strings, but only one instance of each unique symbol is created,...
Definition: types.h:788
hashmap_t
struct hashmap_st * hashmap_t
Definition: ck-polyfill.h:43
ck_array
Definition: ck-polyfill.h:125
ck_array_commit
bool ck_array_commit(ck_array_t *array)
Definition: ck-polyfill.c:138
ck_pr_cas_ptr
bool ck_pr_cas_ptr(void *target, void *old_value, void *new_value)
Definition: ck-polyfill.c:160
simple_hashset_st::hash_func
hash_func_t hash_func
Definition: ck-polyfill.h:36
ck_pr_add_8
void ck_pr_add_8(uint8_t *target, uint8_t delta)
Definition: ck-polyfill.c:206
ck_pr_add_int
void ck_pr_add_int(int *target, int delta)
Definition: ck-polyfill.c:198
ck_hs_compare_cb_t
bool ck_hs_compare_cb_t(const void *, const void *)
Definition: ck-polyfill.h:99
ck_array_iterator::unused
int unused
Definition: ck-polyfill.h:132
ck_pr_cas_8
bool ck_pr_cas_8(uint8_t *target, uint8_t old_value, uint8_t new_value)
Definition: ck-polyfill.c:173
ck_array::lock
pthread_mutex_t lock
Definition: ck-polyfill.h:126
ck_hs::lock
pthread_mutex_t lock
Definition: ck-polyfill.h:85
simple_hashset_item_st::item
symbol_type * item
Definition: ck-polyfill.h:24
ck_array_remove
bool ck_array_remove(ck_array_t *array, void *pointer)
Definition: ck-polyfill.c:122
ck_pr_load_int
int ck_pr_load_int(const int *target)
Definition: ck-polyfill.c:224
simple_hashset_destroy
void simple_hashset_destroy(simple_hashset_t set)
Definition: ck-polyfill.c:286
simple_hashset_item_st
Definition: ck-polyfill.h:22
ck_malloc
Definition: ck-polyfill.h:11
simple_hashset_create
simple_hashset_t simple_hashset_create(void)
Definition: ck-polyfill.c:264
ck_hs_hash_cb_t
unsigned long ck_hs_hash_cb_t(const void *, unsigned long)
Definition: ck-polyfill.h:94
ck_pr_load_8
uint8_t ck_pr_load_8(const uint8_t *target)
Definition: ck-polyfill.c:234
simple_hashset_item_st::hash
size_t hash
Definition: ck-polyfill.h:23
ck_malloc::free
void(* free)(void *, size_t, bool)
Definition: ck-polyfill.h:14