buffer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef _LSUP_BUFFER_H
  2. #define _LSUP_BUFFER_H
  3. #include "core.h"
  4. /** @brief General-purpose data buffer.
  5. *
  6. * The structure is transparently exposed so that the related API only defines
  7. * few basic helper methods. Other operations, such as appending, may be
  8. * performed by simply using the addr and size attributes.
  9. *
  10. * A buffer can be initialized once and reused multiple times, e.g. in a loop,
  11. * without being freed between iterations, by using #LSUP_buffer_reset.
  12. */
  13. typedef struct LSUP_Buffer {
  14. /*@null@*/ void *addr;
  15. size_t size;
  16. } LSUP_Buffer;
  17. /** Reuse an initialized buffer handle.
  18. *
  19. * The data block is resized without being freed (with realloc). If the new
  20. * size is not smaller than the old one and the data parameter is NULL, the
  21. * existing data are preserved. If the size is increased, the new memory is not
  22. * initialized.
  23. *
  24. * The handle must be freed with #LSUP_buffer_done after use.
  25. *
  26. * @param buf[in] A buffer handle obtained with #LSUP_buffer_new or by manual
  27. * allocation.
  28. *
  29. * @param size[in] New size.
  30. *
  31. * @param data[in] If not NULL, data to replace the existing ones. The size
  32. * of the data to be copied is determined by the size parameter.
  33. */
  34. LSUP_rc
  35. LSUP_buffer_reset (LSUP_Buffer *buf, const size_t size, const void *data);
  36. /** Initialize an allocated buffer handle.
  37. *
  38. * The handle must be freed with #LSUP_buffer_done after use.
  39. *
  40. * @param buf[in] A manually (stack or heap) allocated buffer handle.
  41. *
  42. * @param size[in] New size.
  43. *
  44. * @param data[in] If not NULL, data to replace the existing ones. The size
  45. * of the data to be copied is determined by the size parameter.
  46. */
  47. inline LSUP_rc
  48. LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data)
  49. {
  50. buf->addr = NULL;
  51. return LSUP_buffer_reset(buf, size, data);
  52. }
  53. /** @brief Create a new buffer and optionally populate it with data.
  54. *
  55. * To change the buffer size later call #LSUP_buffer_reset.
  56. *
  57. * To copy a buffer just do buf2 = LSUP_buffer_new (buf1->size, buf1->addr);
  58. *
  59. * @param size[in] Length of the data.
  60. *
  61. * @param data[in] Optional data to initially populate the object with.
  62. *
  63. * @return LSUP_Buffer pointer. It must be freed with #LSUP_buffer_free. NULL
  64. * on error.
  65. */
  66. inline LSUP_Buffer *
  67. LSUP_buffer_new (const size_t size, const void *data)
  68. {
  69. LSUP_Buffer *buf;
  70. CRITICAL (buf = malloc (sizeof (*buf)));
  71. if (LSUP_buffer_init (buf, size, data) != LSUP_OK) {
  72. free (buf);
  73. return NULL;
  74. }
  75. return buf;
  76. }
  77. /** @brief Dummy buffer to be used with #LSUP_buffer_reset.
  78. */
  79. #define BUF_DUMMY LSUP_buffer_new(0, NULL);
  80. /** @brief Free the content of a buffer.
  81. */
  82. void LSUP_buffer_done(LSUP_Buffer *buf);
  83. /** @brief Free a buffer.
  84. */
  85. void LSUP_buffer_free (LSUP_Buffer *buf);
  86. /** @brief Print a byte string of a given length in a human-readable format.
  87. *
  88. * The string is printed in Python style: printable characters are output
  89. * literally, and non-printable ones as hex sequences.
  90. */
  91. void LSUP_buffer_print (const LSUP_Buffer *buf);
  92. /** @brief Compare two buffers.
  93. *
  94. * The return value is the same as memcmp.
  95. */
  96. inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  97. { return memcmp (buf1->addr, buf2->addr, max (buf1->size, buf2->size)); }
  98. /** @brief Return whether two buffers are equal.
  99. *
  100. * This may be faster than #LSUP_buffer_cmp because it returns immediately if
  101. * the sizes of the buffers differ.
  102. */
  103. inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  104. {
  105. if (buf1->size != buf2->size) return false;
  106. return (LSUP_buffer_cmp (buf1, buf2) == 0) ? true : false;
  107. }
  108. #endif