Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
cb2782c3
Commit
cb2782c3
authored
Jul 25, 2015
by
antirez
Browse files
SDS: changes to unify Redis SDS with antirez/sds repo.
parent
9894495c
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/networking.c
View file @
cb2782c3
...
@@ -33,6 +33,13 @@
...
@@ -33,6 +33,13 @@
static
void
setProtocolError
(
redisClient
*
c
,
int
pos
);
static
void
setProtocolError
(
redisClient
*
c
,
int
pos
);
/* Return the size consumed from the allocator, for the specified SDS string,
* including internal fragmentation. This function is used in order to compute
* the client output buffer size. */
size_t
sdsZmallocSize
(
sds
s
)
{
void
*
sh
=
sdsAllocPtr
(
s
);
return
zmalloc_size
(
sh
);
}
/* Return the amount of memory used by the sds string at object->ptr
/* Return the amount of memory used by the sds string at object->ptr
* for a string object. */
* for a string object. */
...
...
src/sds.c
View file @
cb2782c3
...
@@ -35,7 +35,7 @@
...
@@ -35,7 +35,7 @@
#include <ctype.h>
#include <ctype.h>
#include <assert.h>
#include <assert.h>
#include "sds.h"
#include "sds.h"
#include "
zm
alloc.h"
#include "
sds
alloc.h"
static
inline
int
sdsHdrSize
(
char
type
)
{
static
inline
int
sdsHdrSize
(
char
type
)
{
switch
(
type
&
SDS_TYPE_MASK
)
{
switch
(
type
&
SDS_TYPE_MASK
)
{
...
@@ -87,7 +87,7 @@ sds sdsnewlen(const void *init, size_t initlen) {
...
@@ -87,7 +87,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
=
z
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
;
...
@@ -153,7 +153,7 @@ sds sdsdup(const sds s) {
...
@@ -153,7 +153,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
;
z
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
...
@@ -217,16 +217,16 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
...
@@ -217,16 +217,16 @@ sds sdsMakeRoomFor(sds s, size_t addlen) {
hdrlen
=
sdsHdrSize
(
type
);
hdrlen
=
sdsHdrSize
(
type
);
if
(
oldtype
==
type
)
{
if
(
oldtype
==
type
)
{
newsh
=
z
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
=
z
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
);
z
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
);
...
@@ -251,14 +251,14 @@ sds sdsRemoveFreeSpace(sds s) {
...
@@ -251,14 +251,14 @@ sds sdsRemoveFreeSpace(sds s) {
type
=
sdsReqType
(
len
);
type
=
sdsReqType
(
len
);
hdrlen
=
sdsHdrSize
(
type
);
hdrlen
=
sdsHdrSize
(
type
);
if
(
oldtype
==
type
)
{
if
(
oldtype
==
type
)
{
newsh
=
z
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
=
z
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
);
z
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
);
...
@@ -279,11 +279,10 @@ size_t sdsAllocSize(sds s) {
...
@@ -279,11 +279,10 @@ size_t sdsAllocSize(sds s) {
return
sdsHdrSize
(
s
[
-
1
])
+
alloc
+
1
;
return
sdsHdrSize
(
s
[
-
1
])
+
alloc
+
1
;
}
}
/* Return the size consumed from the allocator,
/* Return the pointer of the actual SDS allocation (normally SDS strings
* including internal fragmentation */
* are referenced by the start of the string buffer). */
size_t
sdsZmallocSize
(
sds
s
)
{
void
*
sdsAllocPtr
(
sds
s
)
{
struct
sdshdr
*
sh
=
(
void
*
)
(
s
-
sdsHdrSize
(
s
[
-
1
]));
return
(
void
*
)
(
s
-
sdsHdrSize
(
s
[
-
1
]));
return
zmalloc_size
(
sh
);
}
}
/* Increment the sds length and decrements the left free space at the
/* Increment the sds length and decrements the left free space at the
...
@@ -506,7 +505,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
...
@@ -506,7 +505,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
=
z
malloc
(
buflen
);
buf
=
s_
malloc
(
buflen
);
if
(
buf
==
NULL
)
return
NULL
;
if
(
buf
==
NULL
)
return
NULL
;
}
else
{
}
else
{
buflen
=
sizeof
(
staticbuf
);
buflen
=
sizeof
(
staticbuf
);
...
@@ -520,9 +519,9 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
...
@@ -520,9 +519,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
)
z
free
(
buf
);
if
(
buf
!=
staticbuf
)
s_
free
(
buf
);
buflen
*=
2
;
buflen
*=
2
;
buf
=
z
malloc
(
buflen
);
buf
=
s_
malloc
(
buflen
);
if
(
buf
==
NULL
)
return
NULL
;
if
(
buf
==
NULL
)
return
NULL
;
continue
;
continue
;
}
}
...
@@ -531,7 +530,7 @@ sds sdscatvprintf(sds s, const char *fmt, va_list ap) {
...
@@ -531,7 +530,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
)
z
free
(
buf
);
if
(
buf
!=
staticbuf
)
s_
free
(
buf
);
return
t
;
return
t
;
}
}
...
@@ -798,7 +797,7 @@ sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count
...
@@ -798,7 +797,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
=
z
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
)
{
...
@@ -811,7 +810,7 @@ sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count
...
@@ -811,7 +810,7 @@ sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count
sds
*
newtokens
;
sds
*
newtokens
;
slots
*=
2
;
slots
*=
2
;
newtokens
=
z
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
;
}
}
...
@@ -835,7 +834,7 @@ cleanup:
...
@@ -835,7 +834,7 @@ cleanup:
{
{
int
i
;
int
i
;
for
(
i
=
0
;
i
<
elements
;
i
++
)
sdsfree
(
tokens
[
i
]);
for
(
i
=
0
;
i
<
elements
;
i
++
)
sdsfree
(
tokens
[
i
]);
z
free
(
tokens
);
s_
free
(
tokens
);
*
count
=
0
;
*
count
=
0
;
return
NULL
;
return
NULL
;
}
}
...
@@ -846,7 +845,7 @@ void sdsfreesplitres(sds *tokens, int count) {
...
@@ -846,7 +845,7 @@ void sdsfreesplitres(sds *tokens, int count) {
if
(
!
tokens
)
return
;
if
(
!
tokens
)
return
;
while
(
count
--
)
while
(
count
--
)
sdsfree
(
tokens
[
count
]);
sdsfree
(
tokens
[
count
]);
z
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
...
@@ -1020,13 +1019,13 @@ sds *sdssplitargs(const char *line, int *argc) {
...
@@ -1020,13 +1019,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
=
z
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
=
z
malloc
(
sizeof
(
void
*
));
if
(
vector
==
NULL
)
vector
=
s_
malloc
(
sizeof
(
void
*
));
return
vector
;
return
vector
;
}
}
}
}
...
@@ -1034,7 +1033,7 @@ sds *sdssplitargs(const char *line, int *argc) {
...
@@ -1034,7 +1033,7 @@ sds *sdssplitargs(const char *line, int *argc) {
err:
err:
while
((
*
argc
)
--
)
while
((
*
argc
)
--
)
sdsfree
(
vector
[
*
argc
]);
sdsfree
(
vector
[
*
argc
]);
z
free
(
vector
);
s_
free
(
vector
);
if
(
current
)
sdsfree
(
current
);
if
(
current
)
sdsfree
(
current
);
*
argc
=
0
;
*
argc
=
0
;
return
NULL
;
return
NULL
;
...
@@ -1088,7 +1087,7 @@ sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
...
@@ -1088,7 +1087,7 @@ sds sdsjoinsds(sds *argv, int argc, const char *sep, size_t seplen) {
return
join
;
return
join
;
}
}
#if
defined(REDIS_TEST) ||
defined(SDS_TEST_MAIN)
#if defined(SDS_TEST_MAIN)
#include <stdio.h>
#include <stdio.h>
#include "testhelp.h"
#include "testhelp.h"
#include "limits.h"
#include "limits.h"
...
...
src/sds.h
View file @
cb2782c3
...
@@ -255,7 +255,7 @@ sds sdsMakeRoomFor(sds s, size_t addlen);
...
@@ -255,7 +255,7 @@ 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
);
size_t
sdsZmallocSize
(
sds
s
);
void
*
sdsAllocPtr
(
sds
s
);
#ifdef REDIS_TEST
#ifdef REDIS_TEST
int
sdsTest
(
int
argc
,
char
*
argv
[]);
int
sdsTest
(
int
argc
,
char
*
argv
[]);
...
...
src/sdsalloc.h
0 → 100644
View file @
cb2782c3
/* SDSLib 2.0 -- A C dynamic strings library
*
* Copyright (c) 2006-2015, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2015, Redis Labs, Inc
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* SDS allocator selection.
*
* This file is used in order to change the SDS allocator at compile time.
* Just define the following defines to what you want to use. Also add
* the include of your alternate allocator if needed (not needed in order
* to use the default libc allocator). */
#include "zmalloc.h"
#define s_malloc zmalloc
#define s_realloc zrealloc
#define s_free zfree
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment