buffers.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file buffers.h
  8. *
  9. * \brief Header file for buffers.c.
  10. **/
  11. #ifndef TOR_BUFFERS_H
  12. #define TOR_BUFFERS_H
  13. #include "lib/cc/compat_compiler.h"
  14. #include "lib/cc/torint.h"
  15. #include "lib/testsupport/testsupport.h"
  16. #include <stdarg.h>
  17. typedef struct buf_t buf_t;
  18. buf_t *buf_new(void);
  19. buf_t *buf_new_with_capacity(size_t size);
  20. size_t buf_get_default_chunk_size(const buf_t *buf);
  21. void buf_free_(buf_t *buf);
  22. #define buf_free(b) FREE_AND_NULL(buf_t, buf_free_, (b))
  23. void buf_clear(buf_t *buf);
  24. buf_t *buf_copy(const buf_t *buf);
  25. MOCK_DECL(size_t, buf_datalen, (const buf_t *buf));
  26. size_t buf_allocation(const buf_t *buf);
  27. size_t buf_slack(const buf_t *buf);
  28. uint32_t buf_get_oldest_chunk_timestamp(const buf_t *buf, uint32_t now);
  29. size_t buf_get_total_allocation(void);
  30. int buf_add(buf_t *buf, const char *string, size_t string_len);
  31. void buf_add_string(buf_t *buf, const char *string);
  32. void buf_add_printf(buf_t *buf, const char *format, ...)
  33. CHECK_PRINTF(2, 3);
  34. void buf_add_vprintf(buf_t *buf, const char *format, va_list args)
  35. CHECK_PRINTF(2, 0);
  36. int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
  37. void buf_move_all(buf_t *buf_out, buf_t *buf_in);
  38. void buf_peek(const buf_t *buf, char *string, size_t string_len);
  39. void buf_drain(buf_t *buf, size_t n);
  40. int buf_get_bytes(buf_t *buf, char *string, size_t string_len);
  41. int buf_get_line(buf_t *buf, char *data_out, size_t *data_len);
  42. #define PEEK_BUF_STARTSWITH_MAX 16
  43. int buf_peek_startswith(const buf_t *buf, const char *cmd);
  44. int buf_set_to_copy(buf_t **output,
  45. const buf_t *input);
  46. void buf_assert_ok(buf_t *buf);
  47. int buf_find_string_offset(const buf_t *buf, const char *s, size_t n);
  48. void buf_pullup(buf_t *buf, size_t bytes,
  49. const char **head_out, size_t *len_out);
  50. char *buf_extract(buf_t *buf, size_t *sz_out);
  51. #ifdef BUFFERS_PRIVATE
  52. #ifdef TOR_UNIT_TESTS
  53. buf_t *buf_new_with_data(const char *cp, size_t sz);
  54. #endif
  55. size_t buf_preferred_chunk_size(size_t target);
  56. #define DEBUG_CHUNK_ALLOC
  57. /** A single chunk on a buffer. */
  58. typedef struct chunk_t {
  59. struct chunk_t *next; /**< The next chunk on the buffer. */
  60. size_t datalen; /**< The number of bytes stored in this chunk */
  61. size_t memlen; /**< The number of usable bytes of storage in <b>mem</b>. */
  62. #ifdef DEBUG_CHUNK_ALLOC
  63. size_t DBG_alloc;
  64. #endif
  65. char *data; /**< A pointer to the first byte of data stored in <b>mem</b>. */
  66. uint32_t inserted_time; /**< Timestamp when this chunk was inserted. */
  67. char mem[FLEXIBLE_ARRAY_MEMBER]; /**< The actual memory used for storage in
  68. * this chunk. */
  69. } chunk_t;
  70. /** Magic value for buf_t.magic, to catch pointer errors. */
  71. #define BUFFER_MAGIC 0xB0FFF312u
  72. /** A resizeable buffer, optimized for reading and writing. */
  73. struct buf_t {
  74. uint32_t magic; /**< Magic cookie for debugging: Must be set to
  75. * BUFFER_MAGIC. */
  76. size_t datalen; /**< How many bytes is this buffer holding right now? */
  77. size_t default_chunk_size; /**< Don't allocate any chunks smaller than
  78. * this for this buffer. */
  79. chunk_t *head; /**< First chunk in the list, or NULL for none. */
  80. chunk_t *tail; /**< Last chunk in the list, or NULL for none. */
  81. };
  82. chunk_t *buf_add_chunk_with_capacity(buf_t *buf, size_t capacity, int capped);
  83. /** If a read onto the end of a chunk would be smaller than this number, then
  84. * just start a new chunk. */
  85. #define MIN_READ_LEN 8
  86. /** Return the number of bytes that can be written onto <b>chunk</b> without
  87. * running out of space. */
  88. static inline size_t
  89. CHUNK_REMAINING_CAPACITY(const chunk_t *chunk)
  90. {
  91. return (chunk->mem + chunk->memlen) - (chunk->data + chunk->datalen);
  92. }
  93. /** Return the next character in <b>chunk</b> onto which data can be appended.
  94. * If the chunk is full, this might be off the end of chunk->mem. */
  95. static inline char *
  96. CHUNK_WRITE_PTR(chunk_t *chunk)
  97. {
  98. return chunk->data + chunk->datalen;
  99. }
  100. #endif /* defined(BUFFERS_PRIVATE) */
  101. #endif /* !defined(TOR_BUFFERS_H) */