1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci */
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ci/*
26d4afb5ceSopenharmony_ci * lws_dsh (Disordered Shared Heap) is an opaque abstraction supporting a single
27d4afb5ceSopenharmony_ci * linear buffer (overallocated at end of the lws_dsh_t) which may contain
28d4afb5ceSopenharmony_ci * multiple kinds of packets that are retired out of order, and tracked by kind.
29d4afb5ceSopenharmony_ci *
30d4afb5ceSopenharmony_ci * Each kind of packet has an lws_dll2 list of its kind of packets and acts as
31d4afb5ceSopenharmony_ci * a FIFO; packets of a particular type are always retired in order.  But there
32d4afb5ceSopenharmony_ci * is no requirement about the order types are retired matching the original
33d4afb5ceSopenharmony_ci * order they arrived.
34d4afb5ceSopenharmony_ci *
35d4afb5ceSopenharmony_ci * Gaps are tracked as just another kind of "packet" list.
36d4afb5ceSopenharmony_ci *
37d4afb5ceSopenharmony_ci * "allocations" (including gaps) are prepended by an lws_dsh_object_t.
38d4afb5ceSopenharmony_ci *
39d4afb5ceSopenharmony_ci * dsh may themselves be on an lws_dll2_owner list, and under memory pressure
40d4afb5ceSopenharmony_ci * allocate into other buffers on the list.
41d4afb5ceSopenharmony_ci *
42d4afb5ceSopenharmony_ci * All management structures exist inside the allocated buffer.
43d4afb5ceSopenharmony_ci */
44d4afb5ceSopenharmony_ci
45d4afb5ceSopenharmony_ci/**
46d4afb5ceSopenharmony_ci * lws_dsh_create() - Allocate a DSH buffer
47d4afb5ceSopenharmony_ci *
48d4afb5ceSopenharmony_ci * \param owner: the owning list this dsh belongs on, or NULL if standalone
49d4afb5ceSopenharmony_ci * \param buffer_size: the allocation in bytes
50d4afb5ceSopenharmony_ci * \param count_kinds: how many separately-tracked fifos use the buffer
51d4afb5ceSopenharmony_ci *
52d4afb5ceSopenharmony_ci * This makes a single heap allocation that includes internal tracking objects
53d4afb5ceSopenharmony_ci * in the buffer.  Sub-allocated objects are bound to a "kind" index and
54d4afb5ceSopenharmony_ci * managed via a FIFO for each kind.
55d4afb5ceSopenharmony_ci *
56d4afb5ceSopenharmony_ci * Every "kind" of allocation shares the same buffer space.
57d4afb5ceSopenharmony_ci *
58d4afb5ceSopenharmony_ci * Multiple buffers may be bound together in an lws_dll2 list, and if an
59d4afb5ceSopenharmony_ci * allocation cannot be satisfied by the local buffer, space can be borrowed
60d4afb5ceSopenharmony_ci * from other dsh in the same list (the local dsh FIFO tracks these "foreign"
61d4afb5ceSopenharmony_ci * allocations as if they were local).
62d4afb5ceSopenharmony_ci *
63d4afb5ceSopenharmony_ci * Returns an opaque pointer to the dsh, or NULL if allocation failed.
64d4afb5ceSopenharmony_ci */
65d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN struct lws_dsh *
66d4afb5ceSopenharmony_cilws_dsh_create(lws_dll2_owner_t *owner, size_t buffer_size, int count_kinds);
67d4afb5ceSopenharmony_ci
68d4afb5ceSopenharmony_ci/**
69d4afb5ceSopenharmony_ci * lws_dsh_destroy() - Destroy a DSH buffer
70d4afb5ceSopenharmony_ci *
71d4afb5ceSopenharmony_ci * \param pdsh: pointer to the dsh pointer
72d4afb5ceSopenharmony_ci *
73d4afb5ceSopenharmony_ci * Deallocates the DSH and sets *pdsh to NULL.
74d4afb5ceSopenharmony_ci *
75d4afb5ceSopenharmony_ci * Before destruction, any foreign buffer usage on the part of this dsh are
76d4afb5ceSopenharmony_ci * individually freed.  All dsh on the same list are walked and checked if they
77d4afb5ceSopenharmony_ci * have their own foreign allocations on the dsh buffer being destroyed.  If so,
78d4afb5ceSopenharmony_ci * it attempts to migrate the allocation to a dsh that is not currently being
79d4afb5ceSopenharmony_ci * destroyed.  If all else fails (basically the buffer memory is being shrunk)
80d4afb5ceSopenharmony_ci * unmigratable objects are cleanly destroyed.
81d4afb5ceSopenharmony_ci */
82d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN void
83d4afb5ceSopenharmony_cilws_dsh_destroy(struct lws_dsh **pdsh);
84d4afb5ceSopenharmony_ci
85d4afb5ceSopenharmony_ci/**
86d4afb5ceSopenharmony_ci * lws_dsh_alloc_tail() - make a suballocation inside a dsh
87d4afb5ceSopenharmony_ci *
88d4afb5ceSopenharmony_ci * \param dsh: the dsh tracking the allocation
89d4afb5ceSopenharmony_ci * \param kind: the kind of allocation
90d4afb5ceSopenharmony_ci * \param src1: the first source data to copy
91d4afb5ceSopenharmony_ci * \param size1: the size of the first source data
92d4afb5ceSopenharmony_ci * \param src2: the second source data to copy (after the first), or NULL
93d4afb5ceSopenharmony_ci * \param size2: the size of the second source data
94d4afb5ceSopenharmony_ci *
95d4afb5ceSopenharmony_ci * Allocates size1 + size2 bytes in a dsh (it prefers the given dsh but will
96d4afb5ceSopenharmony_ci * borrow space from other dsh on the same list if necessary) and copies size1
97d4afb5ceSopenharmony_ci * bytes into it from src1, followed by size2 bytes from src2 if src2 isn't
98d4afb5ceSopenharmony_ci * NULL.  The actual suballocation is a bit larger because of alignment and a
99d4afb5ceSopenharmony_ci * prepended management header.
100d4afb5ceSopenharmony_ci *
101d4afb5ceSopenharmony_ci * The suballocation is added to the kind-specific FIFO at the tail.
102d4afb5ceSopenharmony_ci */
103d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN int
104d4afb5ceSopenharmony_cilws_dsh_alloc_tail(struct lws_dsh *dsh, int kind, const void *src1,
105d4afb5ceSopenharmony_ci		   size_t size1, const void *src2, size_t size2);
106d4afb5ceSopenharmony_ci
107d4afb5ceSopenharmony_ci/**
108d4afb5ceSopenharmony_ci * lws_dsh_free() - free a suballocation from the dsh
109d4afb5ceSopenharmony_ci *
110d4afb5ceSopenharmony_ci * \param obj: a pointer to a void * that pointed to the allocated payload
111d4afb5ceSopenharmony_ci *
112d4afb5ceSopenharmony_ci * This returns the space used by \p obj in the dsh buffer to the free list
113d4afb5ceSopenharmony_ci * of the dsh the allocation came from.
114d4afb5ceSopenharmony_ci */
115d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN void
116d4afb5ceSopenharmony_cilws_dsh_free(void **obj);
117d4afb5ceSopenharmony_ci
118d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN size_t
119d4afb5ceSopenharmony_cilws_dsh_get_size(struct lws_dsh *dsh, int kind);
120d4afb5ceSopenharmony_ci
121d4afb5ceSopenharmony_ci/**
122d4afb5ceSopenharmony_ci * lws_dsh_get_head() - get the head allocation inside the dsh
123d4afb5ceSopenharmony_ci *
124d4afb5ceSopenharmony_ci * \param dsh: the dsh tracking the allocation
125d4afb5ceSopenharmony_ci * \param kind: the kind of allocation
126d4afb5ceSopenharmony_ci * \param obj: pointer to a void * to be set to the payload
127d4afb5ceSopenharmony_ci * \param size: set to the size of the allocation
128d4afb5ceSopenharmony_ci *
129d4afb5ceSopenharmony_ci * This gets the "next" object in the kind FIFO for the dsh, and returns 0 if
130d4afb5ceSopenharmony_ci * any.  If none, returns nonzero.
131d4afb5ceSopenharmony_ci *
132d4afb5ceSopenharmony_ci * This is nondestructive of the fifo or the payload.  Use lws_dsh_free on
133d4afb5ceSopenharmony_ci * obj to remove the entry from the kind fifo and return the payload to the
134d4afb5ceSopenharmony_ci * free list.
135d4afb5ceSopenharmony_ci */
136d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN int
137d4afb5ceSopenharmony_cilws_dsh_get_head(struct lws_dsh *dsh, int kind, void **obj, size_t *size);
138d4afb5ceSopenharmony_ci
139d4afb5ceSopenharmony_ci/**
140d4afb5ceSopenharmony_ci * lws_dsh_describe() - DEBUG BUILDS ONLY dump the dsh to the logs
141d4afb5ceSopenharmony_ci *
142d4afb5ceSopenharmony_ci * \param dsh: the dsh to dump
143d4afb5ceSopenharmony_ci * \param desc: text that appears at the top of the dump
144d4afb5ceSopenharmony_ci *
145d4afb5ceSopenharmony_ci * Useful information for debugging lws_dsh
146d4afb5ceSopenharmony_ci */
147d4afb5ceSopenharmony_ciLWS_VISIBLE LWS_EXTERN void
148d4afb5ceSopenharmony_cilws_dsh_describe(struct lws_dsh *dsh, const char *desc);
149