lwsac.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #include "private-lib-misc-lwsac.h"
  26. void
  27. lws_list_ptr_insert(lws_list_ptr *head, lws_list_ptr *add,
  28. lws_list_ptr_sort_func_t sort_func)
  29. {
  30. while (sort_func && *head) {
  31. if (sort_func(add, *head) <= 0)
  32. break;
  33. head = *head;
  34. }
  35. *add = *head;
  36. *head = add;
  37. }
  38. size_t
  39. lwsac_align(size_t length)
  40. {
  41. size_t align = sizeof(int *);
  42. if (length & (align - 1))
  43. length += align - (length & (align - 1));
  44. return length;
  45. }
  46. size_t
  47. lwsac_sizeof(int first)
  48. {
  49. return sizeof(struct lwsac) + (first ? sizeof(struct lwsac_head) : 0);
  50. }
  51. size_t
  52. lwsac_get_tail_pos(struct lwsac *lac)
  53. {
  54. return lac->ofs;
  55. }
  56. struct lwsac *
  57. lwsac_get_next(struct lwsac *lac)
  58. {
  59. return lac->next;
  60. }
  61. int
  62. lwsac_extend(struct lwsac *head, int amount)
  63. {
  64. struct lwsac_head *lachead;
  65. struct lwsac *bf;
  66. assert(head);
  67. lachead = (struct lwsac_head *)&head[1];
  68. bf = lachead->curr;
  69. assert(bf);
  70. if (bf->alloc_size - bf->ofs < lwsac_align(amount))
  71. return 1;
  72. /* memset so constant folding never sees uninitialized data */
  73. memset(((uint8_t *)bf) + bf->ofs, 0, lwsac_align(amount));
  74. bf->ofs += lwsac_align(amount);
  75. return 0;
  76. }
  77. static void *
  78. _lwsac_use(struct lwsac **head, size_t ensure, size_t chunk_size, char backfill)
  79. {
  80. struct lwsac_head *lachead = NULL;
  81. size_t ofs, alloc, al, hp;
  82. struct lwsac *bf = *head;
  83. if (bf)
  84. lachead = (struct lwsac_head *)&bf[1];
  85. al = lwsac_align(ensure);
  86. /* backfill into earlier chunks if that is allowed */
  87. if (backfill)
  88. /*
  89. * check if anything can take it, from the start
  90. */
  91. while (bf) {
  92. if (bf->alloc_size - bf->ofs >= ensure)
  93. goto do_use;
  94. bf = bf->next;
  95. }
  96. else {
  97. /*
  98. * If there's a current chunk, just check if he can take it
  99. */
  100. if (lachead && lachead->curr) {
  101. bf = lachead->curr;
  102. if (bf->alloc_size - bf->ofs >= ensure)
  103. goto do_use;
  104. }
  105. }
  106. /* nothing can currently take it... so we must allocate */
  107. hp = sizeof(*bf); /* always need the normal header part... */
  108. if (!*head)
  109. hp += sizeof(struct lwsac_head);
  110. if (!chunk_size)
  111. alloc = LWSAC_CHUNK_SIZE + hp;
  112. else
  113. alloc = chunk_size + hp;
  114. /*
  115. * If we get asked for something outside our expectation,
  116. * increase the allocation to meet it
  117. */
  118. if (al >= alloc - hp)
  119. alloc = al + hp;
  120. lwsl_debug("%s: alloc %d for %d\n", __func__, (int)alloc, (int)ensure);
  121. bf = malloc(alloc);
  122. if (!bf) {
  123. lwsl_err("%s: OOM trying to alloc %llud\n", __func__,
  124. (unsigned long long)alloc);
  125. return NULL;
  126. }
  127. /*
  128. * belabouring the point... ofs is aligned to the platform's
  129. * generic struct alignment at the start then
  130. */
  131. bf->ofs = sizeof(*bf);
  132. if (!*head) {
  133. /*
  134. * We are the first, head, entry...
  135. */
  136. *head = bf;
  137. /*
  138. * ... allocate for the special head block
  139. */
  140. bf->ofs += sizeof(*lachead);
  141. lachead = (struct lwsac_head *)&bf[1];
  142. memset(lachead, 0, sizeof(*lachead));
  143. } else
  144. if (lachead->curr)
  145. lachead->curr->next = bf;
  146. lachead->curr = bf;
  147. bf->head = *head;
  148. bf->next = NULL;
  149. bf->alloc_size = alloc;
  150. lachead->total_alloc_size += alloc;
  151. lachead->total_blocks++;
  152. do_use:
  153. ofs = bf->ofs;
  154. if (al > ensure)
  155. /* zero down the alignment padding part */
  156. memset((char *)bf + ofs + ensure, 0, al - ensure);
  157. bf->ofs += al;
  158. if (bf->ofs >= bf->alloc_size)
  159. bf->ofs = bf->alloc_size;
  160. return (char *)bf + ofs;
  161. }
  162. void *
  163. lwsac_use(struct lwsac **head, size_t ensure, size_t chunk_size)
  164. {
  165. return _lwsac_use(head, ensure, chunk_size, 0);
  166. }
  167. void *
  168. lwsac_use_backfill(struct lwsac **head, size_t ensure, size_t chunk_size)
  169. {
  170. return _lwsac_use(head, ensure, chunk_size, 1);
  171. }
  172. uint8_t *
  173. lwsac_scan_extant(struct lwsac *head, uint8_t *find, size_t len, int nul)
  174. {
  175. while (head) {
  176. uint8_t *pos = (uint8_t *)&head[1],
  177. *end = ((uint8_t *)head) + head->ofs - len;
  178. if (head->ofs - sizeof(*head) >= len)
  179. while (pos < end) {
  180. if (*pos == *find && (!nul || !pos[len]) &&
  181. pos[len - 1] == find[len - 1] &&
  182. !memcmp(pos, find, len))
  183. /* found the blob */
  184. return pos;
  185. pos++;
  186. }
  187. head = head->next;
  188. }
  189. return NULL;
  190. }
  191. uint64_t
  192. lwsac_total_overhead(struct lwsac *head)
  193. {
  194. uint64_t overhead = 0;
  195. while (head) {
  196. overhead += (head->alloc_size - head->ofs) + sizeof(*head);
  197. head = head->next;
  198. }
  199. return overhead;
  200. }
  201. void *
  202. lwsac_use_zero(struct lwsac **head, size_t ensure, size_t chunk_size)
  203. {
  204. void *p = lwsac_use(head, ensure, chunk_size);
  205. if (p)
  206. memset(p, 0, ensure);
  207. return p;
  208. }
  209. void
  210. lwsac_free(struct lwsac **head)
  211. {
  212. struct lwsac *it = *head;
  213. *head = NULL;
  214. lwsl_debug("%s: head %p\n", __func__, *head);
  215. while (it) {
  216. struct lwsac *tmp = it->next;
  217. free(it);
  218. it = tmp;
  219. }
  220. }
  221. void
  222. lwsac_info(struct lwsac *head)
  223. {
  224. #if _LWS_ENABLED_LOGS & LLL_DEBUG
  225. struct lwsac_head *lachead;
  226. if (!head) {
  227. lwsl_debug("%s: empty\n", __func__);
  228. return;
  229. }
  230. lachead = (struct lwsac_head *)&head[1];
  231. lwsl_debug("%s: lac %p: %dKiB in %d blocks\n", __func__, head,
  232. (int)(lachead->total_alloc_size >> 10), lachead->total_blocks);
  233. #endif
  234. }
  235. uint64_t
  236. lwsac_total_alloc(struct lwsac *head)
  237. {
  238. struct lwsac_head *lachead;
  239. if (!head)
  240. return 0;
  241. lachead = (struct lwsac_head *)&head[1];
  242. return lachead->total_alloc_size;
  243. }
  244. void
  245. lwsac_reference(struct lwsac *head)
  246. {
  247. struct lwsac_head *lachead = (struct lwsac_head *)&head[1];
  248. lachead->refcount++;
  249. lwsl_debug("%s: head %p: (det %d) refcount -> %d\n",
  250. __func__, head, lachead->detached, lachead->refcount);
  251. }
  252. void
  253. lwsac_unreference(struct lwsac **head)
  254. {
  255. struct lwsac_head *lachead;
  256. if (!(*head))
  257. return;
  258. lachead = (struct lwsac_head *)&(*head)[1];
  259. if (!lachead->refcount)
  260. lwsl_warn("%s: refcount going below zero\n", __func__);
  261. lachead->refcount--;
  262. lwsl_debug("%s: head %p: (det %d) refcount -> %d\n",
  263. __func__, *head, lachead->detached, lachead->refcount);
  264. if (lachead->detached && !lachead->refcount) {
  265. lwsl_debug("%s: head %p: FREED\n", __func__, *head);
  266. lwsac_free(head);
  267. }
  268. }
  269. void
  270. lwsac_detach(struct lwsac **head)
  271. {
  272. struct lwsac_head *lachead;
  273. if (!(*head))
  274. return;
  275. lachead = (struct lwsac_head *)&(*head)[1];
  276. lachead->detached = 1;
  277. if (!lachead->refcount) {
  278. lwsl_debug("%s: head %p: FREED\n", __func__, *head);
  279. lwsac_free(head);
  280. } else
  281. lwsl_debug("%s: head %p: refcount %d: Marked as detached\n",
  282. __func__, *head, lachead->refcount);
  283. }