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 
50 struct lwsac;
51 typedef 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  */
68 typedef 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  */
74 typedef 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 */
79 LWS_VISIBLE LWS_EXTERN void
80 lws_list_ptr_insert(lws_list_ptr *phead, lws_list_ptr *add,
81  lws_list_ptr_sort_func_t sort);
82 
83 
103 LWS_VISIBLE LWS_EXTERN void *
104 lwsac_use(struct lwsac **head, size_t ensure, size_t chunk_size);
105 
125 LWS_VISIBLE LWS_EXTERN void *
126 lwsac_use_backfill(struct lwsac **head, size_t ensure, size_t chunk_size);
127 
143 LWS_VISIBLE LWS_EXTERN void *
144 lwsac_use_zero(struct lwsac **head, size_t ensure, size_t chunk_size);
145 
146 #define lwsac_use_zeroed lwsac_use_zero
147 
156 LWS_VISIBLE LWS_EXTERN void
157 lwsac_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 
177 LWS_VISIBLE LWS_EXTERN void
178 lwsac_detach(struct lwsac **head);
179 
187 LWS_VISIBLE LWS_EXTERN void
188 lwsac_reference(struct lwsac *head);
189 
198 LWS_VISIBLE LWS_EXTERN void
199 lwsac_unreference(struct lwsac **head);
200 
231 LWS_VISIBLE LWS_EXTERN int
232 lwsac_extend(struct lwsac *head, size_t amount);
233 
234 /* helpers to keep a file cached in memory */
235 
236 LWS_VISIBLE LWS_EXTERN void
237 lwsac_use_cached_file_start(lwsac_cached_file_t cache);
238 
239 LWS_VISIBLE LWS_EXTERN void
240 lwsac_use_cached_file_end(lwsac_cached_file_t *cache);
241 
242 LWS_VISIBLE LWS_EXTERN void
243 lwsac_use_cached_file_detach(lwsac_cached_file_t *cache);
244 
245 LWS_VISIBLE LWS_EXTERN int
246 lwsac_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 */
252 LWS_VISIBLE LWS_EXTERN size_t
253 lwsac_sizeof(int first);
254 
255 LWS_VISIBLE LWS_EXTERN size_t
256 lwsac_get_tail_pos(struct lwsac *lac);
257 
258 LWS_VISIBLE LWS_EXTERN struct lwsac *
259 lwsac_get_next(struct lwsac *lac);
260 
261 LWS_VISIBLE LWS_EXTERN size_t
262 lwsac_align(size_t length);
263 
264 LWS_VISIBLE LWS_EXTERN void
265 lwsac_info(struct lwsac *head);
266 
267 LWS_VISIBLE LWS_EXTERN uint64_t
268 lwsac_total_alloc(struct lwsac *head);
269 
270 LWS_VISIBLE LWS_EXTERN uint64_t
271 lwsac_total_overhead(struct lwsac *head);
272 
287 LWS_VISIBLE LWS_EXTERN uint8_t *
288 lwsac_scan_extant(struct lwsac *head, uint8_t *find, size_t len, int nul);
289 
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_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(struct lwsac **head, size_t ensure, size_t chunk_size)
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_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)