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