feat: Include latests changes from sds upstream

This commit is contained in:
Jan-Erik Rediger 2016-04-20 15:27:38 +02:00
parent d76e13b6d5
commit 63e1cf0c63
2 changed files with 66 additions and 24 deletions

74
sds.c
View File

@ -1,6 +1,8 @@
/* SDSLib, A C dynamic strings library /* SDSLib 2.0 -- A C dynamic strings library
* *
* Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com> * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2015, Oran Agra
* Copyright (c) 2015, Redis Labs, Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -34,6 +36,7 @@
#include <ctype.h> #include <ctype.h>
#include <assert.h> #include <assert.h>
#include "sds.h" #include "sds.h"
#include "sdsalloc.h"
static inline int sdsHdrSize(char type) { static inline int sdsHdrSize(char type) {
switch(type&SDS_TYPE_MASK) { switch(type&SDS_TYPE_MASK) {
@ -85,7 +88,7 @@ sds sdsnewlen(const void *init, size_t initlen) {
int hdrlen = sdsHdrSize(type); int hdrlen = sdsHdrSize(type);
unsigned char *fp; /* flags pointer. */ unsigned char *fp; /* flags pointer. */
sh = malloc(hdrlen+initlen+1); sh = s_malloc(hdrlen+initlen+1);
if (!init) if (!init)
memset(sh, 0, hdrlen+initlen+1); memset(sh, 0, hdrlen+initlen+1);
if (sh == NULL) return NULL; if (sh == NULL) return NULL;
@ -151,7 +154,7 @@ sds sdsdup(const sds s) {
/* Free an sds string. No operation is performed if 's' is NULL. */ /* Free an sds string. No operation is performed if 's' is NULL. */
void sdsfree(sds s) { void sdsfree(sds s) {
if (s == NULL) return; if (s == NULL) return;
free((char*)s-sdsHdrSize(s[-1])); s_free((char*)s-sdsHdrSize(s[-1]));
} }
/* Set the sds string length to the length as obtained with strlen(), so /* Set the sds string length to the length as obtained with strlen(), so
@ -215,16 +218,16 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
hdrlen = sdsHdrSize(type); hdrlen = sdsHdrSize(type);
if (oldtype==type) { if (oldtype==type) {
newsh = realloc(sh, hdrlen+newlen+1); newsh = s_realloc(sh, hdrlen+newlen+1);
if (newsh == NULL) return NULL; if (newsh == NULL) return NULL;
s = (char*)newsh+hdrlen; s = (char*)newsh+hdrlen;
} else { } else {
/* Since the header size changes, need to move the string forward, /* Since the header size changes, need to move the string forward,
* and can't use realloc */ * and can't use realloc */
newsh = malloc(hdrlen+newlen+1); newsh = s_malloc(hdrlen+newlen+1);
if (newsh == NULL) return NULL; if (newsh == NULL) return NULL;
memcpy((char*)newsh+hdrlen, s, len+1); memcpy((char*)newsh+hdrlen, s, len+1);
free(sh); s_free(sh);
s = (char*)newsh+hdrlen; s = (char*)newsh+hdrlen;
s[-1] = type; s[-1] = type;
sdssetlen(s, len); sdssetlen(s, len);
@ -249,14 +252,14 @@ sds sdsRemoveFreeSpace(sds s) {
type = sdsReqType(len); type = sdsReqType(len);
hdrlen = sdsHdrSize(type); hdrlen = sdsHdrSize(type);
if (oldtype==type) { if (oldtype==type) {
newsh = realloc(sh, hdrlen+len+1); newsh = s_realloc(sh, hdrlen+len+1);
if (newsh == NULL) return NULL; if (newsh == NULL) return NULL;
s = (char*)newsh+hdrlen; s = (char*)newsh+hdrlen;
} else { } else {
newsh = malloc(hdrlen+len+1); newsh = s_malloc(hdrlen+len+1);
if (newsh == NULL) return NULL; if (newsh == NULL) return NULL;
memcpy((char*)newsh+hdrlen, s, len+1); memcpy((char*)newsh+hdrlen, s, len+1);
free(sh); s_free(sh);
s = (char*)newsh+hdrlen; s = (char*)newsh+hdrlen;
s[-1] = type; s[-1] = type;
sdssetlen(s, len); sdssetlen(s, len);
@ -277,6 +280,12 @@ size_t sdsAllocSize(sds s) {
return sdsHdrSize(s[-1])+alloc+1; return sdsHdrSize(s[-1])+alloc+1;
} }
/* Return the pointer of the actual SDS allocation (normally SDS strings
* are referenced by the start of the string buffer). */
void *sdsAllocPtr(sds s) {
return (void*) (s-sdsHdrSize(s[-1]));
}
/* Increment the sds length and decrements the left free space at the /* Increment the sds length and decrements the left free space at the
* end of the string according to 'incr'. Also set the null term * end of the string according to 'incr'. Also set the null term
* in the new end of the string. * in the new end of the string.
@ -497,7 +506,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
/* We try to start using a static buffer for speed. /* We try to start using a static buffer for speed.
* If not possible we revert to heap allocation. */ * If not possible we revert to heap allocation. */
if (buflen > sizeof(staticbuf)) { if (buflen > sizeof(staticbuf)) {
buf = malloc(buflen); buf = s_malloc(buflen);
if (buf == NULL) return NULL; if (buf == NULL) return NULL;
} else { } else {
buflen = sizeof(staticbuf); buflen = sizeof(staticbuf);
@ -511,9 +520,9 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
vsnprintf(buf, buflen, fmt, cpy); vsnprintf(buf, buflen, fmt, cpy);
va_end(cpy); va_end(cpy);
if (buf[buflen-2] != '\0') { if (buf[buflen-2] != '\0') {
if (buf != staticbuf) free(buf); if (buf != staticbuf) s_free(buf);
buflen *= 2; buflen *= 2;
buf = malloc(buflen); buf = s_malloc(buflen);
if (buf == NULL) return NULL; if (buf == NULL) return NULL;
continue; continue;
} }
@ -522,7 +531,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
/* Finally concat the obtained string to the SDS string and return it. */ /* Finally concat the obtained string to the SDS string and return it. */
t = sdscat(s, buf); t = sdscat(s, buf);
if (buf != staticbuf) free(buf); if (buf != staticbuf) s_free(buf);
return t; return t;
} }
@ -789,7 +798,7 @@ sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count
if (seplen < 1 || len < 0) return NULL; if (seplen < 1 || len < 0) return NULL;
tokens = malloc(sizeof(sds)*slots); tokens = s_malloc(sizeof(sds)*slots);
if (tokens == NULL) return NULL; if (tokens == NULL) return NULL;
if (len == 0) { if (len == 0) {
@ -802,7 +811,7 @@ sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count
sds *newtokens; sds *newtokens;
slots *= 2; slots *= 2;
newtokens = realloc(tokens,sizeof(sds)*slots); newtokens = s_realloc(tokens,sizeof(sds)*slots);
if (newtokens == NULL) goto cleanup; if (newtokens == NULL) goto cleanup;
tokens = newtokens; tokens = newtokens;
} }
@ -826,7 +835,7 @@ cleanup:
{ {
int i; int i;
for (i = 0; i < elements; i++) sdsfree(tokens[i]); for (i = 0; i < elements; i++) sdsfree(tokens[i]);
free(tokens); s_free(tokens);
*count = 0; *count = 0;
return NULL; return NULL;
} }
@ -837,7 +846,7 @@ void sdsfreesplitres(sds *tokens, int count) {
if (!tokens) return; if (!tokens) return;
while(count--) while(count--)
sdsfree(tokens[count]); sdsfree(tokens[count]);
free(tokens); s_free(tokens);
} }
/* Append to the sds string "s" an escaped string representation where /* Append to the sds string "s" an escaped string representation where
@ -1011,13 +1020,13 @@ sds *sdssplitargs(const char *line, int *argc) {
if (*p) p++; if (*p) p++;
} }
/* add the token to the vector */ /* add the token to the vector */
vector = realloc(vector,((*argc)+1)*sizeof(char*)); vector = s_realloc(vector,((*argc)+1)*sizeof(char*));
vector[*argc] = current; vector[*argc] = current;
(*argc)++; (*argc)++;
current = NULL; current = NULL;
} else { } else {
/* Even on empty input string return something not NULL. */ /* Even on empty input string return something not NULL. */
if (vector == NULL) vector = malloc(sizeof(void*)); if (vector == NULL) vector = s_malloc(sizeof(void*));
return vector; return vector;
} }
} }
@ -1025,7 +1034,7 @@ sds *sdssplitargs(const char *line, int *argc) {
err: err:
while((*argc)--) while((*argc)--)
sdsfree(vector[*argc]); sdsfree(vector[*argc]);
free(vector); s_free(vector);
if (current) sdsfree(current); if (current) sdsfree(current);
*argc = 0; *argc = 0;
return NULL; return NULL;
@ -1067,7 +1076,28 @@ sds sdsjoin(char **argv, int argc, char *sep) {
return join; return join;
} }
#if defined(REDIS_TEST) || defined(SDS_TEST_MAIN) /* Like sdsjoin, but joins an array of SDS strings. */
sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
sds join = sdsempty();
int j;
for (j = 0; j < argc; j++) {
join = sdscatsds(join, argv[j]);
if (j != argc-1) join = sdscatlen(join,sep,seplen);
}
return join;
}
/* Wrappers to the allocators used by SDS. Note that SDS will actually
* just use the macros defined into sdsalloc.h in order to avoid to pay
* the overhead of function calls. Here we define these wrappers only for
* the programs SDS is linked to, if they want to touch the SDS internals
* even if they use a different allocator. */
void *sds_malloc(size_t size) { return s_malloc(size); }
void *sds_realloc(void *ptr, size_t size) { return s_realloc(ptr,size); }
void sds_free(void *ptr) { s_free(ptr); }
#if defined(SDS_TEST_MAIN)
#include <stdio.h> #include <stdio.h>
#include "testhelp.h" #include "testhelp.h"
#include "limits.h" #include "limits.h"

16
sds.h
View File

@ -1,6 +1,8 @@
/* SDSLib, A C dynamic strings library /* SDSLib 2.0 -- A C dynamic strings library
* *
* Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com> * Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2015, Oran Agra
* Copyright (c) 2015, Redis Labs, Inc
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -247,12 +249,22 @@ sds sdscatrepr(sds s, const char *p, size_t len);
sds *sdssplitargs(const char *line, int *argc); sds *sdssplitargs(const char *line, int *argc);
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen); sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
sds sdsjoin(char **argv, int argc, char *sep); sds sdsjoin(char **argv, int argc, char *sep);
sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen);
/* Low level functions exposed to the user API */ /* Low level functions exposed to the user API */
sds sdsMakeRoomFor(sds s, size_t addlen); sds sdsMakeRoomFor(sds s, size_t addlen);
void sdsIncrLen(sds s, int incr); void sdsIncrLen(sds s, int incr);
sds sdsRemoveFreeSpace(sds s); sds sdsRemoveFreeSpace(sds s);
size_t sdsAllocSize(sds s); size_t sdsAllocSize(sds s);
void *sdsAllocPtr(sds s);
/* Export the allocator used by SDS to the program using SDS.
* Sometimes the program SDS is linked to, may use a different set of
* allocators, but may want to allocate or free things that SDS will
* respectively free or allocate. */
void *sds_malloc(size_t size);
void *sds_realloc(void *ptr, size_t size);
void sds_free(void *ptr);
#ifdef REDIS_TEST #ifdef REDIS_TEST
int sdsTest(int argc, char *argv[]); int sdsTest(int argc, char *argv[]);