From e8016325866fb989936019e0ab3aae49afc5f594 Mon Sep 17 00:00:00 2001 From: Angelo Haller Date: Fri, 18 Nov 2022 16:17:31 -0500 Subject: [PATCH] Add MSVC support. MSVC does not support `__attribute__ ((__packed__))`. Add support via `__pragma(pack(push, 1))` when compiling with MSVC. --- sds.h | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/sds.h b/sds.h index adcc12c..fc862f7 100644 --- a/sds.h +++ b/sds.h @@ -42,37 +42,48 @@ extern const char *SDS_NOINIT; typedef char *sds; +#ifdef _MSC_VER +__pragma(pack(push, 1)) +#define _SDS_PACKED +#else +#define _SDS_PACKED __attribute__ ((__packed__)) +#endif + /* Note: sdshdr5 is never used, we just access the flags byte directly. * However is here to document the layout of type 5 SDS strings. */ -struct __attribute__ ((__packed__)) sdshdr5 { +struct _SDS_PACKED sdshdr5 { unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ char buf[]; }; -struct __attribute__ ((__packed__)) sdshdr8 { +struct _SDS_PACKED 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[]; }; -struct __attribute__ ((__packed__)) sdshdr16 { +struct _SDS_PACKED 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[]; }; -struct __attribute__ ((__packed__)) sdshdr32 { +struct _SDS_PACKED 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[]; }; -struct __attribute__ ((__packed__)) sdshdr64 { +struct _SDS_PACKED 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[]; }; +#ifdef _MSC_VER +__pragma(pack(pop)) +#endif + #define SDS_TYPE_5 0 #define SDS_TYPE_8 1 #define SDS_TYPE_16 2