klist.c.bak 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. #include <klist.h>
  10. static void k_alloc_items(K_LIST *list, KLIST_FFL_ARGS)
  11. {
  12. K_ITEM *item;
  13. int allocate, i;
  14. if (list->is_store) {
  15. quithere(1, "List %s store can't %s()" KLIST_FFL,
  16. list->name, __func__, KLIST_FFL_PASS);
  17. }
  18. if (list->limit > 0 && list->total >= list->limit)
  19. return;
  20. allocate = list->allocate;
  21. if (list->limit > 0 && (list->total + allocate) > list->limit)
  22. allocate = list->limit - list->total;
  23. list->item_mem_count++;
  24. if (!(list->item_memory = realloc(list->item_memory,
  25. list->item_mem_count * sizeof(*(list->item_memory))))) {
  26. quithere(1, "List %s item_memory failed to realloc count=%d",
  27. list->name, list->item_mem_count);
  28. }
  29. item = calloc(allocate, sizeof(*item));
  30. if (!item) {
  31. quithere(1, "List %s failed to calloc %d new items - total was %d, limit was %d",
  32. list->name, allocate, list->total, list->limit);
  33. }
  34. list->item_memory[list->item_mem_count - 1] = (void *)item;
  35. list->total += allocate;
  36. list->count = allocate;
  37. list->count_up = allocate;
  38. item[0].name = list->name;
  39. item[0].prev = NULL;
  40. item[0].next = &(item[1]);
  41. for (i = 1; i < allocate-1; i++) {
  42. item[i].name = list->name;
  43. item[i].prev = &item[i-1];
  44. item[i].next = &item[i+1];
  45. }
  46. item[allocate-1].name = list->name;
  47. item[allocate-1].prev = &(item[allocate-2]);
  48. item[allocate-1].next = NULL;
  49. list->head = item;
  50. if (list->do_tail)
  51. list->tail = &(item[allocate-1]);
  52. item = list->head;
  53. while (item) {
  54. list->data_mem_count++;
  55. if (!(list->data_memory = realloc(list->data_memory,
  56. list->data_mem_count *
  57. sizeof(*(list->data_memory))))) {
  58. quithere(1, "List %s data_memory failed to realloc count=%d",
  59. list->name, list->data_mem_count);
  60. }
  61. item->data = calloc(1, list->siz);
  62. if (!(item->data))
  63. quithere(1, "List %s failed to calloc item data", list->name);
  64. list->data_memory[list->data_mem_count - 1] = (void *)(item->data);
  65. item = item->next;
  66. }
  67. }
  68. K_STORE *k_new_store(K_LIST *list)
  69. {
  70. K_STORE *store;
  71. store = calloc(1, sizeof(*store));
  72. if (!store)
  73. quithere(1, "Failed to calloc store for %s", list->name);
  74. store->is_store = true;
  75. store->lock = list->lock;
  76. store->name = list->name;
  77. store->do_tail = list->do_tail;
  78. return store;
  79. }
  80. K_LIST *_k_new_list(const char *name, size_t siz, int allocate, int limit, bool do_tail, KLIST_FFL_ARGS)
  81. {
  82. K_LIST *list;
  83. if (allocate < 1)
  84. quithere(1, "Invalid new list %s with allocate %d must be > 0", name, allocate);
  85. if (limit < 0)
  86. quithere(1, "Invalid new list %s with limit %d must be >= 0", name, limit);
  87. list = calloc(1, sizeof(*list));
  88. if (!list)
  89. quithere(1, "Failed to calloc list %s", name);
  90. list->is_store = false;
  91. list->lock = calloc(1, sizeof(*(list->lock)));
  92. if (!(list->lock))
  93. quithere(1, "Failed to calloc lock for list %s", name);
  94. cglock_init(list->lock);
  95. list->name = name;
  96. list->siz = siz;
  97. list->allocate = allocate;
  98. list->limit = limit;
  99. list->do_tail = do_tail;
  100. k_alloc_items(list, KLIST_FFL_PASS);
  101. return list;
  102. }
  103. /*
  104. * Unlink and return the head of the list
  105. * If the list is empty:
  106. * 1) If it's a store - return NULL
  107. * 2) alloc a new list and return the head -
  108. * which is NULL if the list limit has been reached
  109. */
  110. K_ITEM *_k_unlink_head(K_LIST *list, KLIST_FFL_ARGS)
  111. {
  112. K_ITEM *item;
  113. if (!(list->head) && !(list->is_store))
  114. k_alloc_items(list, KLIST_FFL_PASS);
  115. if (!(list->head))
  116. return NULL;
  117. item = list->head;
  118. list->head = item->next;
  119. if (list->head)
  120. list->head->prev = NULL;
  121. else {
  122. if (list->do_tail)
  123. list->tail = NULL;
  124. }
  125. item->prev = item->next = NULL;
  126. list->count--;
  127. return item;
  128. }
  129. // Zeros the head returned
  130. K_ITEM *_k_unlink_head_zero(K_LIST *list, KLIST_FFL_ARGS)
  131. {
  132. K_ITEM *item;
  133. item = _k_unlink_head(list, KLIST_FFL_PASS);
  134. if (item)
  135. memset(item->data, 0, list->siz);
  136. return item;
  137. }
  138. // Returns NULL if empty
  139. K_ITEM *_k_unlink_tail(K_LIST *list, KLIST_FFL_ARGS)
  140. {
  141. K_ITEM *item;
  142. if (!(list->do_tail)) {
  143. quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
  144. list->name, __func__, KLIST_FFL_PASS);
  145. }
  146. if (!(list->tail))
  147. return NULL;
  148. item = list->tail;
  149. list->tail = item->prev;
  150. if (list->tail)
  151. list->tail->next = NULL;
  152. else
  153. list->head = NULL;
  154. item->prev = item->next = NULL;
  155. list->count--;
  156. return item;
  157. }
  158. void _k_add_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
  159. {
  160. if (item->name != list->name) {
  161. quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
  162. list->name, __func__, item->name, KLIST_FFL_PASS);
  163. }
  164. item->prev = NULL;
  165. item->next = list->head;
  166. if (list->head)
  167. list->head->prev = item;
  168. list->head = item;
  169. if (list->do_tail) {
  170. if (!(list->tail))
  171. list->tail = item;
  172. }
  173. list->count++;
  174. list->count_up++;
  175. }
  176. /* slows it down (of course) - only for debugging
  177. void _k_free_head(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
  178. {
  179. memset(item->data, 0xff, list->siz);
  180. _k_add_head(list, item, KLIST_FFL_PASS);
  181. }
  182. */
  183. void _k_add_tail(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
  184. {
  185. if (item->name != list->name) {
  186. quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
  187. list->name, __func__, item->name, KLIST_FFL_PASS);
  188. }
  189. if (!(list->do_tail)) {
  190. quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
  191. list->name, __func__, KLIST_FFL_PASS);
  192. }
  193. item->prev = list->tail;
  194. item->next = NULL;
  195. if (list->tail)
  196. list->tail->next = item;
  197. list->tail = item;
  198. if (!(list->head))
  199. list->head = item;
  200. list->count++;
  201. list->count_up++;
  202. }
  203. void _k_unlink_item(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
  204. {
  205. if (item->name != list->name) {
  206. quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
  207. list->name, __func__, item->name, KLIST_FFL_PASS);
  208. }
  209. if (item->prev)
  210. item->prev->next = item->next;
  211. if (item->next)
  212. item->next->prev = item->prev;
  213. if (list->head == item)
  214. list->head = item->next;
  215. if (list->do_tail) {
  216. if (list->tail == item)
  217. list->tail = item->prev;
  218. }
  219. item->prev = item->next = NULL;
  220. list->count--;
  221. }
  222. void _k_list_transfer_to_head(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS)
  223. {
  224. if (from->name != to->name) {
  225. quithere(1, "List %s can't %s() to a %s list" KLIST_FFL,
  226. from->name, __func__, to->name, KLIST_FFL_PASS);
  227. }
  228. if (!(from->do_tail)) {
  229. quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
  230. from->name, __func__, KLIST_FFL_PASS);
  231. }
  232. if (!(from->head))
  233. return;
  234. if (to->head)
  235. to->head->prev = from->tail;
  236. else
  237. to->tail = from->tail;
  238. from->tail->next = to->head;
  239. to->head = from->head;
  240. from->head = from->tail = NULL;
  241. to->count += from->count;
  242. from->count = 0;
  243. to->count_up += from->count_up;
  244. from->count_up = 0;
  245. }
  246. void _k_list_transfer_to_tail(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS)
  247. {
  248. if (from->name != to->name) {
  249. quithere(1, "List %s can't %s() to a %s list" KLIST_FFL,
  250. from->name, __func__, to->name, KLIST_FFL_PASS);
  251. }
  252. if (!(from->do_tail)) {
  253. quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
  254. from->name, __func__, KLIST_FFL_PASS);
  255. }
  256. if (!(from->head))
  257. return;
  258. if (to->tail)
  259. to->tail->next = from->head;
  260. else
  261. to->head = from->head;
  262. from->head->prev = to->tail;
  263. to->tail = from->tail;
  264. from->head = from->tail = NULL;
  265. to->count += from->count;
  266. from->count = 0;
  267. to->count_up += from->count_up;
  268. from->count_up = 0;
  269. }
  270. K_LIST *_k_free_list(K_LIST *list, KLIST_FFL_ARGS)
  271. {
  272. int i;
  273. if (list->is_store) {
  274. quithere(1, "List %s can't %s() a store" KLIST_FFL,
  275. list->name, __func__, KLIST_FFL_PASS);
  276. }
  277. for (i = 0; i < list->item_mem_count; i++)
  278. free(list->item_memory[i]);
  279. free(list->item_memory);
  280. for (i = 0; i < list->data_mem_count; i++)
  281. free(list->data_memory[i]);
  282. free(list->data_memory);
  283. cglock_destroy(list->lock);
  284. free(list->lock);
  285. free(list);
  286. return NULL;
  287. }
  288. K_STORE *_k_free_store(K_STORE *store, KLIST_FFL_ARGS)
  289. {
  290. if (!(store->is_store)) {
  291. quithere(1, "Store %s can't %s() the list" KLIST_FFL,
  292. store->name, __func__, KLIST_FFL_PASS);
  293. }
  294. free(store);
  295. return NULL;
  296. }