25 #define BUFFERS_PRIVATE
49 #define check() STMT_BEGIN buf_assert_ok(buf); STMT_END
51 #define check() STMT_NIL
75 #define CHUNK_HEADER_LEN offsetof(chunk_t, mem[0])
78 #ifdef DISABLE_MEMORY_SENTINELS
79 #define SENTINEL_LEN 0
81 #define SENTINEL_LEN 4
85 #define CHUNK_OVERHEAD (CHUNK_HEADER_LEN + SENTINEL_LEN)
89 #define CHUNK_ALLOC_SIZE(memlen) (CHUNK_OVERHEAD + (memlen))
92 #define CHUNK_SIZE_WITH_ALLOC(memlen) ((memlen) - CHUNK_OVERHEAD)
94 #define DEBUG_SENTINEL
96 #if defined(DEBUG_SENTINEL) && !defined(DISABLE_MEMORY_SENTINELS)
99 #define DBG_S(s) (void)0
103 #ifdef DISABLE_MEMORY_SENTINELS
104 #define CHUNK_SET_SENTINEL(chunk, alloclen) STMT_NIL
106 #define CHUNK_SET_SENTINEL(chunk, alloclen) do { \
107 uint8_t *a = (uint8_t*) &(chunk)->mem[(chunk)->memlen]; \
108 DBG_S(uint8_t *b = &((uint8_t*)(chunk))[(alloclen)-SENTINEL_LEN]); \
109 DBG_S(tor_assert(a == b)); \
110 memset(a,0,SENTINEL_LEN); \
120 if (chunk->datalen && chunk->data != &chunk->mem[0]) {
121 memmove(chunk->mem, chunk->data, chunk->datalen);
123 chunk->data = &chunk->mem[0];
129 buf_chunk_free_unchecked(chunk_t *chunk)
133 #ifdef DEBUG_CHUNK_ALLOC
141 static inline chunk_t *
142 chunk_new_with_alloc_size(
size_t alloc)
145 ch = tor_malloc(alloc);
148 #ifdef DEBUG_CHUNK_ALLOC
149 ch->DBG_alloc = alloc;
153 ch->data = &ch->mem[0];
154 CHUNK_SET_SENTINEL(ch, alloc);
160 static inline chunk_t *
164 const size_t memlen_orig = chunk->memlen;
168 offset = chunk->data - chunk->mem;
169 chunk = tor_realloc(chunk, new_alloc);
171 chunk->data = chunk->mem + offset;
172 #ifdef DEBUG_CHUNK_ALLOC
174 chunk->DBG_alloc = new_alloc;
177 CHUNK_SET_SENTINEL(chunk, new_alloc);
182 #define MIN_CHUNK_ALLOC 256
184 #define MAX_CHUNK_ALLOC 65536
211 buf_pullup(buf_t *buf,
size_t bytes,
const char **head_out,
size_t *len_out)
222 if (buf->datalen < bytes)
223 bytes = buf->datalen;
226 if (buf->head->datalen >= bytes) {
227 *head_out = buf->head->data;
228 *len_out = buf->head->datalen;
232 if (buf->head->memlen >= capacity) {
234 size_t needed = capacity - buf->head->datalen;
235 if (CHUNK_REMAINING_CAPACITY(buf->head) < needed)
237 tor_assert(CHUNK_REMAINING_CAPACITY(buf->head) >= needed);
246 if (newhead != buf->head) {
247 if (buf->tail == buf->head)
254 while (dest->datalen < bytes) {
255 size_t n = bytes - dest->datalen;
258 if (n >= src->datalen) {
259 memcpy(CHUNK_WRITE_PTR(dest), src->data, src->datalen);
260 dest->datalen += src->datalen;
261 dest->next = src->next;
262 if (buf->tail == src)
264 buf_chunk_free_unchecked(src);
266 memcpy(CHUNK_WRITE_PTR(dest), src->data, n);
275 *head_out = buf->head->data;
276 *len_out = buf->head->datalen;
279 #ifdef TOR_UNIT_TESTS
285 buf_new_with_data(
const char *cp,
size_t sz)
302 buf->tail = buf->head;
309 tor_assert(sz <= CHUNK_REMAINING_CAPACITY(buf->head));
310 memcpy(&buf->head->mem[0], cp, sz);
312 buf->head->datalen = sz;
313 buf->head->data = &buf->head->mem[0];
335 if (buf->head->datalen > n) {
336 buf->head->datalen -= n;
337 buf->head->data += n;
341 chunk_t *victim = buf->head;
342 n -= victim->datalen;
343 buf->datalen -= victim->datalen;
344 buf->head = victim->next;
345 if (buf->tail == victim)
347 buf_chunk_free_unchecked(victim);
367 buf_t *buf = tor_malloc_zero(
sizeof(buf_t));
368 buf->magic = BUFFER_MAGIC;
369 buf->default_chunk_size = 4096;
374 buf_get_default_chunk_size(
const buf_t *buf)
376 return buf->default_chunk_size;
383 chunk_t *chunk, *next;
385 for (chunk = buf->head; chunk; chunk = next) {
387 buf_chunk_free_unchecked(chunk);
389 buf->head = buf->tail = NULL;
404 const chunk_t *chunk;
405 for (chunk = buf->head; chunk; chunk = chunk->next) {
419 return CHUNK_REMAINING_CAPACITY(buf->tail);
430 buf->magic = 0xdeadbeef;
440 #ifdef DEBUG_CHUNK_ALLOC
444 if (in_chunk->data) {
445 ptrdiff_t offset = in_chunk->data - in_chunk->mem;
446 newch->data = newch->mem + offset;
457 out->default_chunk_size = buf->default_chunk_size;
458 for (ch = buf->head; ch; ch = ch->next) {
461 out->tail->next = newch;
464 out->head = out->tail = newch;
467 out->datalen = buf->datalen;
480 chunk = chunk_new_with_alloc_size(buf->default_chunk_size);
491 buf->tail->next = chunk;
495 buf->head = buf->tail = chunk;
509 return now - buf->head->inserted_time;
516 buf_get_total_allocation(
void)
527 buf_add(buf_t *buf,
const char *
string,
size_t string_len)
530 return (
int)buf->datalen;
540 if (!buf->tail || !CHUNK_REMAINING_CAPACITY(buf->tail))
543 copy = CHUNK_REMAINING_CAPACITY(buf->tail);
544 if (copy > string_len)
546 memcpy(CHUNK_WRITE_PTR(buf->tail),
string, copy);
549 buf->datalen += copy;
550 buf->tail->datalen += copy;
555 return (
int)buf->datalen;
563 buf_add(buf,
string, strlen(
string));
584 buf_add(buf, tmp, strlen(tmp));
598 result = tor_malloc(sz+1);
610 buf_peek(
const buf_t *buf,
char *
string,
size_t string_len)
621 size_t copy = string_len;
623 if (chunk->datalen < copy)
624 copy = chunk->datalen;
625 memcpy(
string, chunk->data, copy);
649 return (
int)buf->datalen;
665 if (BUG(buf_out->datalen >
BUF_MAX_LEN - *buf_flushlen))
669 if (len > buf_in->datalen)
670 len = buf_in->datalen;
678 size_t n = len >
sizeof(b) ?
sizeof(b) : len;
700 if (BUG(buf_out->datalen >
BUF_MAX_LEN - buf_in->datalen))
703 size_t n_bytes_moved = buf_in->datalen;
705 if (buf_out->head == NULL) {
706 buf_out->head = buf_in->head;
707 buf_out->tail = buf_in->tail;
709 buf_out->tail->next = buf_in->head;
710 buf_out->tail = buf_in->tail;
713 buf_out->datalen += buf_in->datalen;
714 buf_in->head = buf_in->tail = NULL;
717 return n_bytes_moved;
732 out->
chunk = buf->head;
743 const chunk_t *chunk;
747 if (out->
chunk->datalen) {
754 for (chunk = out->
chunk; chunk; chunk = chunk->next) {
755 char *cp = memchr(chunk->data+pos, ch, chunk->datalen - pos);
759 out->
pos = (int)(cp - chunk->data);
776 if (pos->
pos == (ptrdiff_t)pos->
chunk->datalen) {
777 if (!pos->
chunk->next)
795 memcpy(&p, pos,
sizeof(p));
836 char tmp[PEEK_BUF_STARTSWITH_MAX];
837 size_t clen = strlen(cmd);
840 if (BUG(clen >
sizeof(tmp)))
842 if (buf->datalen < clen)
854 ptrdiff_t offset = 0;
856 for (chunk = buf->head; chunk; chunk = chunk->next) {
857 char *cp = memchr(chunk->data, ch, chunk->datalen);
859 return offset + (cp - chunk->data);
861 offset += chunk->datalen;
885 sz = (size_t) offset;
886 if (sz+2 > *data_len) {
891 data_out[sz+1] =
'\0';
922 for (ch = buf->head; ch; ch = ch->next) {
923 total += ch->datalen;
927 tor_assert(ch->data <= &ch->mem[0]+ch->memlen);
928 if (ch->data == &ch->mem[0]+ch->memlen) {
930 static int warned = 0;
932 log_warn(
LD_BUG,
"Invariant violation in buf.c related to #15083");
937 tor_assert(ch->data+ch->datalen <= &ch->mem[0] + ch->memlen);
static chunk_t * chunk_grow(chunk_t *chunk, size_t sz)
void buf_add_printf(buf_t *buf, const char *format,...)
static size_t total_bytes_allocated_in_chunks
size_t buf_move_all(buf_t *buf_out, buf_t *buf_in)
void buf_clear(buf_t *buf)
chunk_t * buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped)
buf_t * buf_new_with_capacity(size_t size)
static void chunk_repack(chunk_t *chunk)
int buf_add(buf_t *buf, const char *string, size_t string_len)
void buf_drain(buf_t *buf, size_t n)
size_t buf_allocation(const buf_t *buf)
#define CHUNK_SIZE_WITH_ALLOC(memlen)
static int buf_pos_inc(buf_pos_t *pos)
int buf_find_string_offset(const buf_t *buf, const char *s, size_t n)
size_t buf_datalen(const buf_t *buf)
buf_t * buf_copy(const buf_t *buf)
uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now)
int buf_peek_startswith(const buf_t *buf, const char *cmd)
static chunk_t * chunk_copy(const chunk_t *in_chunk)
static ptrdiff_t buf_find_pos_of_char(char ch, buf_pos_t *out)
void buf_assert_ok(buf_t *buf)
#define CHUNK_ALLOC_SIZE(memlen)
int buf_get_line(buf_t *buf, char *data_out, size_t *data_len)
size_t buf_slack(const buf_t *buf)
void buf_add_vprintf(buf_t *buf, const char *format, va_list args)
static ptrdiff_t buf_find_offset_of_char(buf_t *buf, char ch)
void buf_peek(const buf_t *buf, char *string, size_t string_len)
static void buf_pos_init(const buf_t *buf, buf_pos_t *out)
int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
void buf_free_(buf_t *buf)
void buf_add_string(buf_t *buf, const char *string)
size_t buf_preferred_chunk_size(size_t target)
int buf_set_to_copy(buf_t **output, const buf_t *input)
int buf_get_bytes(buf_t *buf, char *string, size_t string_len)
static int buf_matches_at_pos(const buf_pos_t *pos, const char *s, size_t n)
char * buf_extract(buf_t *buf, size_t *sz_out)
void buf_pullup(buf_t *buf, size_t bytes, const char **head_out, size_t *len_out)
Header file for buffers.c.
uint32_t monotime_coarse_get_stamp(void)
Functions and types for monotonic times.
#define fast_memeq(a, b, c)
Headers for util_malloc.c.
int tor_vasprintf(char **strp, const char *fmt, va_list args)
#define MOCK_IMPL(rv, funcname, arglist)
Integer definitions used throughout Tor.
Macros to manage assertions, fatal and non-fatal.