Patch sds.h so that it can be #included from C++

Empty arrays aren't allowed in C++, so a single dummy element has to be added. sds works by scary cast magic so this dummy element is never actually allocated. A #define is used in this patch so in C this compiles exactly the same as before.
This commit is contained in:
mcc 2019-06-28 22:31:00 -04:00 committed by Bjorn
parent 99f8122f23
commit 38886f0dc4
1 changed files with 11 additions and 5 deletions

View File

@ -43,6 +43,12 @@ extern const char *SDS_NOINIT;
typedef char *sds;
#ifdef __cplusplus
#define DUMMY_ELEMENT 1
#else
#define DUMMY_ELEMENT
#endif
#ifdef _MSC_VER
#define PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop))
@ -60,31 +66,31 @@ typedef long ssize_t;
* However is here to document the layout of type 5 SDS strings. */
PACK(struct sdshdr5 {
unsigned char flags; /* 3 lsb of type, and 5 msb of string length */
char buf[];
char buf[DUMMY_ELEMENT];
});
PACK(struct sdshdr8 {
uint8_t len; /* used */
uint8_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
char buf[DUMMY_ELEMENT];
});
PACK(struct sdshdr16 {
uint16_t len; /* used */
uint16_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
char buf[DUMMY_ELEMENT];
});
PACK(struct sdshdr32 {
uint32_t len; /* used */
uint32_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
char buf[DUMMY_ELEMENT];
});
PACK(struct sdshdr64 {
uint64_t len; /* used */
uint64_t alloc; /* excluding the header and null terminator */
unsigned char flags; /* 3 lsb of type, 5 unused bits */
char buf[];
char buf[DUMMY_ELEMENT];
});
#undef PACK