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