From 0d34c6904eca99ea4e0f48c0a7f8cb7060781cb5 Mon Sep 17 00:00:00 2001 From: Werner Lemberg Date: Thu, 18 Sep 2014 16:36:11 +0200 Subject: [PATCH] Update sds library to commit 14b8e8a1. --- lib/sds.c | 11 +++++++++++ lib/sds.h | 24 ++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/sds.c b/lib/sds.c index 2118018..b960713 100644 --- a/lib/sds.c +++ b/lib/sds.c @@ -36,6 +36,17 @@ #include "sds.h" +typedef struct sdshdr_ { + int len; + int free; + char buf[]; +} sdshdr; + +static inline sdshdr *sds_start(const sds s) +{ + return (sdshdr*) (s-(int)offsetof(sdshdr, buf)); +} + /* Create a new sds string with the content specified by the 'init' pointer * and 'initlen'. * If NULL is used for 'init' the string is initialized with zero bytes. diff --git a/lib/sds.h b/lib/sds.h index 24079bd..eeb80ef 100644 --- a/lib/sds.h +++ b/lib/sds.h @@ -32,6 +32,7 @@ #define SDS_H #ifndef __GNUC__ +# undef __attribute__ # define __attribute__(x) /* nothing */ #endif @@ -47,24 +48,27 @@ extern "C" { typedef char *sds; -typedef struct sdshdr_ { +/* This is a dummy structure to compute `len' and `free' offsets, not to be + * used by applications. The original structure, `sdshdr' (defined in + * `sds.c') uses a flexible array member for `buf', making it invalid for + * C++. Assuming that both C and C++ code gets compiled by the same compiler + * suite, the replacement below should give the same offsets so that `sdslen' + * and `sdavail' can stay as inline definitions. */ +struct sdshdr_h_ { int len; int free; - char buf[]; -} sdshdr; - -static inline sdshdr *sds_start(const sds s) -{ - return (sdshdr*) (s-(int)offsetof(sdshdr, buf)); -} + char buf[1000]; +}; static inline size_t sdslen(const sds s) { - sdshdr *sh = sds_start(s); + struct sdshdr_h_ *sh = (struct sdshdr_h_*) + (s-(int)offsetof(struct sdshdr_h_, buf)); return sh->len; } static inline size_t sdsavail(const sds s) { - sdshdr *sh = sds_start(s); + struct sdshdr_h_ *sh = (struct sdshdr_h_*) + (s-(int)offsetof(struct sdshdr_h_, buf)); return sh->free; } -- 2.11.4.GIT