"README.md" did not exist on "f77779b4fc4247a1f5330e4fc65d2bb77847f6fe"
Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* libwebsockets - lws alloc chunk
*
* Copyright (C) 2018 Andy Green <andy@warmcat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation:
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
#include "core/private.h"
#include "misc/lwsac/private.h"
void
lws_list_ptr_insert(lws_list_ptr *head, lws_list_ptr *add,
lws_list_ptr_sort_func_t sort_func)
{
while (sort_func && *head) {
if (sort_func(add, *head) <= 0)
break;
head = *head;
}
*add = *head;
*head = add;
}
size_t
lwsac_align(size_t length)
{
size_t align = sizeof(int *);
if (length & (align - 1))
length += align - (length & (align - 1));
return length;
}
size_t
lwsac_sizeof(void)
{
return sizeof(struct lwsac);
}
size_t
lwsac_get_tail_pos(struct lwsac *lac)
{
return lac->ofs;
}
struct lwsac *
lwsac_get_next(struct lwsac *lac)
{
return lac->next;
}
void *
lwsac_use(struct lwsac **head, size_t ensure, size_t chunk_size)
{
struct lwsac *chunk;
size_t ofs, alloc;
/* ensure there's a chunk and enough space in it for this name */
if (!*head || (*head)->curr->alloc_size - (*head)->curr->ofs < ensure) {
if (!chunk_size)
alloc = LWSAC_CHUNK_SIZE + sizeof(*chunk);
else
alloc = chunk_size + sizeof(*chunk);
/*
* If we get asked for something outside our expectation,
* allocate to meet it
*/
if (ensure >= alloc - sizeof(*chunk))
alloc = ensure + sizeof(*chunk);
chunk = malloc(alloc);
if (!chunk) {
lwsl_err("%s: OOM trying to alloc %llud\n", __func__,
(unsigned long long)alloc);
return NULL;
}
if (!*head) {
*head = chunk;
chunk->total_alloc_size = 0;
chunk->total_blocks = 0;
}
else
(*head)->curr->next = chunk;
(*head)->curr = chunk;
(*head)->curr->head = *head;
chunk->next = NULL;
chunk->alloc_size = alloc;
chunk->detached = 0;
chunk->refcount = 0;
(*head)->total_alloc_size += alloc;
(*head)->total_blocks++;
/*
* belabouring the point... ofs is aligned to the platform's
* generic struct alignment at the start then
*/
(*head)->curr->ofs = sizeof(*chunk);
}
ofs = (*head)->curr->ofs;
(*head)->curr->ofs += lwsac_align(ensure);
if ((*head)->curr->ofs >= (*head)->curr->alloc_size)
(*head)->curr->ofs = (*head)->curr->alloc_size;
return (char *)(*head)->curr + ofs;
}
void
lwsac_free(struct lwsac **head)
{
struct lwsac *it = *head;
while (it) {
struct lwsac *tmp = it->next;
free(it);
it = tmp;
}
*head = NULL;
}
void
lwsac_info(struct lwsac *head)
{
lwsl_notice("%s: lac %p: %dKiB in %d blocks\n", __func__, head,
(int)(head->total_alloc_size >> 10), head->total_blocks);
}
uint64_t
lwsac_total_alloc(struct lwsac *head)
{
return head->total_alloc_size;
}
void
lwsac_reference(struct lwsac *head)
{
head->refcount++;
}
void
lwsac_unreference(struct lwsac **head)
{