klist.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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_insert_before(K_LIST *list, K_ITEM *item, K_ITEM *before, 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 (!before) {
  210. quithere(1, "%s() (%s) can't before a null item" KLIST_FFL,
  211. __func__, list->name, KLIST_FFL_PASS);
  212. }
  213. item->next = before;
  214. item->prev = before->prev;
  215. if (before->prev)
  216. before->prev->next = item;
  217. else
  218. list->head = item;
  219. before->prev = item;
  220. list->count++;
  221. list->count_up++;
  222. }
  223. void _k_insert_after(K_LIST *list, K_ITEM *item, K_ITEM *after, KLIST_FFL_ARGS)
  224. {
  225. if (item->name != list->name) {
  226. quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
  227. list->name, __func__, item->name, KLIST_FFL_PASS);
  228. }
  229. if (!after) {
  230. quithere(1, "%s() (%s) can't after a null item" KLIST_FFL,
  231. __func__, list->name, KLIST_FFL_PASS);
  232. }
  233. item->prev = after;
  234. item->next = after->next;
  235. if (after->next)
  236. after->next->prev = item;
  237. else {
  238. if (list->do_tail)
  239. list->tail = item;
  240. }
  241. after->next = item;
  242. list->count++;
  243. list->count_up++;
  244. }
  245. void _k_unlink_item(K_LIST *list, K_ITEM *item, KLIST_FFL_ARGS)
  246. {
  247. if (item->name != list->name) {
  248. quithere(1, "List %s can't %s() a %s item" KLIST_FFL,
  249. list->name, __func__, item->name, KLIST_FFL_PASS);
  250. }
  251. if (item->prev)
  252. item->prev->next = item->next;
  253. if (item->next)
  254. item->next->prev = item->prev;
  255. if (list->head == item)
  256. list->head = item->next;
  257. if (list->do_tail) {
  258. if (list->tail == item)
  259. list->tail = item->prev;
  260. }
  261. item->prev = item->next = NULL;
  262. list->count--;
  263. }
  264. void _k_list_transfer_to_head(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS)
  265. {
  266. if (from->name != to->name) {
  267. quithere(1, "List %s can't %s() to a %s list" KLIST_FFL,
  268. from->name, __func__, to->name, KLIST_FFL_PASS);
  269. }
  270. if (!(from->do_tail)) {
  271. quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
  272. from->name, __func__, KLIST_FFL_PASS);
  273. }
  274. if (!(from->head))
  275. return;
  276. if (to->head)
  277. to->head->prev = from->tail;
  278. else
  279. to->tail = from->tail;
  280. from->tail->next = to->head;
  281. to->head = from->head;
  282. from->head = from->tail = NULL;
  283. to->count += from->count;
  284. from->count = 0;
  285. to->count_up += from->count_up;
  286. from->count_up = 0;
  287. }
  288. void _k_list_transfer_to_tail(K_LIST *from, K_LIST *to, KLIST_FFL_ARGS)
  289. {
  290. if (from->name != to->name) {
  291. quithere(1, "List %s can't %s() to a %s list" KLIST_FFL,
  292. from->name, __func__, to->name, KLIST_FFL_PASS);
  293. }
  294. if (!(from->do_tail)) {
  295. quithere(1, "List %s can't %s() - do_tail is false" KLIST_FFL,
  296. from->name, __func__, KLIST_FFL_PASS);
  297. }
  298. if (!(from->head))
  299. return;
  300. if (to->tail)
  301. to->tail->next = from->head;
  302. else
  303. to->head = from->head;
  304. from->head->prev = to->tail;
  305. to->tail = from->tail;
  306. from->head = from->tail = NULL;
  307. to->count += from->count;
  308. from->count = 0;
  309. to->count_up += from->count_up;
  310. from->count_up = 0;
  311. }
  312. K_LIST *_k_free_list(K_LIST *list, KLIST_FFL_ARGS)
  313. {
  314. int i;
  315. if (list->is_store) {
  316. quithere(1, "List %s can't %s() a store" KLIST_FFL,
  317. list->name, __func__, KLIST_FFL_PASS);
  318. }
  319. for (i = 0; i < list->item_mem_count; i++)
  320. free(list->item_memory[i]);
  321. free(list->item_memory);
  322. for (i = 0; i < list->data_mem_count; i++)
  323. free(list->data_memory[i]);
  324. free(list->data_memory);
  325. cglock_destroy(list->lock);
  326. free(list->lock);
  327. free(list);
  328. return NULL;
  329. }
  330. K_STORE *_k_free_store(K_STORE *store, KLIST_FFL_ARGS)
  331. {
  332. if (!(store->is_store)) {
  333. quithere(1, "Store %s can't %s() the list" KLIST_FFL,
  334. store->name, __func__, KLIST_FFL_PASS);
  335. }
  336. free(store);
  337. return NULL;
  338. }