2007-08-21 23:32:17 +00:00
|
|
|
#ifndef RBUF_H
|
|
|
|
#define RBUF_H
|
|
|
|
|
2007-11-29 14:18:54 +00:00
|
|
|
#ident "Copyright (c) 2007 Tokutek Inc. All rights reserved."
|
|
|
|
|
2008-04-07 01:30:25 +00:00
|
|
|
#include "toku_assert.h"
|
2008-04-21 04:22:45 +00:00
|
|
|
#include "memory.h"
|
2008-07-11 22:00:06 +00:00
|
|
|
#include "memarena.h"
|
2008-05-29 03:12:59 +00:00
|
|
|
#include <arpa/inet.h>
|
2007-08-21 23:32:17 +00:00
|
|
|
|
|
|
|
struct rbuf {
|
|
|
|
unsigned char *buf;
|
|
|
|
unsigned int size;
|
|
|
|
unsigned int ndone;
|
|
|
|
};
|
|
|
|
|
2008-05-29 03:12:59 +00:00
|
|
|
static inline unsigned int rbuf_char (struct rbuf *r) {
|
2007-08-21 23:32:17 +00:00
|
|
|
assert(r->ndone<r->size);
|
|
|
|
return r->buf[r->ndone++];
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int rbuf_int (struct rbuf *r) {
|
2008-05-29 03:12:59 +00:00
|
|
|
#if 1
|
|
|
|
assert(r->ndone+4 <= r->size);
|
|
|
|
u_int32_t result = ntohl(*(u_int32_t*)(r->buf+r->ndone)); // This only works on machines where unaligned loads are OK.
|
|
|
|
r->ndone+=4;
|
|
|
|
return result;
|
|
|
|
#else
|
2007-08-21 23:32:17 +00:00
|
|
|
unsigned char c0 = rbuf_char(r);
|
|
|
|
unsigned char c1 = rbuf_char(r);
|
|
|
|
unsigned char c2 = rbuf_char(r);
|
|
|
|
unsigned char c3 = rbuf_char(r);
|
|
|
|
return ((c0<<24)|
|
|
|
|
(c1<<16)|
|
|
|
|
(c2<<8)|
|
|
|
|
(c3<<0));
|
2008-05-29 03:12:59 +00:00
|
|
|
#endif
|
2007-08-21 23:32:17 +00:00
|
|
|
}
|
|
|
|
|
2007-11-14 17:58:38 +00:00
|
|
|
static inline void rbuf_literal_bytes (struct rbuf *r, bytevec *bytes, unsigned int n_bytes) {
|
|
|
|
*bytes = &r->buf[r->ndone];
|
|
|
|
r->ndone+=n_bytes;
|
|
|
|
assert(r->ndone<=r->size);
|
|
|
|
}
|
|
|
|
|
2007-08-21 23:32:17 +00:00
|
|
|
/* Return a pointer into the middle of the buffer. */
|
2008-04-07 01:30:25 +00:00
|
|
|
static inline void rbuf_bytes (struct rbuf *r, bytevec *bytes, unsigned int *n_bytes)
|
2007-08-21 23:32:17 +00:00
|
|
|
{
|
|
|
|
*n_bytes = rbuf_int(r);
|
2007-11-14 17:58:38 +00:00
|
|
|
rbuf_literal_bytes(r, bytes, *n_bytes);
|
|
|
|
}
|
|
|
|
|
2008-04-07 01:30:25 +00:00
|
|
|
static inline unsigned long long rbuf_ulonglong (struct rbuf *r) {
|
2007-11-14 17:58:38 +00:00
|
|
|
unsigned i0 = rbuf_int(r);
|
|
|
|
unsigned i1 = rbuf_int(r);
|
|
|
|
return ((unsigned long long)(i0)<<32) | ((unsigned long long)(i1));
|
2007-08-21 23:32:17 +00:00
|
|
|
}
|
|
|
|
|
2013-04-16 23:57:18 -04:00
|
|
|
static inline signed long long rbuf_longlong (struct rbuf *r) {
|
|
|
|
return (signed long long)rbuf_ulonglong(r);
|
|
|
|
}
|
|
|
|
|
2008-04-07 01:30:25 +00:00
|
|
|
static inline DISKOFF rbuf_diskoff (struct rbuf *r) {
|
2013-04-16 23:57:18 -04:00
|
|
|
return rbuf_ulonglong(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline BLOCKNUM rbuf_blocknum (struct rbuf *r) {
|
|
|
|
BLOCKNUM result = make_blocknum(rbuf_longlong(r));
|
|
|
|
return result;
|
2007-08-21 23:32:17 +00:00
|
|
|
}
|
2008-04-21 04:22:45 +00:00
|
|
|
|
|
|
|
static inline void rbuf_TXNID (struct rbuf *r, TXNID *txnid) {
|
|
|
|
*txnid = rbuf_ulonglong(r);
|
|
|
|
}
|
2008-07-11 22:00:06 +00:00
|
|
|
static inline void rbuf_ma_TXNID (struct rbuf *r, MEMARENA ma __attribute__((__unused__)), TXNID *txnid) {
|
2013-04-16 23:57:20 -04:00
|
|
|
rbuf_TXNID(r, txnid);
|
2008-07-11 22:00:06 +00:00
|
|
|
}
|
2008-04-21 04:22:45 +00:00
|
|
|
|
|
|
|
static inline void rbuf_FILENUM (struct rbuf *r, FILENUM *filenum) {
|
|
|
|
filenum->fileid = rbuf_int(r);
|
|
|
|
}
|
2008-07-11 22:00:06 +00:00
|
|
|
static inline void rbuf_ma_FILENUM (struct rbuf *r, MEMARENA ma __attribute__((__unused__)), FILENUM *filenum) {
|
2013-04-16 23:57:20 -04:00
|
|
|
rbuf_FILENUM(r, filenum);
|
2008-07-11 22:00:06 +00:00
|
|
|
}
|
2008-04-21 04:22:45 +00:00
|
|
|
|
|
|
|
// Don't try to use the same space, malloc it
|
|
|
|
static inline void rbuf_BYTESTRING (struct rbuf *r, BYTESTRING *bs) {
|
|
|
|
bs->len = rbuf_int(r);
|
|
|
|
u_int32_t newndone = r->ndone + bs->len;
|
|
|
|
assert(newndone < r->size);
|
|
|
|
bs->data = toku_memdup(&r->buf[r->ndone], (size_t)bs->len);
|
|
|
|
assert(bs->data);
|
|
|
|
r->ndone = newndone;
|
|
|
|
}
|
|
|
|
|
2008-07-11 22:00:06 +00:00
|
|
|
static inline void rbuf_ma_BYTESTRING (struct rbuf *r, MEMARENA ma, BYTESTRING *bs) {
|
|
|
|
bs->len = rbuf_int(r);
|
|
|
|
u_int32_t newndone = r->ndone + bs->len;
|
|
|
|
assert(newndone < r->size);
|
|
|
|
bs->data = memarena_memdup(ma, &r->buf[r->ndone], (size_t)bs->len);
|
|
|
|
assert(bs->data);
|
|
|
|
r->ndone = newndone;
|
|
|
|
}
|
|
|
|
|
2007-08-21 23:32:17 +00:00
|
|
|
#endif
|