klist.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2013-2014 Andrew Smith - BlackArrow Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #ifndef KLIST_H
  10. #define KLIST_H
  11. #include <miner.h>
  12. #define KLIST_FFL " - from %s %s() line %d"
  13. #define KLIST_FFL_HERE __FILE__, __func__, __LINE__
  14. #define KLIST_FFL_PASS file, func, line
  15. #define KLIST_FFL_ARGS __maybe_unused const char *file, \
  16. __maybe_unused const char *func, \
  17. __maybe_unused const int line
  18. typedef struct k_item {
  19. const char *name;
  20. struct k_item *prev;
  21. struct k_item *next;
  22. void *data;
  23. } K_ITEM;
  24. typedef struct k_list {
  25. const char *name;
  26. bool is_store;
  27. cglock_t *lock;
  28. struct k_item *head;
  29. struct k_item *tail;
  30. size_t siz; // item data size
  31. int total; // total allocated
  32. int count; // in this list
  33. int count_up; // incremented every time one is added
  34. int allocate; // number to intially allocate and each time we run out
  35. int limit; // total limit - 0 means unlimited
  36. bool do_tail; // track the tail?
  37. int item_mem_count; // how many item memory buffers have been allocated
  38. void **item_memory; // allocated item memory buffers
  39. int data_mem_count; // how many item data memory buffers have been allocated
  40. void **data_memory; // allocated item data memory buffers
  41. } K_LIST;
  42. /*
  43. * K_STORE is for a list of items taken from a K_LIST
  44. * The restriction is, a K_STORE must not allocate new items,
  45. * only the K_LIST should do that
  46. * i.e. all K_STORE items came from a K_LIST
  47. */
  48. #define K_STORE K_LIST
  49. /*
  50. * N.B. all locking is done in the code using the K_*LOCK macros
  51. */
  52. #define K_WLOCK(_list) cg_wlock(_list->lock)
  53. #define K_WUNLOCK(_list) cg_wunlock(_list->lock)
  54. #define K_RLOCK(_list) cg_rlock(_list->lock)
  55. #define K_RUNLOCK(_list) cg_runlock(_list->lock)
  56. extern K_STORE *k_new_store(K_LIST *list);
  57. extern K_LIST *_k_new_list(const char *name, size_t siz, int allocate, int limit, bool do_tail, KLIST_FFL_ARGS);
  58. #define k_new_list(_name, _siz, _allocate, _limit, _do_tail) _k_new_list(_name, _siz, _allocate, _limit, _do_tail, KLIST_FFL_HERE)
  59. extern K_ITEM *_k_unlink_head(K_LIST *list, KLIST_FFL_ARGS);
  60. #define k_unlink_head(_list) _k_unlink_head(_list, KLIST_FFL_HERE)
  61. extern K_ITEM *_k_unlink_head_zero(K_LIST *list, KLIST_FFL_ARGS);
  62. #define k_unlink_head_zero(_list) _k_unlink_head_zero(_list, KLIST_FFL_HERE)
  63. extern K_ITEM *_k_unlink_tail(K_LIST *list, KLIST_FFL_ARGS);
  64. #define k_unlink_tail(_list) _k_unlink_tail(_list, KLIST_FFL_HERE)
  65. extern void _k_add_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
  66. #define k_add_head(_list, _item) _k_add_head(_list, _item, KLIST_FFL_HERE)
  67. // extern void k_free_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
  68. #define k_free_head(__list, __item) _k_add_head(__list, __item, KLIST_FFL_HERE)
  69. extern void _k_add_tail(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
  70. #define k_add_tail(_list, _item) _k_add_tail(_list, _item, KLIST_FFL_HERE)
  71. extern void _k_insert_before(K_LIST *list, K_ITEM *item, K_ITEM *before, KLIST_FFL_ARGS);
  72. #define k_insert_before(_list, _item, _before) _k_insert_before(_list, _item, _before, KLIST_FFL_HERE)
  73. extern void _k_insert_after(K_LIST *list, K_ITEM *item, K_ITEM *after, KLIST_FFL_ARGS);
  74. #define k_insert_after(_list, _item, _after) _k_insert_after(_list, _item, _after, KLIST_FFL_HERE)
  75. extern void _k_unlink_item(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS);
  76. #define k_unlink_item(_list, _item) _k_unlink_item(_list, _item, KLIST_FFL_HERE)
  77. void _k_list_transfer_to_head(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS);
  78. #define k_list_transfer_to_head(_from, _to) _k_list_transfer_to_head(_from, _to, KLIST_FFL_HERE)
  79. void _k_list_transfer_to_tail(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS);
  80. #define k_list_transfer_to_tail(_from, _to) _k_list_transfer_to_tail(_from, _to, KLIST_FFL_HERE)
  81. extern K_LIST *_k_free_list(K_LIST *list, KLIST_FFL_ARGS);
  82. #define k_free_list(_list) _k_free_list(_list, KLIST_FFL_HERE)
  83. extern K_STORE *_k_free_store(K_STORE *store, KLIST_FFL_ARGS);
  84. #define k_free_store(_store) _k_free_store(_store, KLIST_FFL_HERE)
  85. #endif