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 | |
48 | typedef 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. */ |
52 | struct __attribute__ ((__packed__)) sdshdr5 { |
53 | unsigned char flags; /* 3 lsb of type, and 5 msb of string length */ |
54 | char buf[]; |
55 | }; |
56 | struct __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 | }; |
62 | struct __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 | }; |
68 | struct __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 | }; |
74 | struct __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 | |
92 | static 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 | |
109 | static 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 | |
135 | static 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 | |
159 | static 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() */ |
185 | static 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 | |
202 | static 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 | |
223 | sds sdsnewlen(const void *init, size_t initlen); |
224 | sds sdsnew(const char *init); |
225 | sds sdsempty(void); |
226 | sds sdsdup(const sds s); |
227 | void sdsfree(sds s); |
228 | sds sdsgrowzero(sds s, size_t len); |
229 | sds sdscatlen(sds s, const void *t, size_t len); |
230 | sds sdscat(sds s, const char *t); |
231 | sds sdscatsds(sds s, const sds t); |
232 | sds sdscpylen(sds s, const char *t, size_t len); |
233 | sds sdscpy(sds s, const char *t); |
234 | |
235 | sds sdscatvprintf(sds s, const char *fmt, va_list ap); |
236 | #ifdef __GNUC__ |
237 | sds sdscatprintf(sds s, const char *fmt, ...) |
238 | __attribute__((format(printf, 2, 3))); |
239 | #else |
240 | sds sdscatprintf(sds s, const char *fmt, ...); |
241 | #endif |
242 | |
243 | sds sdscatfmt(sds s, char const *fmt, ...); |
244 | sds sdstrim(sds s, const char *cset); |
245 | void sdsrange(sds s, ssize_t start, ssize_t end); |
246 | void sdsupdatelen(sds s); |
247 | void sdsclear(sds s); |
248 | int sdscmp(const sds s1, const sds s2); |
249 | sds *sdssplitlen(const char *s, ssize_t len, const char *sep, int seplen, int *count); |
250 | void sdsfreesplitres(sds *tokens, int count); |
251 | void sdstolower(sds s); |
252 | void sdstoupper(sds s); |
253 | sds sdsfromlonglong(long long value); |
254 | sds sdscatrepr(sds s, const char *p, size_t len); |
255 | sds *sdssplitargs(const char *line, int *argc); |
256 | sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); |
257 | sds sdsjoin(char **argv, int argc, char *sep); |
258 | sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen); |
259 | |
260 | /* Low level functions exposed to the user API */ |
261 | sds sdsMakeRoomFor(sds s, size_t addlen); |
262 | void sdsIncrLen(sds s, ssize_t incr); |
263 | sds sdsRemoveFreeSpace(sds s); |
264 | size_t sdsAllocSize(sds s); |
265 | void *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. */ |
271 | void *sds_malloc(size_t size); |
272 | void *sds_realloc(void *ptr, size_t size); |
273 | void sds_free(void *ptr); |
274 | |
275 | #ifdef REDIS_TEST |
276 | int sdsTest(int argc, char *argv[]); |
277 | #endif |
278 | |
279 | #endif |
280 | |