4
#include "util/easy_buf.h"
5
#include "util/easy_string.h"
6
#include "io/easy_log.h"
7
#include "easy_define.h"
8
#include "io/easy_connection.h"
14
easy_buf_t *easy_buf_create(easy_pool_t *pool, uint32_t size)
18
if ((b = (easy_buf_t *)easy_pool_calloc(pool, sizeof(easy_buf_t))) == NULL)
23
size = pool->end - pool->last;
25
if ((b->data = (char *)easy_pool_alloc(pool, size)) == NULL)
30
b->end = b->last + size;
33
easy_list_init(&b->node);
38
static uint64_t priv_pool_created = 0;
39
static uint64_t priv_pool_destroyed = 0;
40
static void easy_buf_free_private_pool(easy_buf_t * b, easy_pool_t * pool)
42
easy_debug_log("easy free residual buffer: %p remain=%d\n", b, easy_buf_len(b));
43
easy_pool_destroy(pool);
44
priv_pool_destroyed++;
47
easy_buf_t* easy_buf_clone_with_private_pool(easy_buf_t* b)
49
easy_buf_t* nb = NULL;
50
int64_t data_len = easy_buf_len(b);
51
easy_pool_t* pool = easy_pool_create(data_len + sizeof(*b) + sizeof(*pool));
53
nb = easy_buf_create(pool, data_len);
56
memcpy(nb->last, b->pos, data_len);
58
nb->cleanup = (easy_buf_cleanup_pt*)easy_buf_free_private_pool;
60
if ((priv_pool_created & 0x1ff) == 0) {
61
easy_info_log("easy created (%ld) private pools, and destoyed (%ld) pools.\n",
62
priv_pool_created, priv_pool_destroyed);
68
easy_pool_destroy(pool);
76
easy_buf_t *easy_buf_pack(easy_pool_t *pool, const void *data, uint32_t size)
80
if ((b = (easy_buf_t *)easy_pool_calloc(pool, sizeof(easy_buf_t))) == NULL)
83
easy_buf_set_data(pool, b, data, size);
91
void easy_buf_set_data(easy_pool_t *pool, easy_buf_t *b, const void *data, uint32_t size)
93
b->data = (char *)data;
95
b->last = b->pos + size;
100
easy_list_init(&b->node);
106
easy_file_buf_t *easy_file_buf_create(easy_pool_t *pool)
110
b = (easy_file_buf_t *)easy_pool_calloc(pool, sizeof(easy_file_buf_t));
111
b->flags = EASY_BUF_FILE;
114
easy_list_init(&b->node);
119
void easy_file_buf_set_close(easy_file_buf_t *b)
121
if ((b->flags & EASY_BUF_FILE))
122
b->flags = EASY_BUF_CLOSE_FILE;
125
void easy_buf_set_cleanup(easy_buf_t *b, easy_buf_cleanup_pt *cleanup, void *args)
127
b->cleanup = cleanup;
131
void easy_buf_destroy(easy_buf_t *b)
134
easy_buf_cleanup_pt *cleanup;
135
ev_tstamp easy_hold_time;
142
easy_list_del(&b->node);
143
if ((b->flags & EASY_BUF_CLOSE_FILE) == EASY_BUF_CLOSE_FILE) {
144
close(((easy_file_buf_t *)b)->fd);
151
if ((cleanup = b->cleanup)) {
153
(*cleanup)(b, b->args);
160
if ((s->type == EASY_TYPE_SESSION) ||
161
(s->type == EASY_TYPE_KEEPALIVE_SESSION) ||
162
(s->type == EASY_TYPE_RL_SESSION)) {
165
if (unlikely(s->enable_trace)) {
166
easy_debug_log("destroy buffer, session=%p, count=%ld, on_write_success=%p",
167
s, s->buf_count, s->on_write_success);
169
if (s->buf_count == 0) {
170
easy_hold_time = ev_time() - s->now;
171
if ((easy_hold_time > 1.0) && (EASY_REACH_TIME_INTERVAL(1 * 1000 * 1000))) {
172
easy_info_log("Session hold by easy for too much time, session(%p), time(%fs), "
173
"packet_id(%" PRId64 "), conn(%p).", s, easy_hold_time, s->packet_id, s->c);
177
if (s->on_write_success) {
178
s->on_write_success(s);
187
if ((s != NULL) && (s->tx_buf_separated > 0) && (s->tx_buf != NULL)) {
188
easy_pool_realloc(s->tx_buf, 0);
197
int easy_buf_check_read_space(easy_pool_t *pool, easy_buf_t *b, uint32_t size)
202
if ((b->end - b->last) >= (int)size)
206
dsize = (b->last - b->pos);
207
size = easy_max(dsize * 3 / 2, size + dsize);
208
size = easy_align(size, EASY_POOL_PAGE_SIZE);
211
if ((ptr = (char *)easy_pool_alloc(pool, size)) == NULL)
216
memcpy(ptr, b->pos, dsize);
220
b->last = b->pos + dsize;
221
b->end = b->pos + size;
229
easy_buf_t *easy_buf_check_write_space(easy_pool_t *pool, easy_list_t *bc, uint32_t size)
231
easy_buf_t *b = easy_list_get_last(bc, easy_buf_t, node);
233
if (b != NULL && (b->end - b->last) >= (int)size)
237
size = easy_align(size, EASY_POOL_PAGE_SIZE);
239
if ((b = easy_buf_create(pool, size)) == NULL)
242
easy_list_add_tail(&b->node, bc);
250
void easy_buf_chain_clear(easy_list_t *l)
254
easy_list_for_each_entry_safe(b, b1, l, node) {
255
easy_debug_log("easy_buf_chain_clear, b(%), b1(p).\n", b, b1);
264
void easy_buf_chain_offer(easy_list_t *l, easy_buf_t *b)
266
if (!l->next) easy_list_init(l);
268
easy_list_add_tail(&b->node, l);
274
int easy_buf_string_copy(easy_pool_t *pool, easy_buf_string_t *d, const easy_buf_string_t *s)
277
d->data = (char *)easy_pool_alloc(pool, s->len + 1);
278
memcpy(d->data, s->data, s->len);
279
d->data[s->len] = '\0';
286
int easy_buf_string_printf(easy_pool_t *pool, easy_buf_string_t *d, const char *fmt, ...)
293
len = easy_vsnprintf(buffer, 2048, fmt, args);
295
d->data = (char *)easy_pool_alloc(pool, len + 1);
296
memcpy(d->data, buffer, len);
302
int easy_buf_list_len(easy_list_t *l)
307
easy_list_for_each_entry(b, l, node) {
308
len += easy_buf_len(b);