Skip to content
This repository
Browse code

endian.c/h -> endianconv.c/h to avoid issues with broken libraries se…

…arch paths.
  • Loading branch information...
commit 7a3e372025b2a54486ce52d706b1da95be4b7e7e 1 parent 18aa2b8
Salvatore Sanfilippo authored
10 src/Makefile
@@ -73,7 +73,7 @@ QUIET_CC = @printf ' %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR
73 73 QUIET_LINK = @printf ' %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR);
74 74 endif
75 75
76   -OBJ = adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endian.o slowlog.o scripting.o bio.o rio.o rand.o
  76 +OBJ = adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o
77 77 BENCHOBJ = ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o
78 78 CLIOBJ = anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o
79 79 CHECKDUMPOBJ = redis-check-dump.o lzf_c.o lzf_d.o
@@ -112,8 +112,8 @@ db.o: db.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
112 112 debug.o: debug.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
113 113 zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h util.h sha1.h
114 114 dict.o: dict.c fmacros.h dict.h zmalloc.h
115   -endian.o: endian.c
116   -intset.o: intset.c intset.h zmalloc.h endian.h
  115 +endianconv.o: endianconv.c
  116 +intset.o: intset.c intset.h zmalloc.h endianconv.h
117 117 lzf_c.o: lzf_c.c lzfP.h
118 118 lzf_d.o: lzf_d.c lzfP.h
119 119 multi.o: multi.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
@@ -163,8 +163,8 @@ t_string.o: t_string.c redis.h fmacros.h config.h ae.h sds.h dict.h \
163 163 t_zset.o: t_zset.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
164 164 zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h util.h
165 165 util.o: util.c fmacros.h util.h
166   -ziplist.o: ziplist.c zmalloc.h util.h ziplist.h endian.h
167   -zipmap.o: zipmap.c zmalloc.h endian.h
  166 +ziplist.o: ziplist.c zmalloc.h util.h ziplist.h endianconv.h
  167 +zipmap.o: zipmap.c zmalloc.h endianconv.h
168 168 zmalloc.o: zmalloc.c config.h zmalloc.h
169 169
170 170 # Clean local objects when ARCH is different
80 src/endian.c
... ... @@ -1,80 +0,0 @@
1   -#include <stdint.h>
2   -
3   -/* Toggle the 16 bit unsigned integer pointed by *p from little endian to
4   - * big endian */
5   -void memrev16(void *p) {
6   - unsigned char *x = p, t;
7   -
8   - t = x[0];
9   - x[0] = x[1];
10   - x[1] = t;
11   -}
12   -
13   -/* Toggle the 32 bit unsigned integer pointed by *p from little endian to
14   - * big endian */
15   -void memrev32(void *p) {
16   - unsigned char *x = p, t;
17   -
18   - t = x[0];
19   - x[0] = x[3];
20   - x[3] = t;
21   - t = x[1];
22   - x[1] = x[2];
23   - x[2] = t;
24   -}
25   -
26   -/* Toggle the 64 bit unsigned integer pointed by *p from little endian to
27   - * big endian */
28   -void memrev64(void *p) {
29   - unsigned char *x = p, t;
30   -
31   - t = x[0];
32   - x[0] = x[7];
33   - x[7] = t;
34   - t = x[1];
35   - x[1] = x[6];
36   - x[6] = t;
37   - t = x[2];
38   - x[2] = x[5];
39   - x[5] = t;
40   - t = x[3];
41   - x[3] = x[4];
42   - x[4] = t;
43   -}
44   -
45   -uint16_t intrev16(uint16_t v) {
46   - memrev16(&v);
47   - return v;
48   -}
49   -
50   -uint32_t intrev32(uint32_t v) {
51   - memrev32(&v);
52   - return v;
53   -}
54   -
55   -uint64_t intrev64(uint64_t v) {
56   - memrev64(&v);
57   - return v;
58   -}
59   -
60   -#ifdef TESTMAIN
61   -#include <stdio.h>
62   -
63   -int main(void) {
64   - char buf[32];
65   -
66   - sprintf(buf,"ciaoroma");
67   - memrev16(buf);
68   - printf("%s\n", buf);
69   -
70   - sprintf(buf,"ciaoroma");
71   - memrev32(buf);
72   - printf("%s\n", buf);
73   -
74   - sprintf(buf,"ciaoroma");
75   - memrev64(buf);
76   - printf("%s\n", buf);
77   -
78   - return 0;
79   -}
80   -#endif
29 src/endian.h
... ... @@ -1,29 +0,0 @@
1   -#ifndef __ENDIAN_H
2   -#define __ENDIAN_H
3   -
4   -void memrev16(void *p);
5   -void memrev32(void *p);
6   -void memrev64(void *p);
7   -uint16_t intrev16(uint16_t v);
8   -uint32_t intrev32(uint32_t v);
9   -uint64_t intrev64(uint64_t v);
10   -
11   -/* variants of the function doing the actual convertion only if the target
12   - * host is big endian */
13   -#if (BYTE_ORDER == LITTLE_ENDIAN)
14   -#define memrev16ifbe(p)
15   -#define memrev32ifbe(p)
16   -#define memrev64ifbe(p)
17   -#define intrev16ifbe(v) (v)
18   -#define intrev32ifbe(v) (v)
19   -#define intrev64ifbe(v) (v)
20   -#else
21   -#define memrev16ifbe(p) memrev16(p)
22   -#define memrev32ifbe(p) memrev32(p)
23   -#define memrev64ifbe(p) memrev64(p)
24   -#define intrev16ifbe(v) intrev16(v)
25   -#define intrev32ifbe(v) intrev32(v)
26   -#define intrev64ifbe(v) intrev64(v)
27   -#endif
28   -
29   -#endif
124 src/endianconv.c
... ... @@ -0,0 +1,124 @@
  1 +/* endinconv.c -- Endian conversions utilities.
  2 + *
  3 + * This functions are never called directly, but always using the macros
  4 + * defined into endianconv.h, this way we define everything is a non-operation
  5 + * if the arch is already little endian.
  6 + *
  7 + * Redis tries to encode everything as little endian (but a few things that need
  8 + * to be backward compatible are still in big endian) because most of the
  9 + * production environments are little endian, and we have a lot of conversions
  10 + * in a few places because ziplists, intsets, zipmaps, need to be endian-neutral
  11 + * even in memory, since they are serialied on RDB files directly with a single
  12 + * write(2) without other additional steps.
  13 + *
  14 + * ----------------------------------------------------------------------------
  15 + *
  16 + * Copyright (c) 2011-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  17 + * All rights reserved.
  18 + *
  19 + * Redistribution and use in source and binary forms, with or without
  20 + * modification, are permitted provided that the following conditions are met:
  21 + *
  22 + * * Redistributions of source code must retain the above copyright notice,
  23 + * this list of conditions and the following disclaimer.
  24 + * * Redistributions in binary form must reproduce the above copyright
  25 + * notice, this list of conditions and the following disclaimer in the
  26 + * documentation and/or other materials provided with the distribution.
  27 + * * Neither the name of Redis nor the names of its contributors may be used
  28 + * to endorse or promote products derived from this software without
  29 + * specific prior written permission.
  30 + *
  31 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  32 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  39 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41 + * POSSIBILITY OF SUCH DAMAGE.
  42 + */
  43 +
  44 +
  45 +#include <stdint.h>
  46 +
  47 +/* Toggle the 16 bit unsigned integer pointed by *p from little endian to
  48 + * big endian */
  49 +void memrev16(void *p) {
  50 + unsigned char *x = p, t;
  51 +
  52 + t = x[0];
  53 + x[0] = x[1];
  54 + x[1] = t;
  55 +}
  56 +
  57 +/* Toggle the 32 bit unsigned integer pointed by *p from little endian to
  58 + * big endian */
  59 +void memrev32(void *p) {
  60 + unsigned char *x = p, t;
  61 +
  62 + t = x[0];
  63 + x[0] = x[3];
  64 + x[3] = t;
  65 + t = x[1];
  66 + x[1] = x[2];
  67 + x[2] = t;
  68 +}
  69 +
  70 +/* Toggle the 64 bit unsigned integer pointed by *p from little endian to
  71 + * big endian */
  72 +void memrev64(void *p) {
  73 + unsigned char *x = p, t;
  74 +
  75 + t = x[0];
  76 + x[0] = x[7];
  77 + x[7] = t;
  78 + t = x[1];
  79 + x[1] = x[6];
  80 + x[6] = t;
  81 + t = x[2];
  82 + x[2] = x[5];
  83 + x[5] = t;
  84 + t = x[3];
  85 + x[3] = x[4];
  86 + x[4] = t;
  87 +}
  88 +
  89 +uint16_t intrev16(uint16_t v) {
  90 + memrev16(&v);
  91 + return v;
  92 +}
  93 +
  94 +uint32_t intrev32(uint32_t v) {
  95 + memrev32(&v);
  96 + return v;
  97 +}
  98 +
  99 +uint64_t intrev64(uint64_t v) {
  100 + memrev64(&v);
  101 + return v;
  102 +}
  103 +
  104 +#ifdef TESTMAIN
  105 +#include <stdio.h>
  106 +
  107 +int main(void) {
  108 + char buf[32];
  109 +
  110 + sprintf(buf,"ciaoroma");
  111 + memrev16(buf);
  112 + printf("%s\n", buf);
  113 +
  114 + sprintf(buf,"ciaoroma");
  115 + memrev32(buf);
  116 + printf("%s\n", buf);
  117 +
  118 + sprintf(buf,"ciaoroma");
  119 + memrev64(buf);
  120 + printf("%s\n", buf);
  121 +
  122 + return 0;
  123 +}
  124 +#endif
61 src/endianconv.h
... ... @@ -0,0 +1,61 @@
  1 +/* See endianconv.c top comments for more information
  2 + *
  3 + * ----------------------------------------------------------------------------
  4 + *
  5 + * Copyright (c) 2011-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  6 + * All rights reserved.
  7 + *
  8 + * Redistribution and use in source and binary forms, with or without
  9 + * modification, are permitted provided that the following conditions are met:
  10 + *
  11 + * * Redistributions of source code must retain the above copyright notice,
  12 + * this list of conditions and the following disclaimer.
  13 + * * Redistributions in binary form must reproduce the above copyright
  14 + * notice, this list of conditions and the following disclaimer in the
  15 + * documentation and/or other materials provided with the distribution.
  16 + * * Neither the name of Redis nor the names of its contributors may be used
  17 + * to endorse or promote products derived from this software without
  18 + * specific prior written permission.
  19 + *
  20 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30 + * POSSIBILITY OF SUCH DAMAGE.
  31 + */
  32 +
  33 +#ifndef __ENDIANCONV_H
  34 +#define __ENDIANCONV_H
  35 +
  36 +void memrev16(void *p);
  37 +void memrev32(void *p);
  38 +void memrev64(void *p);
  39 +uint16_t intrev16(uint16_t v);
  40 +uint32_t intrev32(uint32_t v);
  41 +uint64_t intrev64(uint64_t v);
  42 +
  43 +/* variants of the function doing the actual convertion only if the target
  44 + * host is big endian */
  45 +#if (BYTE_ORDER == LITTLE_ENDIAN)
  46 +#define memrev16ifbe(p)
  47 +#define memrev32ifbe(p)
  48 +#define memrev64ifbe(p)
  49 +#define intrev16ifbe(v) (v)
  50 +#define intrev32ifbe(v) (v)
  51 +#define intrev64ifbe(v) (v)
  52 +#else
  53 +#define memrev16ifbe(p) memrev16(p)
  54 +#define memrev32ifbe(p) memrev32(p)
  55 +#define memrev64ifbe(p) memrev64(p)
  56 +#define intrev16ifbe(v) intrev16(v)
  57 +#define intrev32ifbe(v) intrev32(v)
  58 +#define intrev64ifbe(v) intrev64(v)
  59 +#endif
  60 +
  61 +#endif
2  src/intset.c
@@ -3,7 +3,7 @@
3 3 #include <string.h>
4 4 #include "intset.h"
5 5 #include "zmalloc.h"
6   -#include "endian.h"
  6 +#include "endianconv.h"
7 7
8 8 /* Note that these encodings are ordered, so:
9 9 * INTSET_ENC_INT16 < INTSET_ENC_INT32 < INTSET_ENC_INT64. */
2  src/ziplist.c
@@ -69,7 +69,7 @@
69 69 #include "zmalloc.h"
70 70 #include "util.h"
71 71 #include "ziplist.h"
72   -#include "endian.h"
  72 +#include "endianconv.h"
73 73
74 74 #define ZIP_END 255
75 75 #define ZIP_BIGLEN 254
2  src/zipmap.c
@@ -80,7 +80,7 @@
80 80 #include <string.h>
81 81 #include <assert.h>
82 82 #include "zmalloc.h"
83   -#include "endian.h"
  83 +#include "endianconv.h"
84 84
85 85 #define ZIPMAP_BIGLEN 254
86 86 #define ZIPMAP_END 255

0 comments on commit 7a3e372

Please sign in to comment.
Something went wrong with that request. Please try again.