chiark / gitweb /
unaligned.h: rationalise; provide buf_append_uint8 et al
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Jul 2013 17:30:46 +0000 (18:30 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Jul 2013 17:30:46 +0000 (18:30 +0100)
Replace the formulaic macros
  buf_{,un}{append,prepend}_{uint32,uint16}
with some macro-generated inline functions.  These have better
typechecking, and are also better because it's not possible to
accidentally mess up one of the definitions by failing to permute it
in the right way.

Add the functions for uint8 too.  This involves providing put_uint8
and get_uint8 macros, which we do along the lines of the existing put_
and get_ macros.

Use the new uint8 function in the one place where it's currently useful.
More call sites will appear shortly.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
slip.c
unaligned.h

diff --git a/slip.c b/slip.c
index a4529262bd1b913de9ec6d40d317cf3583d9d703..d8f1a17113f4cd20b8711617f684c07a1f1f159a 100644 (file)
--- a/slip.c
+++ b/slip.c
@@ -7,6 +7,7 @@
 #include "util.h"
 #include "netlink.h"
 #include "process.h"
+#include "unaligned.h"
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -125,7 +126,7 @@ static void slip_unstuff(struct slip *st, uint8_t *buf, uint32_t l)
                buffer_init(st->buff,st->nl.max_start_pad);
            } else if (outputchr != OUTPUT_NOTHING) {
                if (st->buff->size < st->buff->len) {
-                   *(uint8_t *)buf_append(st->buff,1)=outputchr;
+                   buf_append_uint8(st->buff,outputchr);
                } else {
                    Message(M_WARNING, "userv_afterpoll: dropping overlong"
                            " SLIP packet\n");
index a15043e56597eb4ad85ffb01391006c0ffaf5902..00c6fc6bca568f6d7c4fd78d8c542279c08bc850 100644 (file)
@@ -2,6 +2,7 @@
 #define unaligned_h
 
 #include <stdint.h>
+#include "util.h"
 
 /* Parts of the secnet key-exchange protocol require access to
    unaligned big-endian quantities in buffers. These macros provide
 
 #define put_uint16(a,v) do {(a)[0]=((v)&0xff00)>>8; (a)[1]=(v)&0xff;} while(0)
 
+#define put_uint8(a,v) do {(a)[0]=((v)&0xff);} while(0)
+
 #define get_uint32(a)                                  \
   (((uint32_t)(a)[0]<<24) | ((uint32_t)(a)[1]<<16) |   \
    ((uint32_t)(a)[2]<<8)  |  (uint32_t)(a)[3])
 
 #define get_uint16(a) (((uint16_t)(a)[0]<<8)|(uint16_t)(a)[1])
 
-#define buf_append_uint32(buf,v) do { uint8_t *c=buf_append((buf),4); \
-    put_uint32(c,(v)); } while(0)
-
-#define buf_append_uint16(buf,v) do { uint8_t *c=buf_append((buf),2); \
-       put_uint16(c,(v)); } while(0)
-
-#define buf_prepend_uint32(buf,v) do { uint8_t *c=buf_prepend((buf),4); \
-           put_uint32(c,(v)); } while(0)
-
-#define buf_prepend_uint16(buf,v) do { uint8_t *c=buf_prepend((buf),2); \
-               put_uint16(c,(v)); } while(0)
-
-#define buf_unappend_uint32(buf) ({uint8_t *c=buf_unappend((buf),4); \
-                   get_uint32(c);})
-
-#define buf_unappend_uint16(buf) ({uint8_t *c=buf_unappend((buf),2); \
-                       get_uint16(c);})
-
-#define buf_unprepend_uint32(buf) ({uint8_t *c=buf_unprepend((buf),4); \
-                       get_uint32(c);})
-
-#define buf_unprepend_uint16(buf) ({uint8_t *c=buf_unprepend((buf),2); \
-                       get_uint16(c);})
+#define get_uint8(a) (((uint8_t)(a)[0]))
+
+#define UNALIGNED_DEF_FORTYPE(type,appre)                              \
+static inline void buf_##appre##_##type(struct buffer_if *buf, type##_t v) \
+{                                                                      \
+    uint8_t *c=buf_##appre(buf,sizeof(type##_t));                      \
+    put_##type(c,v);                                                   \
+}                                                                      \
+static inline type##_t buf_un##appre##_##type(struct buffer_if *buf)   \
+{                                                                      \
+    const uint8_t *c=buf_un##appre(buf,sizeof(type##_t));              \
+    return get_##type(c);                                              \
+}
+
+UNALIGNED_DEF_FORTYPE(uint32,append)
+UNALIGNED_DEF_FORTYPE(uint16,append)
+UNALIGNED_DEF_FORTYPE(uint8,append)
+UNALIGNED_DEF_FORTYPE(uint32,prepend)
+UNALIGNED_DEF_FORTYPE(uint16,prepend)
+UNALIGNED_DEF_FORTYPE(uint8,prepend)
 
 #endif /* unaligned_h */