| 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 __SDS_H | ||
| 34 | #define __SDS_H | ||
| 35 | |||
| 36 | #define SDS_MAX_PREALLOC (1024*1024) | ||
| 37 | #ifdef _MSC_VER | ||
| 38 | typedef intptr_t ssize_t; | ||
| 39 | #define SSIZE_MAX INTPTR_MAX | ||
| 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 | #ifdef __cplusplus | ||
| 50 | extern "C" { | ||
| 51 | #endif | ||
| 52 | |||
| 53 | typedef char *sds; | ||
| 54 | |||
| 55 | /* Note: sdshdr5 is never used, we just access the flags byte directly. | ||
| 56 | * However is here to document the layout of type 5 SDS strings. */ | ||
| 57 | struct __attribute__ ((__packed__)) sdshdr5 { | ||
| 58 | unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ | ||
| 59 | char buf[]; | ||
| 60 | }; | ||
| 61 | struct __attribute__ ((__packed__)) sdshdr8 { | ||
| 62 | uint8_t len; /* used */ | ||
| 63 | uint8_t alloc; /* excluding the header and null terminator */ | ||
| 64 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 65 | char buf[]; | ||
| 66 | }; | ||
| 67 | struct __attribute__ ((__packed__)) sdshdr16 { | ||
| 68 | uint16_t len; /* used */ | ||
| 69 | uint16_t alloc; /* excluding the header and null terminator */ | ||
| 70 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 71 | char buf[]; | ||
| 72 | }; | ||
| 73 | struct __attribute__ ((__packed__)) sdshdr32 { | ||
| 74 | uint32_t len; /* used */ | ||
| 75 | uint32_t alloc; /* excluding the header and null terminator */ | ||
| 76 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 77 | char buf[]; | ||
| 78 | }; | ||
| 79 | struct __attribute__ ((__packed__)) sdshdr64 { | ||
| 80 | uint64_t len; /* used */ | ||
| 81 | uint64_t alloc; /* excluding the header and null terminator */ | ||
| 82 | unsigned char flags; /* 3 lsb of type, 5 unused bits */ | ||
| 83 | char buf[]; | ||
| 84 | }; | ||
| 85 | |||
| 86 | #define SDS_TYPE_5 0 | ||
| 87 | #define SDS_TYPE_8 1 | ||
| 88 | #define SDS_TYPE_16 2 | ||
| 89 | #define SDS_TYPE_32 3 | ||
| 90 | #define SDS_TYPE_64 4 | ||
| 91 | #define SDS_TYPE_MASK 7 | ||
| 92 | #define SDS_TYPE_BITS 3 | ||
| 93 | #define SDS_HDR_VAR(T,s) struct sdshdr##T *sh = (struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T))); | ||
| 94 | #define SDS_HDR(T,s) ((struct sdshdr##T *)((s)-(sizeof(struct sdshdr##T)))) | ||
| 95 | #define SDS_TYPE_5_LEN(f) ((f)>>SDS_TYPE_BITS) | ||
| 96 | |||
| 97 | 59617 | static inline size_t sdslen(const sds s) { | |
| 98 | 59617 | unsigned char flags = s[-1]; | |
| 99 | 59617 | switch(flags&SDS_TYPE_MASK) { | |
| 100 | 1 | case SDS_TYPE_5: | |
| 101 | 1 | return SDS_TYPE_5_LEN(flags); | |
| 102 | 59527 | case SDS_TYPE_8: | |
| 103 | 59527 | return SDS_HDR(8,s)->len; | |
| 104 | 0 | case SDS_TYPE_16: | |
| 105 | 0 | return SDS_HDR(16,s)->len; | |
| 106 | 89 | case SDS_TYPE_32: | |
| 107 | 89 | return SDS_HDR(32,s)->len; | |
| 108 | 0 | case SDS_TYPE_64: | |
| 109 | 0 | return SDS_HDR(64,s)->len; | |
| 110 | } | ||
| 111 | 0 | return 0; | |
| 112 | } | ||
| 113 | |||
| 114 | static inline size_t sdsavail(const sds s) { | ||
| 115 | unsigned char flags = s[-1]; | ||
| 116 | switch(flags&SDS_TYPE_MASK) { | ||
| 117 | case SDS_TYPE_5: { | ||
| 118 | return 0; | ||
| 119 | } | ||
| 120 | case SDS_TYPE_8: { | ||
| 121 | SDS_HDR_VAR(8,s); | ||
| 122 | return sh->alloc - sh->len; | ||
| 123 | } | ||
| 124 | case SDS_TYPE_16: { | ||
| 125 | SDS_HDR_VAR(16,s); | ||
| 126 | return sh->alloc - sh->len; | ||
| 127 | } | ||
| 128 | case SDS_TYPE_32: { | ||
| 129 | SDS_HDR_VAR(32,s); | ||
| 130 | return sh->alloc - sh->len; | ||
| 131 | } | ||
| 132 | case SDS_TYPE_64: { | ||
| 133 | SDS_HDR_VAR(64,s); | ||
| 134 | return sh->alloc - sh->len; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | |||
| 140 | static inline void sdssetlen(sds s, size_t newlen) { | ||
| 141 | unsigned char flags = s[-1]; | ||
| 142 | switch(flags&SDS_TYPE_MASK) { | ||
| 143 | case SDS_TYPE_5: | ||
| 144 | { | ||
| 145 | unsigned char *fp = ((unsigned char*)s)-1; | ||
| 146 | *fp = (unsigned char)(SDS_TYPE_5 | (newlen << SDS_TYPE_BITS)); | ||
| 147 | } | ||
| 148 | break; | ||
| 149 | case SDS_TYPE_8: | ||
| 150 | SDS_HDR(8,s)->len = (uint8_t)newlen; | ||
| 151 | break; | ||
| 152 | case SDS_TYPE_16: | ||
| 153 | SDS_HDR(16,s)->len = (uint16_t)newlen; | ||
| 154 | break; | ||
| 155 | case SDS_TYPE_32: | ||
| 156 | SDS_HDR(32,s)->len = (uint32_t)newlen; | ||
| 157 | break; | ||
| 158 | case SDS_TYPE_64: | ||
| 159 | SDS_HDR(64,s)->len = (uint64_t)newlen; | ||
| 160 | break; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | static inline void sdsinclen(sds s, size_t inc) { | ||
| 165 | unsigned char flags = s[-1]; | ||
| 166 | switch(flags&SDS_TYPE_MASK) { | ||
| 167 | case SDS_TYPE_5: | ||
| 168 | { | ||
| 169 | unsigned char *fp = ((unsigned char*)s)-1; | ||
| 170 | unsigned char newlen = SDS_TYPE_5_LEN(flags)+(unsigned char)inc; | ||
| 171 | *fp = SDS_TYPE_5 | (newlen << SDS_TYPE_BITS); | ||
| 172 | } | ||
| 173 | break; | ||
| 174 | case SDS_TYPE_8: | ||
| 175 | SDS_HDR(8,s)->len += (uint8_t)inc; | ||
| 176 | break; | ||
| 177 | case SDS_TYPE_16: | ||
| 178 | SDS_HDR(16,s)->len += (uint16_t)inc; | ||
| 179 | break; | ||
| 180 | case SDS_TYPE_32: | ||
| 181 | SDS_HDR(32,s)->len += (uint32_t)inc; | ||
| 182 | break; | ||
| 183 | case SDS_TYPE_64: | ||
| 184 | SDS_HDR(64,s)->len += (uint64_t)inc; | ||
| 185 | break; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | /* sdsalloc() = sdsavail() + sdslen() */ | ||
| 190 | static inline size_t sdsalloc(const sds s) { | ||
| 191 | unsigned char flags = s[-1]; | ||
| 192 | switch(flags&SDS_TYPE_MASK) { | ||
| 193 | case SDS_TYPE_5: | ||
| 194 | return SDS_TYPE_5_LEN(flags); | ||
| 195 | case SDS_TYPE_8: | ||
| 196 | return SDS_HDR(8,s)->alloc; | ||
| 197 | case SDS_TYPE_16: | ||
| 198 | return SDS_HDR(16,s)->alloc; | ||
| 199 | case SDS_TYPE_32: | ||
| 200 | return SDS_HDR(32,s)->alloc; | ||
| 201 | case SDS_TYPE_64: | ||
| 202 | return SDS_HDR(64,s)->alloc; | ||
| 203 | } | ||
| 204 | return 0; | ||
| 205 | } | ||
| 206 | |||
| 207 | static inline void sdssetalloc(sds s, size_t newlen) { | ||
| 208 | unsigned char flags = s[-1]; | ||
| 209 | switch(flags&SDS_TYPE_MASK) { | ||
| 210 | case SDS_TYPE_5: | ||
| 211 | /* Nothing to do, this type has no total allocation info. */ | ||
| 212 | break; | ||
| 213 | case SDS_TYPE_8: | ||
| 214 | SDS_HDR(8,s)->alloc = (uint8_t)newlen; | ||
| 215 | break; | ||
| 216 | case SDS_TYPE_16: | ||
| 217 | SDS_HDR(16,s)->alloc = (uint16_t)newlen; | ||
| 218 | break; | ||
| 219 | case SDS_TYPE_32: | ||
| 220 | SDS_HDR(32,s)->alloc = (uint32_t)newlen; | ||
| 221 | break; | ||
| 222 | case SDS_TYPE_64: | ||
| 223 | SDS_HDR(64,s)->alloc = (uint64_t)newlen; | ||
| 224 | break; | ||
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | sds sdsnewlen(const void *init, size_t initlen); | ||
| 229 | sds sdsnew(const char *init); | ||
| 230 | sds sdsempty(void); | ||
| 231 | sds sdsdup(const sds s); | ||
| 232 | void sdsfree(sds s); | ||
| 233 | sds sdsgrowzero(sds s, size_t len); | ||
| 234 | sds sdscatlen(sds s, const void *t, size_t len); | ||
| 235 | sds sdscat(sds s, const char *t); | ||
| 236 | sds sdscatsds(sds s, const sds t); | ||
| 237 | sds sdscpylen(sds s, const char *t, size_t len); | ||
| 238 | sds sdscpy(sds s, const char *t); | ||
| 239 | |||
| 240 | sds sdscatvprintf(sds s, const char *fmt, va_list ap); | ||
| 241 | #ifdef __GNUC__ | ||
| 242 | sds sdscatprintf(sds s, const char *fmt, ...) | ||
| 243 | __attribute__((format(printf, 2, 3))); | ||
| 244 | #else | ||
| 245 | sds sdscatprintf(sds s, const char *fmt, ...); | ||
| 246 | #endif | ||
| 247 | |||
| 248 | sds sdscatfmt(sds s, char const *fmt, ...); | ||
| 249 | sds sdstrim(sds s, const char *cset); | ||
| 250 | int sdsrange(sds s, ssize_t start, ssize_t end); | ||
| 251 | void sdsupdatelen(sds s); | ||
| 252 | void sdsclear(sds s); | ||
| 253 | int sdscmp(const sds s1, const sds s2); | ||
| 254 | sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count); | ||
| 255 | void sdsfreesplitres(sds *tokens, int count); | ||
| 256 | void sdstolower(sds s); | ||
| 257 | void sdstoupper(sds s); | ||
| 258 | sds sdsfromlonglong(long long value); | ||
| 259 | sds sdscatrepr(sds s, const char *p, size_t len); | ||
| 260 | sds *sdssplitargs(const char *line, int *argc); | ||
| 261 | sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); | ||
| 262 | sds sdsjoin(char **argv, int argc, char *sep); | ||
| 263 | sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen); | ||
| 264 | |||
| 265 | /* Low level functions exposed to the user API */ | ||
| 266 | sds sdsMakeRoomFor(sds s, size_t addlen); | ||
| 267 | void sdsIncrLen(sds s, int incr); | ||
| 268 | sds sdsRemoveFreeSpace(sds s); | ||
| 269 | size_t sdsAllocSize(sds s); | ||
| 270 | void *sdsAllocPtr(sds s); | ||
| 271 | |||
| 272 | /* Export the allocator used by SDS to the program using SDS. | ||
| 273 | * Sometimes the program SDS is linked to, may use a different set of | ||
| 274 | * allocators, but may want to allocate or free things that SDS will | ||
| 275 | * respectively free or allocate. */ | ||
| 276 | void *sds_malloc(size_t size); | ||
| 277 | void *sds_realloc(void *ptr, size_t size); | ||
| 278 | void sds_free(void *ptr); | ||
| 279 | |||
| 280 | #ifdef REDIS_TEST | ||
| 281 | int sdsTest(int argc, char *argv[]); | ||
| 282 | #endif | ||
| 283 | |||
| 284 | #ifdef __cplusplus | ||
| 285 | } | ||
| 286 | #endif | ||
| 287 | #endif |