buffer.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_init.
  12. */
  13. typedef struct LSUP_Buffer {
  14. /*@null@*/ void *addr;
  15. size_t size;
  16. } LSUP_Buffer;
  17. /** Initialize or reuse a buffer handle.
  18. *
  19. * The handle must have been created with #LSUP_buffer_new*().
  20. *
  21. * The data block is resized without being freed first. The handle must be
  22. * eventually freed with #LSUP_buffer_done() after use.
  23. *
  24. * @param buf[in] A buffer handle obtained with #LSUP_buffer_new or by manual
  25. * allocation.
  26. *
  27. * @param size[in] New size.
  28. *
  29. * @param data[in] If not NULL, data to replace the existing ones. The size
  30. * of the data to be copied is determined by the size parameter. If NULL, the
  31. * existing data are preserved as with a normal realloc().
  32. */
  33. LSUP_rc
  34. LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data);
  35. /** @brief Create a new buffer and optionally populate it with data.
  36. *
  37. * To change the buffer size and/or data later call #LSUP_buffer_init.
  38. *
  39. * To copy a buffer just do buf2 = LSUP_buffer_new (buf1->size, buf1->addr);
  40. *
  41. * @param size[in] Length of the data.
  42. *
  43. * @param data[in] Optional data to initially populate the object with.
  44. *
  45. * @return LSUP_Buffer pointer. It must be freed with #LSUP_buffer_free. NULL
  46. * on error.
  47. */
  48. inline LSUP_Buffer *
  49. LSUP_buffer_new (const size_t size, const void *data)
  50. {
  51. LSUP_Buffer *buf = calloc (1, sizeof (*buf));
  52. if (!buf) return NULL;
  53. if (LSUP_buffer_init (buf, size, data) != LSUP_OK) {
  54. free (buf->addr);
  55. free (buf);
  56. return NULL;
  57. }
  58. return buf;
  59. }
  60. /** @brief Dummy buffer to be used with #LSUP_buffer_init.
  61. */
  62. #define BUF_DUMMY LSUP_buffer_new (0, NULL)
  63. /** @brief Free the content of a buffer.
  64. */
  65. void LSUP_buffer_done (LSUP_Buffer *buf);
  66. /** @brief Free a buffer.
  67. */
  68. void LSUP_buffer_free (LSUP_Buffer *buf);
  69. /** @brief Print a byte string of a given length in a human-readable format.
  70. *
  71. * The string is printed in Python style: printable characters are output
  72. * literally, and non-printable ones as hex sequences.
  73. */
  74. void LSUP_buffer_print (const LSUP_Buffer *buf);
  75. /** @brief Compare two buffers.
  76. *
  77. * The return value is the same as memcmp.
  78. */
  79. inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  80. { return memcmp (buf1->addr, buf2->addr, max (buf1->size, buf2->size)); }
  81. /** @brief Return whether two buffers are equal.
  82. *
  83. * This may be faster than #LSUP_buffer_cmp() because it does a size comparison
  84. * first.
  85. */
  86. inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  87. {
  88. if (buf1->size != buf2->size) return false;
  89. return (LSUP_buffer_cmp (buf1, buf2) == 0) ? true : false;
  90. }
  91. #endif