buffer.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef _LSUP_BUFFER_H
  2. #define _LSUP_BUFFER_H
  3. #include "xxhash.h"
  4. #include "core.h"
  5. #ifndef HASH_SEED
  6. /** @brief Seed used for all hashing. Compile-time configurable.
  7. */
  8. #define HASH_SEED 0
  9. #endif
  10. /** @brief General-purpose data buffer.
  11. *
  12. * The structure is transparently exposed so that the related API only defines
  13. * few basic helper methods. Other operations, such as appending, may be
  14. * performed by simply using the addr and size attributes.
  15. *
  16. * A buffer can be initialized once and reused multiple times, e.g. in a loop,
  17. * without being freed between iterations, by using #LSUP_buffer_init.
  18. */
  19. typedef struct LSUP_Buffer {
  20. /*@null@*/ void *addr;
  21. size_t size;
  22. } LSUP_Buffer;
  23. /** Initialize or reuse a buffer handle.
  24. *
  25. * The handle must have been created with #LSUP_buffer_new*().
  26. *
  27. * The data block is resized without being freed first. The handle must be
  28. * eventually freed with #LSUP_buffer_done() after use.
  29. *
  30. * @param buf[in] A buffer handle obtained with #LSUP_buffer_new or by manual
  31. * allocation.
  32. *
  33. * @param size[in] New size.
  34. *
  35. * @param data[in] If not NULL, data to replace the existing ones. The size
  36. * of the data to be copied is determined by the size parameter. If NULL, the
  37. * existing data are preserved as with a normal realloc().
  38. */
  39. LSUP_rc
  40. LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data);
  41. /** @brief Create a new buffer and optionally populate it with data.
  42. *
  43. * To change the buffer size and/or data later call #LSUP_buffer_init.
  44. *
  45. * To copy a buffer just do buf2 = LSUP_buffer_new (buf1->size, buf1->addr);
  46. *
  47. * @param size[in] Length of the data.
  48. *
  49. * @param data[in] Optional data to initially populate the object with. If
  50. * NULL, the buffer data are garbage.
  51. *
  52. * @return LSUP_Buffer pointer. It must be freed with #LSUP_buffer_free. NULL
  53. * on error.
  54. */
  55. inline LSUP_Buffer *
  56. LSUP_buffer_new (const size_t size, const void *data)
  57. {
  58. LSUP_Buffer *buf = calloc (1, sizeof (*buf));
  59. if (!buf) return NULL;
  60. if (LSUP_buffer_init (buf, size, data) != LSUP_OK) {
  61. free (buf->addr);
  62. free (buf);
  63. return NULL;
  64. }
  65. return buf;
  66. }
  67. /** @brief Dummy buffer to be used with #LSUP_buffer_init.
  68. */
  69. #define BUF_DUMMY LSUP_buffer_new (0, NULL)
  70. /** @brief Free the content of a buffer.
  71. */
  72. void LSUP_buffer_done (LSUP_Buffer *buf);
  73. /** @brief Free a buffer.
  74. */
  75. void LSUP_buffer_free (LSUP_Buffer *buf);
  76. /** @brief Hash a buffer.
  77. */
  78. inline LSUP_Key
  79. LSUP_buffer_hash (const LSUP_Buffer *buf)
  80. { return XXH64(buf->addr, buf->size, HASH_SEED); }
  81. /** @brief Combine hash values.
  82. *
  83. * TODO Adapt to different sizes of LSUP_Key.
  84. */
  85. inline LSUP_Key LSUP_btriple_hash (
  86. const LSUP_Buffer *b1, const LSUP_Buffer *b2)
  87. { return XXH64 (b2->addr, b2->size, XXH64 (b1->addr, b1->size, HASH_SEED)); }
  88. /** @brief Print a byte string of a given length in a human-readable format.
  89. *
  90. * The string is printed in Python style: printable characters are output
  91. * literally, and non-printable ones as hex sequences.
  92. */
  93. void LSUP_buffer_print (const LSUP_Buffer *buf);
  94. /** @brief Format a buffer into anb ASCII string.
  95. *
  96. * The string has non-printable characters escaped as "\xNN".
  97. *
  98. * @param buf[in] Buffer to convert.
  99. *
  100. * @return Formatted string. It must be freed with free().
  101. */
  102. char *
  103. LSUP_buffer_as_str (const LSUP_Buffer *buf);
  104. /** @brief Compare two buffers.
  105. *
  106. * The return value is the same as memcmp.
  107. */
  108. inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  109. { return memcmp (buf1->addr, buf2->addr, max (buf1->size, buf2->size)); }
  110. /** @brief Return whether two buffers are equal.
  111. *
  112. * This may be faster than #LSUP_buffer_cmp() because it does a size comparison
  113. * first.
  114. */
  115. inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  116. {
  117. if (buf1->size != buf2->size) return false;
  118. return (LSUP_buffer_cmp (buf1, buf2) == 0) ? true : false;
  119. }
  120. #endif