2007-08-21 23:32:17 +00:00
# ifndef RBUF_H
# define RBUF_H
2013-04-16 23:57:48 -04:00
# ident "$Id$"
2013-04-16 23:57:48 -04:00
# ident "Copyright (c) 2007, 2008, 2009 Tokutek Inc. All rights reserved."
# 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."
2007-11-29 14:18:54 +00:00
2013-04-16 23:57:29 -04:00
# include "toku_portability.h"
2013-04-16 23:57:21 -04:00
# include "memarena.h"
2008-04-07 01:30:25 +00:00
# include "toku_assert.h"
2013-04-16 23:57:21 -04:00
# include "brttypes.h"
2008-04-21 04:22:45 +00:00
# include "memory.h"
2013-04-16 23:57:46 -04:00
# include "toku_htonl.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 + + ] ;
}
2013-04-16 23:57:46 -04:00
//Read an int that MUST be in network order regardless of disk order
static unsigned int rbuf_network_int ( struct rbuf * r ) __attribute__ ( ( __unused__ ) ) ;
static unsigned int rbuf_network_int ( struct rbuf * r ) {
assert ( r - > ndone + 4 < = r - > size ) ;
u_int32_t result = toku_ntohl ( * ( u_int32_t * ) ( r - > buf + r - > ndone ) ) ; // This only works on machines where unaligned loads are OK.
r - > ndone + = 4 ;
return result ;
}
2007-08-21 23:32:17 +00:00
static unsigned int rbuf_int ( struct rbuf * r ) {
2008-05-29 03:12:59 +00:00
# if 1
assert ( r - > ndone + 4 < = r - > size ) ;
2013-04-16 23:57:46 -04:00
u_int32_t result = toku_dtoh32 ( * ( u_int32_t * ) ( r - > buf + r - > ndone ) ) ; // This only works on machines where unaligned loads are OK.
2008-05-29 03:12:59 +00:00
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 ) {
2013-04-16 23:57:41 -04:00
unsigned i0 = rbuf_int ( r ) ;
2007-11-14 17:58:38 +00:00
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 ) ;
}
2013-04-16 23:57:47 -04:00
static inline LSN rbuf_lsn ( struct rbuf * r ) {
LSN lsn = { rbuf_ulonglong ( r ) } ;
return lsn ;
}
2013-04-16 23:57:18 -04:00
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