klist lists for bab
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
diff --git a/Makefile.am b/Makefile.am
index af24c49..bb0155e 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -43,6 +43,10 @@ cgminer_SOURCES += elist.h miner.h compat.h bench_block.h \
cgminer_SOURCES += logging.c
+if HAS_BAB
+cgminer_SOURCES += klist.h klist.c
+endif
+
if NEED_FPGAUTILS
cgminer_SOURCES += fpgautils.c fpgautils.h
endif
diff --git a/klist.c b/klist.c
new file mode 100644
index 0000000..ca325e5
--- /dev/null
+++ b/klist.c
@@ -0,0 +1,286 @@
+/*
+ * Copyright 2013-2014 Andrew Smith - BlackArrow Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version. See COPYING for more details.
+ */
+
+#include <klist.h>
+
+static void k_alloc_items(K_LIST *list, KLIST_FFL_ARGS)
+{
+ K_ITEM *item;
+ int allocate, i;
+
+ if (list->is_store) {
+ quithere(1, "List %s store can't %s" KLIST_FFL,
+ list->name, __func__, KLIST_FFL_PASS);
+ }
+
+ if (list->limit > 0 && list->total >= list->limit)
+ return;
+
+ allocate = list->allocate;
+ if (list->limit > 0 && (list->total + allocate) > list->limit)
+ allocate = list->limit - list->total;
+
+ list->item_mem_count++;
+ if (!realloc(list->item_memory, list->item_mem_count * sizeof(*(list->item_memory)))) {
+ quithere(1, "List %s item_memory failed to realloc count=%d",
+ list->name, list->item_mem_count);
+ }
+ item = calloc(allocate, sizeof(*item));
+ if (!item) {
+ quithere(1, "List %s failed to calloc %d new items - total was %d, limit was %d",
+ list->name, allocate, list->total, list->limit);
+ }
+ list->item_memory[list->item_mem_count - 1] = (void *)item;
+
+ list->total += allocate;
+ list->count = allocate;
+ list->count_up = allocate;
+
+ item[0].name = list->name;
+ item[0].prev = NULL;
+ item[0].next = &(item[1]);
+ for (i = 1; i < allocate-1; i++) {
+ item[i].name = list->name;
+ item[i].prev = &item[i-1];
+ item[i].next = &item[i+1];
+ }
+ item[allocate-1].name = list->name;
+ item[allocate-1].prev = &(item[allocate-2]);
+ item[allocate-1].next = NULL;
+
+ list->head = item;
+ if (list->do_tail)
+ list->tail = &(item[allocate-1]);
+
+ item = list->head;
+ while (item) {
+ list->data_mem_count++;
+ if (!realloc(list->data_memory, list->data_mem_count * sizeof(*(list->data_memory)))) {
+ quithere(1, "List %s data_memory failed to realloc count=%d",
+ list->name, list->data_mem_count);
+ }
+ item->data = calloc(1, list->siz);
+ if (!(item->data))
+ quithere(1, "List %s failed to calloc item data", list->name);
+ list->data_memory[list->data_mem_count - 1] = (void *)(item->data);
+ item = item->next;
+ }
+}
+
+K_STORE *k_new_store(K_LIST *list)
+{
+ K_STORE *store;
+
+ store = calloc(1, sizeof(*store));
+ if (!store)
+ quithere(1, "Failed to calloc store for %s", list->name);
+
+ store->is_store = true;
+ store->lock = list->lock;
+ store->name = list->name;
+ store->do_tail = list->do_tail;
+
+ return store;
+}
+
+K_LIST *_k_new_list(const char *name, size_t siz, int allocate, int limit, bool do_tail, KLIST_FFL_ARGS)
+{
+ K_LIST *list;
+
+ if (allocate < 1)
+ quithere(1, "Invalid new list %s with allocate %d must be > 0", name, allocate);
+
+ if (limit < 0)
+ quithere(1, "Invalid new list %s with limit %d must be >= 0", name, limit);
+
+ list = calloc(1, sizeof(*list));
+ if (!list)
+ quithere(1, "Failed to calloc list %s", name);
+
+ list->is_store = false;
+
+ list->lock = calloc(1, sizeof(*(list->lock)));
+ if (!(list->lock))
+ quithere(1, "Failed to calloc lock for list %s", name);
+
+ cglock_init(list->lock);
+
+ list->name = name;
+ list->siz = siz;
+ list->allocate = allocate;
+ list->limit = limit;
+ list->do_tail = do_tail;
+
+ k_alloc_items(list, KLIST_FFL_PASS);
+
+ return list;
+}
+
+/*
+ * Unlink and return the head of the list
+ * If the list is empty:
+ * 1) If it's a store - return NULL
+ * 2) alloc a new list and return the head -
+ * which is NULL if the list limit has been reached
+ */
+K_ITEM *_k_unlink_head(K_LIST *list, KLIST_FFL_ARGS)
+{
+ K_ITEM *item;
+
+ if (!(list->head) && !(list->is_store))
+ k_alloc_items(list, KLIST_FFL_PASS);
+
+ if (!(list->head))
+ return NULL;
+
+ item = list->head;
+ list->head = item->next;
+ if (list->head)
+ list->head->prev = NULL;
+ else {
+ if (list->do_tail)
+ list->tail = NULL;
+ }
+
+ item->prev = item->next = NULL;
+
+ list->count--;
+
+ return item;
+}
+
+// Zeros the head returned
+K_ITEM *_k_unlink_head_zero(K_LIST *list, KLIST_FFL_ARGS)
+{
+ K_ITEM *item;
+
+ item = _k_unlink_head(list, KLIST_FFL_PASS);
+
+ if (item)
+ memset(item->data, 0, list->siz);
+
+ return item;
+}
+
+// Returns NULL if empty
+K_ITEM *_k_unlink_tail(K_LIST *list, KLIST_FFL_ARGS)
+{
+ K_ITEM *item;
+
+ if (!(list->do_tail)) {
+ quithere(1, "List %s do_tail false can't %s" KLIST_FFL,
+ list->name, __func__, KLIST_FFL_PASS);
+ }
+
+ if (!(list->tail))
+ return NULL;
+
+ item = list->tail;
+ list->tail = item->prev;
+ if (list->tail)
+ list->tail->next = NULL;
+ else
+ list->head = NULL;
+
+ item->prev = item->next = NULL;
+
+ list->count--;
+
+ return item;
+}
+
+void _k_add_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
+{
+ if (item->name != list->name) {
+ quithere(1, "List %s can't %s a %s item" KLIST_FFL,
+ list->name, __func__, item->name, KLIST_FFL_PASS);
+ }
+
+ item->prev = NULL;
+ item->next = list->head;
+ if (list->head)
+ list->head->prev = item;
+
+ list->head = item;
+
+ if (list->do_tail) {
+ if (!(list->tail))
+ list->tail = item;
+ }
+
+ list->count++;
+ list->count_up++;
+}
+
+/* slows it down (of course) - only for debugging
+void _k_free_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
+{
+ memset(item->data, 0xff, list->siz);
+ _k_add_head(list, item, KLIST_FFL_PASS);
+}
+*/
+
+void k_unlink_item(K_LIST *list, K_ITEM *item)
+{
+ if (item->prev)
+ item->prev->next = item->next;
+
+ if (item->next)
+ item->next->prev = item->prev;
+
+ if (list->head == item)
+ list->head = item->next;
+
+ if (list->do_tail) {
+ if (list->tail == item)
+ list->tail = item->prev;
+ }
+
+ item->prev = item->next = NULL;
+
+ list->count--;
+}
+
+K_LIST *_k_free_list(K_LIST *list, KLIST_FFL_ARGS)
+{
+ int i;
+
+ if (list->is_store) {
+ quithere(1, "List %s can't %s() a store" KLIST_FFL,
+ list->name, __func__, KLIST_FFL_PASS);
+ }
+
+ for (i = 0; i < list->item_mem_count; i++)
+ free(list->item_memory[i]);
+ free(list->item_memory);
+
+ for (i = 0; i < list->data_mem_count; i++)
+ free(list->data_memory[i]);
+ free(list->data_memory);
+
+ cglock_destroy(list->lock);
+
+ free(list->lock);
+
+ free(list);
+
+ return NULL;
+}
+
+K_STORE *_k_free_store(K_STORE *store, KLIST_FFL_ARGS)
+{
+ if (!(store->is_store)) {
+ quithere(1, "Store %s can't %s() the list" KLIST_FFL,
+ store->name, __func__, KLIST_FFL_PASS);
+ }
+
+ free(store);
+
+ return NULL;
+}
diff --git a/klist.h b/klist.h
new file mode 100644
index 0000000..7608e24
--- /dev/null
+++ b/klist.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2013-2014 Andrew Smith - BlackArrow Ltd
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version. See COPYING for more details.
+ */
+
+#ifndef KLIST_H
+#define KLIST_H
+
+#include <miner.h>
+
+#define KLIST_FFL " - from %s %s() line %d"
+#define KLIST_FFL_HERE __FILE__, __func__, __LINE__
+#define KLIST_FFL_PASS file, func, line
+#define KLIST_FFL_ARGS __maybe_unused const char *file, \
+ __maybe_unused const char *func, \
+ __maybe_unused const int line
+
+typedef struct k_item {
+ const char *name;
+ struct k_item *prev;
+ struct k_item *next;
+ void *data;
+} K_ITEM;
+
+typedef struct k_list {
+ const char *name;
+ bool is_store;
+ cglock_t *lock;
+ struct k_item *head;
+ struct k_item *tail;
+ size_t siz; // item data size
+ int total; // total allocated
+ int count; // in this list
+ int count_up; // incremented every time one is added
+ int allocate; // number to intially allocate and each time we run out
+ int limit; // total limit - 0 means unlimited
+ bool do_tail; // track the tail?
+ int item_mem_count; // how many item memory buffers have been allocated
+ void **item_memory; // allocated item memory buffers
+ int data_mem_count; // how many item data memory buffers have been allocated
+ void **data_memory; // allocated item data memory buffers
+} K_LIST;
+
+/*
+ * K_STORE is for a list of items taken from a K_LIST
+ * The restriction is, a K_STORE must not allocate new items,
+ * only the K_LIST should do that
+ * i.e. all K_STORE items came from a K_LIST
+ */
+#define K_STORE K_LIST
+
+/*
+ * N.B. all locking is done in the code calling the k_ functions
+ */
+
+#define K_WLOCK(_list) cg_wlock(_list->lock)
+#define K_WUNLOCK(_list) cg_wunlock(_list->lock)
+#define K_RLOCK(_list) cg_rlock(_list->lock)
+#define K_RUNLOCK(_list) cg_runlock(_list->lock)
+
+extern K_STORE *k_new_store(K_LIST *list);
+extern K_LIST *_k_new_list(const char *name, size_t siz, int allocate, int limit, bool do_tail, KLIST_FFL_ARGS);
+#define k_new_list(_name, _siz, _allocate, _limit, _do_tail) _k_new_list(_name, _siz, _allocate, _limit, _do_tail, KLIST_FFL_HERE)
+extern K_ITEM *_k_unlink_head(K_LIST *list, KLIST_FFL_ARGS);
+#define k_unlink_head(_list) _k_unlink_head(_list, KLIST_FFL_HERE)
+extern K_ITEM *_k_unlink_head_zero(K_LIST *list, KLIST_FFL_ARGS);
+#define k_unlink_head_zero(_list) _k_unlink_head_zero(_list, KLIST_FFL_HERE)
+extern K_ITEM *_k_unlink_tail(K_LIST *list, KLIST_FFL_ARGS);
+#define k_unlink_tail(_list) _k_unlink_tail(_list, KLIST_FFL_HERE)
+extern void _k_add_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
+#define k_add_head(_list, _item) _k_add_head(_list, _item, KLIST_FFL_HERE)
+// extern void k_free_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
+#define k_free_head(__list, __item) _k_add_head(__list, __item, KLIST_FFL_HERE)
+extern void k_unlink_item(K_LIST *list, K_ITEM *item);
+extern K_LIST *_k_free_list(K_LIST *list, KLIST_FFL_ARGS);
+#define k_free_list(_list) _k_free_list(_list, KLIST_FFL_HERE)
+extern K_STORE *_k_free_store(K_STORE *store, KLIST_FFL_ARGS);
+#define k_free_store(_store) _k_free_store(_store, KLIST_FFL_HERE)
+
+#endif