buffer.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef _LSUP_BUFFER_H
  2. #define _LSUP_BUFFER_H
  3. #include "core.h"
  4. typedef struct LSUP_Buffer {
  5. void *addr;
  6. size_t size;
  7. } LSUP_Buffer;
  8. LSUP_rc
  9. LSUP_buffer_new(size_t size, LSUP_Buffer *buf_p);
  10. LSUP_rc
  11. LSUP_buffer_reset(LSUP_Buffer *buf, size_t size);
  12. void LSUP_buffer_print(const LSUP_Buffer *buf);
  13. LSUP_rc
  14. LSUP_buffer_copy(const LSUP_Buffer *src, LSUP_Buffer *dest);
  15. void LSUP_buffer_free(LSUP_Buffer *buf);
  16. /** @brief Compare two buffers.
  17. *
  18. * The return value is the same as memcmp.
  19. */
  20. inline int LSUP_buffer_cmp(const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  21. { return memcmp(buf1->addr, buf2->addr, max(buf1->size, buf2->size)); }
  22. /** @brief Return whether two buffers are equal.
  23. *
  24. * This may be faster than #LSUP_buffer_cmp because it returns immediately if
  25. * the sizes of the buffers differ.
  26. */
  27. inline bool LSUP_buffer_eq(const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  28. {
  29. if (buf1->size != buf2->size) return false;
  30. return (LSUP_buffer_cmp(buf1, buf2) == 0) ? true : false;
  31. }
  32. #endif