-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.c
203 lines (183 loc) · 6.49 KB
/
memory.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright (c) 2014 DeNA Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "h2o/memory.h"
struct st_h2o_mempool_direct_t {
struct st_h2o_mempool_direct_t *next;
size_t _dummy; /* align to 2*sizeof(void*) */
char bytes[1];
};
struct st_h2o_mempool_shared_ref_t {
struct st_h2o_mempool_shared_ref_t *next;
struct st_h2o_mempool_shared_entry_t *entry;
};
void h2o_fatal(const char *msg)
{
fprintf(stderr, "fatal:%s\n", msg);
abort();
}
void h2o_mempool_init(h2o_mempool_t *pool)
{
pool->chunks = &pool->_first_chunk;
pool->directs = NULL;
pool->shared_refs = NULL;
pool->_first_chunk.next = NULL;
pool->_first_chunk.offset = 0;
}
void h2o_mempool_clear(h2o_mempool_t *pool)
{
/* release the refcounted chunks */
if (pool->shared_refs != NULL) {
struct st_h2o_mempool_shared_ref_t *ref = pool->shared_refs;
do {
h2o_mempool_release_shared(ref->entry->bytes);
} while ((ref = ref->next) != NULL);
pool->shared_refs = NULL;
}
/* release the direct chunks */
if (pool->directs != NULL) {
struct st_h2o_mempool_direct_t *direct = pool->directs, *next;
do {
next = direct->next;
free(direct);
} while ((direct = next) != NULL);
pool->directs = NULL;
}
/* free chunks, and reset the first chunk */
while (pool->chunks != &pool->_first_chunk) {
h2o_mempool_chunk_t *next = pool->chunks->next;
free(pool->chunks);
pool->chunks = next;
}
pool->_first_chunk.next = NULL;
pool->_first_chunk.offset = 0;
}
void *h2o_mempool_alloc(h2o_mempool_t *pool, size_t sz)
{
void *ret;
if (sz >= sizeof(pool->chunks->bytes) / 4) {
/* allocate large requests directly */
struct st_h2o_mempool_direct_t *newp = h2o_malloc(offsetof(struct st_h2o_mempool_direct_t, bytes) + sz);
newp->next = pool->directs;
pool->directs = newp;
return newp->bytes;
}
/* 16-bytes rounding */
sz = (sz + 15) & ~15;
if (sizeof(pool->chunks->bytes) < pool->chunks->offset + sz) {
/* allocate new chunk */
h2o_mempool_chunk_t *newp = h2o_malloc(sizeof(*newp));
newp->next = pool->chunks;
newp->offset = 0;
pool->chunks = newp;
}
ret = pool->chunks->bytes + pool->chunks->offset;
pool->chunks->offset += sz;
return ret;
}
static void link_shared(h2o_mempool_t *pool, struct st_h2o_mempool_shared_entry_t *entry)
{
struct st_h2o_mempool_shared_ref_t *ref = h2o_mempool_alloc(pool, sizeof(struct st_h2o_mempool_shared_ref_t));
ref->entry = entry;
ref->next = pool->shared_refs;
pool->shared_refs = ref;
}
void *h2o_mempool_alloc_shared(h2o_mempool_t *pool, size_t sz, void (*dispose)(void *))
{
struct st_h2o_mempool_shared_entry_t *entry = h2o_malloc(offsetof(struct st_h2o_mempool_shared_entry_t, bytes) + sz);
entry->refcnt = 1;
entry->dispose = dispose;
if (pool != NULL)
link_shared(pool, entry);
return entry->bytes;
}
void h2o_mempool_link_shared(h2o_mempool_t *pool, void *p)
{
h2o_mempool_addref_shared(p);
link_shared(pool, H2O_STRUCT_FROM_MEMBER(h2o_mempool_shared_entry_t, bytes, p));
}
const h2o_input_buffer_t h2o__null_input_buffer = {};
h2o_buf_t h2o_reserve_input_buffer(h2o_input_buffer_t **_inbuf, size_t min_guarantee)
{
h2o_input_buffer_t *inbuf = *_inbuf;
h2o_buf_t ret;
if (inbuf == &h2o__null_input_buffer) {
inbuf = h2o_malloc(offsetof(h2o_input_buffer_t, _buf) + min_guarantee * 2);
*_inbuf = inbuf;
inbuf->size = 0;
inbuf->bytes = inbuf->_buf;
inbuf->_capacity = min_guarantee * 2;
} else {
if (inbuf->bytes != inbuf->_buf) {
assert(inbuf->size != 0);
memmove(inbuf->_buf, inbuf->bytes, inbuf->size);
inbuf->bytes = inbuf->_buf;
}
if (inbuf->_capacity - inbuf->size < min_guarantee) {
do {
inbuf->_capacity *= 2;
} while (inbuf->_capacity - inbuf->size < min_guarantee);
inbuf = h2o_realloc(inbuf, offsetof(h2o_input_buffer_t, _buf) + inbuf->_capacity);
inbuf->bytes = inbuf->_buf;
*_inbuf = inbuf;
}
/* TODO shrink the size if possible */
}
ret.base = inbuf->bytes + inbuf->size;
ret.len = inbuf->_capacity - inbuf->size;
return ret;
}
void h2o_consume_input_buffer(h2o_input_buffer_t **_inbuf, size_t delta)
{
h2o_input_buffer_t *inbuf = *_inbuf;
if (delta != 0) {
assert(inbuf != &h2o__null_input_buffer);
if (inbuf->size == delta) {
inbuf->size = 0;
inbuf->bytes = inbuf->_buf;
} else {
inbuf->size -= delta;
inbuf->bytes += delta;
}
}
}
void h2o_vector__expand(h2o_mempool_t *pool, h2o_vector_t *vector, size_t element_size, size_t new_capacity)
{
void *new_entries;
assert(vector->capacity < new_capacity);
if (vector->capacity == 0)
vector->capacity = 4;
while (vector->capacity < new_capacity)
vector->capacity *= 2;
if (pool != NULL) {
new_entries = h2o_mempool_alloc(pool, element_size * vector->capacity);
memcpy(new_entries, vector->entries, element_size * vector->size);
} else {
new_entries = h2o_realloc(vector->entries, element_size * vector->capacity);
}
vector->entries = new_entries;
}