1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #pragma once
40 
41 /* Purpose of this file is to provide definitions of
42  * Host to Disk byte transposition functions, an abstraction of
43  * htod32()/dtoh32() and htod16()/dtoh16() functions.
44  *
45  * These htod/dtoh functions will only perform the transposition
46  * if the disk and host are defined to be in opposite endian-ness.
47  * If we define the disk to be in host order, then no byte
48  * transposition is performed.  (We might do this to save the
49  * the time used for byte transposition.)
50  *
51  * This abstraction layer allows us to define the disk to be in
52  * any byte order with a single compile-time switch (in htod.c).
53  *
54  * NOTE: THIS FILE DOES NOT CURRENTLY SUPPORT A BIG-ENDIAN
55  *       HOST AND A LITTLE-ENDIAN DISK.
56  */
57 
58 #include <portability/toku_config.h>
59 
60 #if defined(HAVE_ENDIAN_H)
61 # include <endian.h>
62 #elif defined(HAVE_MACHINE_ENDIAN_H)
63 # include <machine/endian.h>
64 # define __BYTE_ORDER __DARWIN_BYTE_ORDER
65 # define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
66 # define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
67 #endif
68 #if !defined(__BYTE_ORDER) || \
69     !defined(__LITTLE_ENDIAN) || \
70     !defined(__BIG_ENDIAN)
71 #error Standard endianness things not all defined
72 #endif
73 
74 
75 static const int64_t toku_byte_order_host = 0x0102030405060708LL;
76 
77 #define NETWORK_BYTE_ORDER  (__BIG_ENDIAN)
78 #define INTEL_BYTE_ORDER    (__LITTLE_ENDIAN)
79 #define HOST_BYTE_ORDER     (__BYTE_ORDER)
80 
81 //DISK_BYTE_ORDER is the byte ordering for integers written to disk.
82 //If DISK_BYTE_ORDER is the same as HOST_BYTE_ORDER no conversions are necessary.
83 //Otherwise some structures require conversion to HOST_BYTE_ORDER on loading from disk (HOST_BYTE_ORDER in memory), and
84 //others require conversion to HOST_BYTE_ORDER on every access/mutate (DISK_BYTE_ORDER in memory).
85 #define DISK_BYTE_ORDER     (INTEL_BYTE_ORDER)
86 
87 #if HOST_BYTE_ORDER!=INTEL_BYTE_ORDER
88 //Even though the functions are noops if DISK==HOST, we do not have the logic to test whether the file was moved from another BYTE_ORDER machine.
89 #error Only intel byte order supported so far.
90 #endif
91 
92 #if DISK_BYTE_ORDER == HOST_BYTE_ORDER
93 static inline uint64_t
toku_dtoh64(uint64_t i)94 toku_dtoh64(uint64_t i) {
95     return i;
96 }
97 
98 static inline uint64_t
toku_htod64(uint64_t i)99 toku_htod64(uint64_t i) {
100     return i;
101 }
102 
103 static inline uint32_t
toku_dtoh32(uint32_t i)104 toku_dtoh32(uint32_t i) {
105     return i;
106 }
107 
108 static inline uint32_t
toku_htod32(uint32_t i)109 toku_htod32(uint32_t i) {
110     return i;
111 }
112 #else
113 #error Not supported
114 #endif
115