You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
10 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ifndef WBUF_H
  4. #define WBUF_H
  5. #ident "$Id$"
  6. /*
  7. COPYING CONDITIONS NOTICE:
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of version 2 of the GNU General Public License as
  10. published by the Free Software Foundation, and provided that the
  11. following conditions are met:
  12. * Redistributions of source code must retain this COPYING
  13. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  14. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  15. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  16. GRANT (below).
  17. * Redistributions in binary form must reproduce this COPYING
  18. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  19. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  20. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  21. GRANT (below) in the documentation and/or other materials
  22. provided with the distribution.
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  26. 02110-1301, USA.
  27. COPYRIGHT NOTICE:
  28. TokuDB, Tokutek Fractal Tree Indexing Library.
  29. Copyright (C) 2007-2013 Tokutek, Inc.
  30. DISCLAIMER:
  31. This program is distributed in the hope that it will be useful, but
  32. WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. General Public License for more details.
  35. UNIVERSITY PATENT NOTICE:
  36. The technology is licensed by the Massachusetts Institute of
  37. Technology, Rutgers State University of New Jersey, and the Research
  38. Foundation of State University of New York at Stony Brook under
  39. United States of America Serial No. 11/760379 and to the patents
  40. and/or patent applications resulting from it.
  41. PATENT MARKING NOTICE:
  42. This software is covered by US Patent No. 8,185,551.
  43. PATENT RIGHTS GRANT:
  44. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  45. Tokutek as part of the Fractal Tree project.
  46. "PATENT CLAIMS" means the claims of patents that are owned or
  47. licensable by Tokutek, both currently or in the future; and that in
  48. the absence of this license would be infringed by THIS
  49. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  50. "PATENT CHALLENGE" shall mean a challenge to the validity,
  51. patentability, enforceability and/or non-infringement of any of the
  52. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  53. Tokutek hereby grants to you, for the term and geographical scope of
  54. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  55. irrevocable (except as stated in this section) patent license to
  56. make, have made, use, offer to sell, sell, import, transfer, and
  57. otherwise run, modify, and propagate the contents of THIS
  58. IMPLEMENTATION, where such license applies only to the PATENT
  59. CLAIMS. This grant does not include claims that would be infringed
  60. only as a consequence of further modifications of THIS
  61. IMPLEMENTATION. If you or your agent or licensee institute or order
  62. or agree to the institution of patent litigation against any entity
  63. (including a cross-claim or counterclaim in a lawsuit) alleging that
  64. THIS IMPLEMENTATION constitutes direct or contributory patent
  65. infringement, or inducement of patent infringement, then any rights
  66. granted to you under this License shall terminate as of the date
  67. such litigation is filed. If you or your agent or exclusive
  68. licensee institute or order or agree to the institution of a PATENT
  69. CHALLENGE, then Tokutek may terminate any rights granted to you
  70. under this License.
  71. */
  72. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  73. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  74. #include <memory.h>
  75. #include <string.h>
  76. #include <portability/toku_htonl.h>
  77. #include "fttypes.h"
  78. #include "x1764.h"
  79. #define CRC_INCR
  80. /* When serializing a value, write it into a buffer. */
  81. /* This code requires that the buffer be big enough to hold whatever you put into it. */
  82. /* This abstraction doesn't do a good job of hiding its internals.
  83. * Why? The performance of this code is important, and we want to inline stuff */
  84. //Why is size here an int instead of DISKOFF like in the initializer?
  85. struct wbuf {
  86. unsigned char *buf;
  87. unsigned int size;
  88. unsigned int ndone;
  89. struct x1764 checksum; // The checksum state
  90. };
  91. static inline void wbuf_nocrc_init (struct wbuf *w, void *buf, DISKOFF size) {
  92. w->buf = (unsigned char *) buf;
  93. w->size = size;
  94. w->ndone = 0;
  95. }
  96. static inline void wbuf_init (struct wbuf *w, void *buf, DISKOFF size) {
  97. wbuf_nocrc_init(w, buf, size);
  98. x1764_init(&w->checksum);
  99. }
  100. static inline size_t wbuf_get_woffset(struct wbuf *w) {
  101. return w->ndone;
  102. }
  103. /* Write a character. */
  104. static inline void wbuf_nocrc_char (struct wbuf *w, unsigned char ch) {
  105. assert(w->ndone<w->size);
  106. w->buf[w->ndone++]=ch;
  107. }
  108. /* Write a character. */
  109. static inline void wbuf_nocrc_uint8_t (struct wbuf *w, uint8_t ch) {
  110. assert(w->ndone<w->size);
  111. w->buf[w->ndone++]=ch;
  112. }
  113. static inline void wbuf_char (struct wbuf *w, unsigned char ch) {
  114. wbuf_nocrc_char (w, ch);
  115. x1764_add(&w->checksum, &w->buf[w->ndone-1], 1);
  116. }
  117. //Write an int that MUST be in network order regardless of disk order
  118. static void wbuf_network_int (struct wbuf *w, int32_t i) __attribute__((__unused__));
  119. static void wbuf_network_int (struct wbuf *w, int32_t i) {
  120. assert(w->ndone + 4 <= w->size);
  121. *(uint32_t*)(&w->buf[w->ndone]) = toku_htonl(i);
  122. x1764_add(&w->checksum, &w->buf[w->ndone], 4);
  123. w->ndone += 4;
  124. }
  125. static inline void wbuf_nocrc_int (struct wbuf *w, int32_t i) {
  126. #if 0
  127. wbuf_nocrc_char(w, i>>24);
  128. wbuf_nocrc_char(w, i>>16);
  129. wbuf_nocrc_char(w, i>>8);
  130. wbuf_nocrc_char(w, i>>0);
  131. #else
  132. assert(w->ndone + 4 <= w->size);
  133. #if 0
  134. w->buf[w->ndone+0] = i>>24;
  135. w->buf[w->ndone+1] = i>>16;
  136. w->buf[w->ndone+2] = i>>8;
  137. w->buf[w->ndone+3] = i>>0;
  138. #else
  139. *(uint32_t*)(&w->buf[w->ndone]) = toku_htod32(i);
  140. #endif
  141. w->ndone += 4;
  142. #endif
  143. }
  144. static inline void wbuf_int (struct wbuf *w, int32_t i) {
  145. wbuf_nocrc_int(w, i);
  146. x1764_add(&w->checksum, &w->buf[w->ndone-4], 4);
  147. }
  148. static inline void wbuf_nocrc_uint (struct wbuf *w, uint32_t i) {
  149. wbuf_nocrc_int(w, (int32_t)i);
  150. }
  151. static inline void wbuf_uint (struct wbuf *w, uint32_t i) {
  152. wbuf_int(w, (int32_t)i);
  153. }
  154. static inline void wbuf_nocrc_literal_bytes(struct wbuf *w, bytevec bytes_bv, uint32_t nbytes) {
  155. const unsigned char *bytes = (const unsigned char *) bytes_bv;
  156. #if 0
  157. { int i; for (i=0; i<nbytes; i++) wbuf_nocrc_char(w, bytes[i]); }
  158. #else
  159. assert(w->ndone + nbytes <= w->size);
  160. memcpy(w->buf + w->ndone, bytes, (size_t)nbytes);
  161. w->ndone += nbytes;
  162. #endif
  163. }
  164. static inline void wbuf_literal_bytes(struct wbuf *w, bytevec bytes_bv, uint32_t nbytes) {
  165. wbuf_nocrc_literal_bytes(w, bytes_bv, nbytes);
  166. x1764_add(&w->checksum, &w->buf[w->ndone-nbytes], nbytes);
  167. }
  168. static void wbuf_nocrc_bytes (struct wbuf *w, bytevec bytes_bv, uint32_t nbytes) {
  169. wbuf_nocrc_uint(w, nbytes);
  170. wbuf_nocrc_literal_bytes(w, bytes_bv, nbytes);
  171. }
  172. static void wbuf_bytes (struct wbuf *w, bytevec bytes_bv, uint32_t nbytes) {
  173. wbuf_uint(w, nbytes);
  174. wbuf_literal_bytes(w, bytes_bv, nbytes);
  175. }
  176. static void wbuf_nocrc_ulonglong (struct wbuf *w, uint64_t ull) {
  177. wbuf_nocrc_uint(w, (uint32_t)(ull>>32));
  178. wbuf_nocrc_uint(w, (uint32_t)(ull&0xFFFFFFFF));
  179. }
  180. static void wbuf_ulonglong (struct wbuf *w, uint64_t ull) {
  181. wbuf_uint(w, (uint32_t)(ull>>32));
  182. wbuf_uint(w, (uint32_t)(ull&0xFFFFFFFF));
  183. }
  184. static inline void wbuf_nocrc_uint64_t(struct wbuf *w, uint64_t ull) {
  185. wbuf_nocrc_ulonglong(w, ull);
  186. }
  187. static inline void wbuf_uint64_t(struct wbuf *w, uint64_t ull) {
  188. wbuf_ulonglong(w, ull);
  189. }
  190. static inline void wbuf_nocrc_bool (struct wbuf *w, bool b) {
  191. wbuf_nocrc_uint8_t(w, (uint8_t)(b ? 1 : 0));
  192. }
  193. static inline void wbuf_nocrc_BYTESTRING (struct wbuf *w, BYTESTRING v) {
  194. wbuf_nocrc_bytes(w, v.data, v.len);
  195. }
  196. static inline void wbuf_BYTESTRING (struct wbuf *w, BYTESTRING v) {
  197. wbuf_bytes(w, v.data, v.len);
  198. }
  199. static inline void wbuf_uint8_t (struct wbuf *w, uint8_t v) {
  200. wbuf_char(w, v);
  201. }
  202. static inline void wbuf_nocrc_uint32_t (struct wbuf *w, uint32_t v) {
  203. wbuf_nocrc_uint(w, v);
  204. }
  205. static inline void wbuf_uint32_t (struct wbuf *w, uint32_t v) {
  206. wbuf_uint(w, v);
  207. }
  208. static inline void wbuf_DISKOFF (struct wbuf *w, DISKOFF off) {
  209. wbuf_ulonglong(w, (uint64_t)off);
  210. }
  211. static inline void wbuf_BLOCKNUM (struct wbuf *w, BLOCKNUM b) {
  212. wbuf_ulonglong(w, b.b);
  213. }
  214. static inline void wbuf_nocrc_BLOCKNUM (struct wbuf *w, BLOCKNUM b) {
  215. wbuf_nocrc_ulonglong(w, b.b);
  216. }
  217. static inline void wbuf_nocrc_TXNID (struct wbuf *w, TXNID tid) {
  218. wbuf_nocrc_ulonglong(w, tid);
  219. }
  220. static inline void wbuf_nocrc_TXNID_PAIR (struct wbuf *w, TXNID_PAIR tid) {
  221. wbuf_nocrc_ulonglong(w, tid.parent_id64);
  222. wbuf_nocrc_ulonglong(w, tid.child_id64);
  223. }
  224. static inline void wbuf_TXNID (struct wbuf *w, TXNID tid) {
  225. wbuf_ulonglong(w, tid);
  226. }
  227. static inline void wbuf_nocrc_XIDP (struct wbuf *w, XIDP xid) {
  228. wbuf_nocrc_uint32_t(w, xid->formatID);
  229. wbuf_nocrc_uint8_t(w, xid->gtrid_length);
  230. wbuf_nocrc_uint8_t(w, xid->bqual_length);
  231. wbuf_nocrc_literal_bytes(w, xid->data, xid->gtrid_length+xid->bqual_length);
  232. }
  233. static inline void wbuf_nocrc_LSN (struct wbuf *w, LSN lsn) {
  234. wbuf_nocrc_ulonglong(w, lsn.lsn);
  235. }
  236. static inline void wbuf_LSN (struct wbuf *w, LSN lsn) {
  237. wbuf_ulonglong(w, lsn.lsn);
  238. }
  239. static inline void wbuf_MSN (struct wbuf *w, MSN msn) {
  240. wbuf_ulonglong(w, msn.msn);
  241. }
  242. static inline void wbuf_nocrc_FILENUM (struct wbuf *w, FILENUM fileid) {
  243. wbuf_nocrc_uint(w, fileid.fileid);
  244. }
  245. static inline void wbuf_FILENUM (struct wbuf *w, FILENUM fileid) {
  246. wbuf_uint(w, fileid.fileid);
  247. }
  248. // 2954
  249. static inline void wbuf_nocrc_FILENUMS (struct wbuf *w, FILENUMS v) {
  250. wbuf_nocrc_uint(w, v.num);
  251. uint32_t i;
  252. for (i = 0; i < v.num; i++) {
  253. wbuf_nocrc_FILENUM(w, v.filenums[i]);
  254. }
  255. }
  256. // 2954
  257. static inline void wbuf_FILENUMS (struct wbuf *w, FILENUMS v) {
  258. wbuf_uint(w, v.num);
  259. uint32_t i;
  260. for (i = 0; i < v.num; i++) {
  261. wbuf_FILENUM(w, v.filenums[i]);
  262. }
  263. }
  264. #endif