libwebsockets
Lightweight C library for HTML5 websockets
lws-lwsac.h
1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
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
49
50struct lwsac;
51typedef unsigned char * lwsac_cached_file_t;
52
53
54#define lws_list_ptr_container(P,T,M) ((T *)((char *)(P) - offsetof(T, M)))
55
56/*
57 * linked-list helper that's commonly useful to manage lists of things
58 * allocated using lwsac.
59 *
60 * These lists point to their corresponding "next" member in the target, NOT
61 * the original containing struct. To get the containing struct, you must use
62 * lws_list_ptr_container() to convert.
63 *
64 * It's like that because it means we no longer have to have the next pointer
65 * at the start of the struct, and we can have the same struct on multiple
66 * linked-lists with everything held in the struct itself.
67 */
68typedef void * lws_list_ptr;
69
70/*
71 * optional sorting callback called by lws_list_ptr_insert() to sort the right
72 * things inside the opqaue struct being sorted / inserted on the list.
73 */
74typedef int (*lws_list_ptr_sort_func_t)(lws_list_ptr a, lws_list_ptr b);
75
76#define lws_list_ptr_advance(_lp) _lp = *((void **)_lp)
77
78/* sort may be NULL if you don't care about order */
79LWS_VISIBLE LWS_EXTERN void
80lws_list_ptr_insert(lws_list_ptr *phead, lws_list_ptr *add,
81 lws_list_ptr_sort_func_t sort);
82
83
103LWS_VISIBLE LWS_EXTERN void *
104lwsac_use(struct lwsac **head, size_t ensure, size_t chunk_size);
105
125LWS_VISIBLE LWS_EXTERN void *
126lwsac_use_backfill(struct lwsac **head, size_t ensure, size_t chunk_size);
127
143LWS_VISIBLE LWS_EXTERN void *
144lwsac_use_zero(struct lwsac **head, size_t ensure, size_t chunk_size);
145
146#define lwsac_use_zeroed lwsac_use_zero
147
156LWS_VISIBLE LWS_EXTERN void
157lwsac_free(struct lwsac **head);
158
159/*
160 * Optional helpers useful for where consumers may need to defer destruction
161 * until all consumers are finished with the lwsac
162 */
163
177LWS_VISIBLE LWS_EXTERN void
178lwsac_detach(struct lwsac **head);
179
187LWS_VISIBLE LWS_EXTERN void
188lwsac_reference(struct lwsac *head);
189
198LWS_VISIBLE LWS_EXTERN void
199lwsac_unreference(struct lwsac **head);
200
231LWS_VISIBLE LWS_EXTERN int
232lwsac_extend(struct lwsac *head, size_t amount);
233
234/* helpers to keep a file cached in memory */
235
236LWS_VISIBLE LWS_EXTERN void
237lwsac_use_cached_file_start(lwsac_cached_file_t cache);
238
239LWS_VISIBLE LWS_EXTERN void
240lwsac_use_cached_file_end(lwsac_cached_file_t *cache);
241
242LWS_VISIBLE LWS_EXTERN void
243lwsac_use_cached_file_detach(lwsac_cached_file_t *cache);
244
245LWS_VISIBLE LWS_EXTERN int
246lwsac_cached_file(const char *filepath, lwsac_cached_file_t *cache,
247 size_t *len);
248
249/* more advanced helpers */
250
251/* offset from lac to start of payload, first = 1 = first lac in chain */
252LWS_VISIBLE LWS_EXTERN size_t
253lwsac_sizeof(int first);
254
255LWS_VISIBLE LWS_EXTERN size_t
256lwsac_get_tail_pos(struct lwsac *lac);
257
258LWS_VISIBLE LWS_EXTERN struct lwsac *
259lwsac_get_next(struct lwsac *lac);
260
261LWS_VISIBLE LWS_EXTERN size_t
262lwsac_align(size_t length);
263
264LWS_VISIBLE LWS_EXTERN void
265lwsac_info(struct lwsac *head);
266
267LWS_VISIBLE LWS_EXTERN uint64_t
268lwsac_total_alloc(struct lwsac *head);
269
270LWS_VISIBLE LWS_EXTERN uint64_t
271lwsac_total_overhead(struct lwsac *head);
272
287LWS_VISIBLE LWS_EXTERN uint8_t *
288lwsac_scan_extant(struct lwsac *head, uint8_t *find, size_t len, int nul);
289
LWS_VISIBLE LWS_EXTERN uint8_t * lwsac_scan_extant(struct lwsac *head, uint8_t *find, size_t len, int nul)
LWS_VISIBLE LWS_EXTERN void * lwsac_use_zero(struct lwsac **head, size_t ensure, size_t chunk_size)
LWS_VISIBLE LWS_EXTERN void lwsac_free(struct lwsac **head)
LWS_VISIBLE LWS_EXTERN void lwsac_reference(struct lwsac *head)
LWS_VISIBLE LWS_EXTERN void * lwsac_use_backfill(struct lwsac **head, size_t ensure, size_t chunk_size)
LWS_VISIBLE LWS_EXTERN void * lwsac_use(struct lwsac **head, size_t ensure, size_t chunk_size)
LWS_VISIBLE LWS_EXTERN void lwsac_unreference(struct lwsac **head)
LWS_VISIBLE LWS_EXTERN int lwsac_extend(struct lwsac *head, size_t amount)
LWS_VISIBLE LWS_EXTERN void lwsac_detach(struct lwsac **head)