diff --git a/src/lib/sds/sds.c b/src/lib/sds/sds.c index 39ad595e..a1b837c7 100644 --- a/src/lib/sds/sds.c +++ b/src/lib/sds/sds.c @@ -1,5 +1,6 @@ -/* SDSLib 2.0 -- A C dynamic strings library +/* SDSLib 2.x -- A C dynamic strings library * + * Copyright (c) 2018 Lynn Kirby * Copyright (c) 2006-2015, Salvatore Sanfilippo * Copyright (c) 2015, Oran Agra * Copyright (c) 2015, Redis Labs, Inc @@ -1246,9 +1247,8 @@ int sdsTest(void) { memcmp(y,"\"\\a\\n\\x00foo\\r\"",15) == 0) { - unsigned int oldfree; char *p; - int step = 10, j, i; + size_t step = 10, j, i; sdsfree(x); sdsfree(y); @@ -1258,14 +1258,13 @@ int sdsTest(void) { /* Run the test a few times in order to hit the first two * SDS header types. */ for (i = 0; i < 10; i++) { - int oldlen = sdslen(x); + size_t oldlen = sdslen(x); x = sdsMakeRoomFor(x,step); int type = x[-1]&SDS_TYPE_MASK; test_cond("sdsMakeRoomFor() len", sdslen(x) == oldlen); if (type != SDS_TYPE_5) { test_cond("sdsMakeRoomFor() free", sdsavail(x) >= step); - oldfree = sdsavail(x); } p = x+oldlen; for (j = 0; j < step; j++) { diff --git a/src/lib/sds/sds.h b/src/lib/sds/sds.h index 1bdb60de..f99d3418 100644 --- a/src/lib/sds/sds.h +++ b/src/lib/sds/sds.h @@ -1,5 +1,6 @@ -/* SDSLib 2.0 -- A C dynamic strings library +/* SDSLib 2.x -- A C dynamic strings library * + * Copyright (c) 2018 Lynn Kirby * Copyright (c) 2006-2015, Salvatore Sanfilippo * Copyright (c) 2015, Oran Agra * Copyright (c) 2015, Redis Labs, Inc @@ -42,36 +43,51 @@ const char *SDS_NOINIT; typedef char *sds; +#ifdef _MSC_VER +#define PACK(decl) __pragma(pack(push, 1)) decl __pragma(pack(pop)) + +#if defined(_WIN64) +typedef __int64 ssize_t; +#else +typedef long ssize_t; +#endif + +#else +#define PACK(decl) decl __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 { +PACK(struct sdshdr5 { unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ char buf[]; -}; -struct __attribute__ ((__packed__)) sdshdr8 { +}); +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[]; -}; -struct __attribute__ ((__packed__)) sdshdr16 { +}); +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[]; -}; -struct __attribute__ ((__packed__)) sdshdr32 { +}); +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[]; -}; -struct __attribute__ ((__packed__)) sdshdr64 { +}); +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[]; -}; +}); + +#undef PACK #define SDS_TYPE_5 0 #define SDS_TYPE_8 1 @@ -80,7 +96,7 @@ struct __attribute__ ((__packed__)) sdshdr64 { #define SDS_TYPE_64 4 #define SDS_TYPE_MASK 7 #define SDS_TYPE_BITS 3 -#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (void*)((s)-(sizeof(struct sdshdr##T))); +#define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))); #define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)))) #define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS)