buffer.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // "NULL" key, a value that is never user-provided. Used to mark special
  11. // values (e.g. deleted records).
  12. #define NULL_KEY 0
  13. /** @brief General-purpose data buffer.
  14. *
  15. * The structure is transparently exposed so that the related API only defines
  16. * few basic helper methods. Other operations, such as appending, may be
  17. * performed by simply using the addr and size attributes.
  18. *
  19. * A buffer can be initialized once and reused multiple times, e.g. in a loop,
  20. * without being freed between iterations, by using #LSUP_buffer_init.
  21. */
  22. typedef struct LSUP_Buffer {
  23. /*@null@*/ void *addr;
  24. size_t size;
  25. } LSUP_Buffer;
  26. /** @brief Triple of byte buffers.
  27. *
  28. * This is a generic data triple. Store implementations should handle this
  29. * data type rather than RDF terms and triples. Conversion to/from RDF terms
  30. * and triples is done in the term and triple modules.
  31. */
  32. typedef struct buffer_triple_t {
  33. LSUP_Buffer *s;
  34. LSUP_Buffer *p;
  35. LSUP_Buffer *o;
  36. } LSUP_BufferTriple;
  37. /** Initialize or reuse a buffer handle.
  38. *
  39. * The handle must have been created with #LSUP_buffer_new*().
  40. *
  41. * The data block is resized without being freed first. The handle must be
  42. * eventually freed with #LSUP_buffer_done() after use.
  43. *
  44. * @param buf[in] A buffer handle obtained with #LSUP_buffer_new or by manual
  45. * allocation.
  46. *
  47. * @param size[in] New size.
  48. *
  49. * @param data[in] If not NULL, data to replace the existing ones. The size
  50. * of the data to be copied is determined by the size parameter. If NULL, the
  51. * existing data are preserved as with a normal realloc().
  52. */
  53. LSUP_rc
  54. LSUP_buffer_init (LSUP_Buffer *buf, const size_t size, const void *data);
  55. /** @brief Create a new buffer and optionally populate it with data.
  56. *
  57. * To change the buffer size and/or data later call #LSUP_buffer_init.
  58. *
  59. * To copy a buffer just do buf2 = LSUP_buffer_new (buf1->size, buf1->addr);
  60. *
  61. * @param size[in] Length of the data.
  62. *
  63. * @param data[in] Optional data to initially populate the object with. If
  64. * NULL, the buffer data are garbage.
  65. *
  66. * @return LSUP_Buffer pointer. It must be freed with #LSUP_buffer_free. NULL
  67. * on error.
  68. */
  69. inline LSUP_Buffer *
  70. LSUP_buffer_new (const size_t size, const void *data)
  71. {
  72. LSUP_Buffer *buf;
  73. CALLOC_GUARD (buf, NULL);
  74. if (LSUP_buffer_init (buf, size, data) != LSUP_OK) {
  75. free (buf->addr);
  76. free (buf);
  77. return NULL;
  78. }
  79. return buf;
  80. }
  81. /** @brief Dummy buffer to be used with #LSUP_buffer_init.
  82. */
  83. #define BUF_DUMMY LSUP_buffer_new (0, NULL)
  84. /** @brief Free the content of a buffer.
  85. */
  86. void LSUP_buffer_done (LSUP_Buffer *buf);
  87. /** @brief Free a buffer.
  88. */
  89. void LSUP_buffer_free (LSUP_Buffer *buf);
  90. /** @brief Hash a buffer.
  91. */
  92. inline LSUP_Key
  93. LSUP_buffer_hash (const LSUP_Buffer *buf)
  94. { return (buf == NULL) ? NULL_KEY : XXH64 (buf->addr, buf->size, HASH_SEED); }
  95. /** @brief Print a byte string of a given length in a human-readable format.
  96. *
  97. * The string is printed in Python style: printable characters are output
  98. * literally, and non-printable ones as hex sequences.
  99. */
  100. void LSUP_buffer_print (const LSUP_Buffer *buf);
  101. /** @brief Format a buffer into anb ASCII string.
  102. *
  103. * The string has non-printable characters escaped as "\xNN".
  104. *
  105. * @param buf[in] Buffer to convert.
  106. *
  107. * @return Formatted string. It must be freed with free().
  108. */
  109. char *
  110. LSUP_buffer_as_str (const LSUP_Buffer *buf);
  111. /** @brief Compare two buffers.
  112. *
  113. * The return value is the same as memcmp.
  114. */
  115. inline int LSUP_buffer_cmp (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  116. {
  117. return memcmp (
  118. buf1->addr, buf2->addr,
  119. (buf1->size > buf2->size ? buf1->size : buf2->size));
  120. }
  121. /** @brief Return whether two buffers are equal.
  122. *
  123. * This may be faster than #LSUP_buffer_cmp() because it does a size comparison
  124. * first.
  125. */
  126. inline bool LSUP_buffer_eq (const LSUP_Buffer *buf1, const LSUP_Buffer *buf2)
  127. {
  128. if (buf1->size != buf2->size) return false;
  129. return (LSUP_buffer_cmp (buf1, buf2) == 0) ? true : false;
  130. }
  131. /*
  132. * Buffer triples.
  133. */
  134. LSUP_BufferTriple *
  135. LSUP_btriple_new(LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  136. /** @brief Initialize internal term pointers in a heap-allocated buffer triple.
  137. *
  138. * The triple must be freed with #LSUP_btriple_free().
  139. *
  140. * @param sspo[in] Serialized triple pointer to initialize.
  141. */
  142. LSUP_rc
  143. LSUP_btriple_init (
  144. LSUP_BufferTriple *sspo, LSUP_Buffer *s, LSUP_Buffer *p, LSUP_Buffer *o);
  145. /** @brief Free the internal pointers of a buffer triple.
  146. *
  147. * @param sspo[in] Buffer triple to be freed.
  148. */
  149. void
  150. LSUP_btriple_done (LSUP_BufferTriple *sspo);
  151. /** @brief Free a buffer triple and all its internal pointers.
  152. *
  153. * NOTE: If the buffer pointers are not to be freed (e.g. they are owned by a
  154. * back end), use a simple free(sspo) instead of this.
  155. *
  156. * @param sspo[in] Buffer triple to be freed.
  157. */
  158. void
  159. LSUP_btriple_free (LSUP_BufferTriple *sspo);
  160. /** @brief Free a buffer triple and its buffer handles but not the buffer data.
  161. *
  162. * This is useful when freeing a "dummy" triple whose buffers are owned by the
  163. * caller but the data the terms point to are owned by the store.
  164. *
  165. */
  166. void
  167. LSUP_btriple_free_shallow (LSUP_BufferTriple *sspo);
  168. /** @brief Hash a buffer triple.
  169. *
  170. * TODO This doesn't handle blank nodes correctly.
  171. */
  172. inline LSUP_Key
  173. LSUP_btriple_hash (const LSUP_BufferTriple *strp)
  174. {
  175. return XXH64 (
  176. strp->s->addr, strp->s->size,
  177. XXH64 (
  178. strp->p->addr, strp->p->size,
  179. XXH64 (strp->o->addr, strp->o->size, HASH_SEED)
  180. )
  181. );
  182. }
  183. #define STRP_DUMMY LSUP_btriple_new (NULL, NULL, NULL)
  184. #endif