diff options
Diffstat (limited to 'examples/redis-unstable/deps/hiredis/sds.h')
| -rw-r--r-- | examples/redis-unstable/deps/hiredis/sds.h | 280 |
1 files changed, 0 insertions, 280 deletions
diff --git a/examples/redis-unstable/deps/hiredis/sds.h b/examples/redis-unstable/deps/hiredis/sds.h deleted file mode 100644 index 80784bc..0000000 --- a/examples/redis-unstable/deps/hiredis/sds.h +++ /dev/null | |||
| @@ -1,280 +0,0 @@ | |||
| 1 | /* SDSLib 2.0 -- A C dynamic strings library | ||
| 2 | * | ||
| 3 | * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com> | ||
| 4 | * Copyright (c) 2015, Oran Agra | ||
| 5 | * Copyright (c) 2015, Redis Labs, Inc | ||
| 6 | * All rights reserved. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions are met: | ||
| 10 | * | ||
| 11 | * * Redistributions of source code must retain the above copyright notice, | ||
| 12 | * this list of conditions and the following disclaimer. | ||
| 13 | * * Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * * Neither the name of Redis nor the names of its contributors may be used | ||
| 17 | * to endorse or promote products derived from this software without | ||
| 18 | * specific prior written permission. | ||
| 19 | * | ||
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 21 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | */ | ||
| 32 | |||
| 33 | #ifndef HIREDIS_SDS_H | ||
| 34 | #define HIREDIS_SDS_H | ||
| 35 | |||
| 36 | #define HI_SDS_MAX_PREALLOC (1024*1024) | ||
| 37 | #ifdef _MSC_VER | ||
| 38 | typedef long long ssize_t; | ||
| 39 | #define SSIZE_MAX (LLONG_MAX >> 1) | ||
| 40 | #ifndef __clang__ | ||
| 41 | #define __attribute__(x) | ||
| 42 | #endif | ||
| 43 | #endif | ||
| 44 | |||
| 45 | #include <sys/types.h> | ||
| 46 | #include <stdarg.h> | ||
| 47 | #include <stdint.h> | ||
| 48 | |||
| 49 | typedef char *hisds; | ||
| 50 | |||
| 51 | /* Note: sdshdr5 is never used, we just access the flags byte directly. | ||
| 52 | * However is here to document the layout of type 5 SDS strings. */ | ||
| 53 | struct __attribute__ ((__packed__)) hisdshdr5 { | ||
| 54 | unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ | ||
| 55 | char buf[]; | ||
| 56 | }; | ||
| 57 | struct __attribute__ ((__packed__)) hisdshdr8 { | ||
| 58 | uint8_t len; /* used */ | ||
| 59 | uint8_t alloc; /* excluding the header and null terminator */ | ||
| 60 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 61 | char buf[]; | ||
| 62 | }; | ||
| 63 | struct __attribute__ ((__packed__)) hisdshdr16 { | ||
| 64 | uint16_t len; /* used */ | ||
| 65 | uint16_t alloc; /* excluding the header and null terminator */ | ||
| 66 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 67 | char buf[]; | ||
| 68 | }; | ||
| 69 | struct __attribute__ ((__packed__)) hisdshdr32 { | ||
| 70 | uint32_t len; /* used */ | ||
| 71 | uint32_t alloc; /* excluding the header and null terminator */ | ||
| 72 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 73 | char buf[]; | ||
| 74 | }; | ||
| 75 | struct __attribute__ ((__packed__)) hisdshdr64 { | ||
| 76 | uint64_t len; /* used */ | ||
| 77 | uint64_t alloc; /* excluding the header and null terminator */ | ||
| 78 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 79 | char buf[]; | ||
| 80 | }; | ||
| 81 | |||
| 82 | #define HI_SDS_TYPE_5 0 | ||
| 83 | #define HI_SDS_TYPE_8 1 | ||
| 84 | #define HI_SDS_TYPE_16 2 | ||
| 85 | #define HI_SDS_TYPE_32 3 | ||
| 86 | #define HI_SDS_TYPE_64 4 | ||
| 87 | #define HI_SDS_TYPE_MASK 7 | ||
| 88 | #define HI_SDS_TYPE_BITS 3 | ||
| 89 | #define HI_SDS_HDR_VAR(T,s) struct hisdshdr##T *sh = (struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T))); | ||
| 90 | #define HI_SDS_HDR(T,s) ((struct hisdshdr##T *)((s)-(sizeof(struct hisdshdr##T)))) | ||
| 91 | #define HI_SDS_TYPE_5_LEN(f) ((f)>>HI_SDS_TYPE_BITS) | ||
| 92 | |||
| 93 | static inline size_t hi_sdslen(const hisds s) { | ||
| 94 | unsigned char flags = s[-1]; | ||
| 95 | switch(flags & HI_SDS_TYPE_MASK) { | ||
| 96 | case HI_SDS_TYPE_5: | ||
| 97 | return HI_SDS_TYPE_5_LEN(flags); | ||
| 98 | case HI_SDS_TYPE_8: | ||
| 99 | return HI_SDS_HDR(8,s)->len; | ||
| 100 | case HI_SDS_TYPE_16: | ||
| 101 | return HI_SDS_HDR(16,s)->len; | ||
| 102 | case HI_SDS_TYPE_32: | ||
| 103 | return HI_SDS_HDR(32,s)->len; | ||
| 104 | case HI_SDS_TYPE_64: | ||
| 105 | return HI_SDS_HDR(64,s)->len; | ||
| 106 | } | ||
| 107 | return 0; | ||
| 108 | } | ||
| 109 | |||
| 110 | static inline size_t hi_sdsavail(const hisds s) { | ||
| 111 | unsigned char flags = s[-1]; | ||
| 112 | switch(flags&HI_SDS_TYPE_MASK) { | ||
| 113 | case HI_SDS_TYPE_5: { | ||
| 114 | return 0; | ||
| 115 | } | ||
| 116 | case HI_SDS_TYPE_8: { | ||
| 117 | HI_SDS_HDR_VAR(8,s); | ||
| 118 | return sh->alloc - sh->len; | ||
| 119 | } | ||
| 120 | case HI_SDS_TYPE_16: { | ||
| 121 | HI_SDS_HDR_VAR(16,s); | ||
| 122 | return sh->alloc - sh->len; | ||
| 123 | } | ||
| 124 | case HI_SDS_TYPE_32: { | ||
| 125 | HI_SDS_HDR_VAR(32,s); | ||
| 126 | return sh->alloc - sh->len; | ||
| 127 | } | ||
| 128 | case HI_SDS_TYPE_64: { | ||
| 129 | HI_SDS_HDR_VAR(64,s); | ||
| 130 | return sh->alloc - sh->len; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | static inline void hi_sdssetlen(hisds s, size_t newlen) { | ||
| 137 | unsigned char flags = s[-1]; | ||
| 138 | switch(flags&HI_SDS_TYPE_MASK) { | ||
| 139 | case HI_SDS_TYPE_5: | ||
| 140 | { | ||
| 141 | unsigned char *fp = ((unsigned char*)s)-1; | ||
| 142 | *fp = (unsigned char)(HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS)); | ||
| 143 | } | ||
| 144 | break; | ||
| 145 | case HI_SDS_TYPE_8: | ||
| 146 | HI_SDS_HDR(8,s)->len = (uint8_t)newlen; | ||
| 147 | break; | ||
| 148 | case HI_SDS_TYPE_16: | ||
| 149 | HI_SDS_HDR(16,s)->len = (uint16_t)newlen; | ||
| 150 | break; | ||
| 151 | case HI_SDS_TYPE_32: | ||
| 152 | HI_SDS_HDR(32,s)->len = (uint32_t)newlen; | ||
| 153 | break; | ||
| 154 | case HI_SDS_TYPE_64: | ||
| 155 | HI_SDS_HDR(64,s)->len = (uint64_t)newlen; | ||
| 156 | break; | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | static inline void hi_sdsinclen(hisds s, size_t inc) { | ||
| 161 | unsigned char flags = s[-1]; | ||
| 162 | switch(flags&HI_SDS_TYPE_MASK) { | ||
| 163 | case HI_SDS_TYPE_5: | ||
| 164 | { | ||
| 165 | unsigned char *fp = ((unsigned char*)s)-1; | ||
| 166 | unsigned char newlen = HI_SDS_TYPE_5_LEN(flags)+(unsigned char)inc; | ||
| 167 | *fp = HI_SDS_TYPE_5 | (newlen << HI_SDS_TYPE_BITS); | ||
| 168 | } | ||
| 169 | break; | ||
| 170 | case HI_SDS_TYPE_8: | ||
| 171 | HI_SDS_HDR(8,s)->len += (uint8_t)inc; | ||
| 172 | break; | ||
| 173 | case HI_SDS_TYPE_16: | ||
| 174 | HI_SDS_HDR(16,s)->len += (uint16_t)inc; | ||
| 175 | break; | ||
| 176 | case HI_SDS_TYPE_32: | ||
| 177 | HI_SDS_HDR(32,s)->len += (uint32_t)inc; | ||
| 178 | break; | ||
| 179 | case HI_SDS_TYPE_64: | ||
| 180 | HI_SDS_HDR(64,s)->len += (uint64_t)inc; | ||
| 181 | break; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | /* hi_sdsalloc() = hi_sdsavail() + hi_sdslen() */ | ||
| 186 | static inline size_t hi_sdsalloc(const hisds s) { | ||
| 187 | unsigned char flags = s[-1]; | ||
| 188 | switch(flags & HI_SDS_TYPE_MASK) { | ||
| 189 | case HI_SDS_TYPE_5: | ||
| 190 | return HI_SDS_TYPE_5_LEN(flags); | ||
| 191 | case HI_SDS_TYPE_8: | ||
| 192 | return HI_SDS_HDR(8,s)->alloc; | ||
| 193 | case HI_SDS_TYPE_16: | ||
| 194 | return HI_SDS_HDR(16,s)->alloc; | ||
| 195 | case HI_SDS_TYPE_32: | ||
| 196 | return HI_SDS_HDR(32,s)->alloc; | ||
| 197 | case HI_SDS_TYPE_64: | ||
| 198 | return HI_SDS_HDR(64,s)->alloc; | ||
| 199 | } | ||
| 200 | return 0; | ||
| 201 | } | ||
| 202 | |||
| 203 | static inline void hi_sdssetalloc(hisds s, size_t newlen) { | ||
| 204 | unsigned char flags = s[-1]; | ||
| 205 | switch(flags&HI_SDS_TYPE_MASK) { | ||
| 206 | case HI_SDS_TYPE_5: | ||
| 207 | /* Nothing to do, this type has no total allocation info. */ | ||
| 208 | break; | ||
| 209 | case HI_SDS_TYPE_8: | ||
| 210 | HI_SDS_HDR(8,s)->alloc = (uint8_t)newlen; | ||
| 211 | break; | ||
| 212 | case HI_SDS_TYPE_16: | ||
| 213 | HI_SDS_HDR(16,s)->alloc = (uint16_t)newlen; | ||
| 214 | break; | ||
| 215 | case HI_SDS_TYPE_32: | ||
| 216 | HI_SDS_HDR(32,s)->alloc = (uint32_t)newlen; | ||
| 217 | break; | ||
| 218 | case HI_SDS_TYPE_64: | ||
| 219 | HI_SDS_HDR(64,s)->alloc = (uint64_t)newlen; | ||
| 220 | break; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | hisds hi_sdsnewlen(const void *init, size_t initlen); | ||
| 225 | hisds hi_sdsnew(const char *init); | ||
| 226 | hisds hi_sdsempty(void); | ||
| 227 | hisds hi_sdsdup(const hisds s); | ||
| 228 | void hi_sdsfree(hisds s); | ||
| 229 | hisds hi_sdsgrowzero(hisds s, size_t len); | ||
| 230 | hisds hi_sdscatlen(hisds s, const void *t, size_t len); | ||
| 231 | hisds hi_sdscat(hisds s, const char *t); | ||
| 232 | hisds hi_sdscatsds(hisds s, const hisds t); | ||
| 233 | hisds hi_sdscpylen(hisds s, const char *t, size_t len); | ||
| 234 | hisds hi_sdscpy(hisds s, const char *t); | ||
| 235 | |||
| 236 | hisds hi_sdscatvprintf(hisds s, const char *fmt, va_list ap); | ||
| 237 | #ifdef __GNUC__ | ||
| 238 | hisds hi_sdscatprintf(hisds s, const char *fmt, ...) | ||
| 239 | __attribute__((format(printf, 2, 3))); | ||
| 240 | #else | ||
| 241 | hisds hi_sdscatprintf(hisds s, const char *fmt, ...); | ||
| 242 | #endif | ||
| 243 | |||
| 244 | hisds hi_sdscatfmt(hisds s, char const *fmt, ...); | ||
| 245 | hisds hi_sdstrim(hisds s, const char *cset); | ||
| 246 | int hi_sdsrange(hisds s, ssize_t start, ssize_t end); | ||
| 247 | void hi_sdsupdatelen(hisds s); | ||
| 248 | void hi_sdsclear(hisds s); | ||
| 249 | int hi_sdscmp(const hisds s1, const hisds s2); | ||
| 250 | hisds *hi_sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count); | ||
| 251 | void hi_sdsfreesplitres(hisds *tokens, int count); | ||
| 252 | void hi_sdstolower(hisds s); | ||
| 253 | void hi_sdstoupper(hisds s); | ||
| 254 | hisds hi_sdsfromlonglong(long long value); | ||
| 255 | hisds hi_sdscatrepr(hisds s, const char *p, size_t len); | ||
| 256 | hisds *hi_sdssplitargs(const char *line, int *argc); | ||
| 257 | hisds hi_sdsmapchars(hisds s, const char *from, const char *to, size_t setlen); | ||
| 258 | hisds hi_sdsjoin(char **argv, int argc, char *sep); | ||
| 259 | hisds hi_sdsjoinsds(hisds *argv, int argc, const char *sep, size_t seplen); | ||
| 260 | |||
| 261 | /* Low level functions exposed to the user API */ | ||
| 262 | hisds hi_sdsMakeRoomFor(hisds s, size_t addlen); | ||
| 263 | void hi_sdsIncrLen(hisds s, int incr); | ||
| 264 | hisds hi_sdsRemoveFreeSpace(hisds s); | ||
| 265 | size_t hi_sdsAllocSize(hisds s); | ||
| 266 | void *hi_sdsAllocPtr(hisds s); | ||
| 267 | |||
| 268 | /* Export the allocator used by SDS to the program using SDS. | ||
| 269 | * Sometimes the program SDS is linked to, may use a different set of | ||
| 270 | * allocators, but may want to allocate or free things that SDS will | ||
| 271 | * respectively free or allocate. */ | ||
| 272 | void *hi_sds_malloc(size_t size); | ||
| 273 | void *hi_sds_realloc(void *ptr, size_t size); | ||
| 274 | void hi_sds_free(void *ptr); | ||
| 275 | |||
| 276 | #ifdef REDIS_TEST | ||
| 277 | int hi_sdsTest(int argc, char *argv[]); | ||
| 278 | #endif | ||
| 279 | |||
| 280 | #endif /* HIREDIS_SDS_H */ | ||
