Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
hiredis
Commits
4e8c8e74
Commit
4e8c8e74
authored
Dec 31, 2010
by
Pieter Noordhuis
Browse files
Replace zmalloc with regular malloc
parent
5703dfc3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Makefile
View file @
4e8c8e74
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
# Copyright (C) 2010 Salvatore Sanfilippo <antirez at gmail dot com>
# Copyright (C) 2010 Salvatore Sanfilippo <antirez at gmail dot com>
# This file is released under the BSD license, see the COPYING file
# This file is released under the BSD license, see the COPYING file
OBJ
=
net.o hiredis.o sds.o async.o
OBJ
=
net.o hiredis.o sds.o async.o
dict.o
BINS
=
hiredis-example hiredis-test
BINS
=
hiredis-example hiredis-test
uname_S
:=
$(
shell
sh
-c
'uname -s 2>/dev/null || echo not'
)
uname_S
:=
$(
shell
sh
-c
'uname -s 2>/dev/null || echo not'
)
...
@@ -49,6 +49,7 @@ async.o: async.c async.h hiredis.h sds.h util.h
...
@@ -49,6 +49,7 @@ async.o: async.c async.h hiredis.h sds.h util.h
example.o
:
example.c hiredis.h
example.o
:
example.c hiredis.h
hiredis.o
:
hiredis.c hiredis.h net.h sds.h util.h
hiredis.o
:
hiredis.c hiredis.h net.h sds.h util.h
sds.o
:
sds.c sds.h
sds.o
:
sds.c sds.h
dict.o
:
dict.c dict.h
test.o
:
test.c hiredis.h
test.o
:
test.c hiredis.h
${DYLIBNAME}
:
${OBJ}
${DYLIBNAME}
:
${OBJ}
...
...
dict.c
View file @
4e8c8e74
...
@@ -43,34 +43,6 @@
...
@@ -43,34 +43,6 @@
#include <limits.h>
#include <limits.h>
#include "dict.h"
#include "dict.h"
#include "zmalloc.h"
/* ---------------------------- Utility funcitons --------------------------- */
static
void
_dictPanic
(
const
char
*
fmt
,
...)
{
va_list
ap
;
va_start
(
ap
,
fmt
);
fprintf
(
stderr
,
"
\n
DICT LIBRARY PANIC: "
);
vfprintf
(
stderr
,
fmt
,
ap
);
fprintf
(
stderr
,
"
\n\n
"
);
va_end
(
ap
);
}
/* ------------------------- Heap Management Wrappers------------------------ */
static
void
*
_dictAlloc
(
size_t
size
)
{
void
*
p
=
zmalloc
(
size
);
if
(
p
==
NULL
)
_dictPanic
(
"Out of memory"
);
return
p
;
}
static
void
_dictFree
(
void
*
ptr
)
{
zfree
(
ptr
);
}
/* -------------------------- private prototypes ---------------------------- */
/* -------------------------- private prototypes ---------------------------- */
...
@@ -125,7 +97,7 @@ static void _dictReset(dict *ht)
...
@@ -125,7 +97,7 @@ static void _dictReset(dict *ht)
dict
*
dictCreate
(
dictType
*
type
,
dict
*
dictCreate
(
dictType
*
type
,
void
*
privDataPtr
)
void
*
privDataPtr
)
{
{
dict
*
ht
=
_dictA
lloc
(
sizeof
(
*
ht
));
dict
*
ht
=
ma
lloc
(
sizeof
(
*
ht
));
_dictInit
(
ht
,
type
,
privDataPtr
);
_dictInit
(
ht
,
type
,
privDataPtr
);
return
ht
;
return
ht
;
...
@@ -166,10 +138,7 @@ int dictExpand(dict *ht, unsigned long size)
...
@@ -166,10 +138,7 @@ int dictExpand(dict *ht, unsigned long size)
_dictInit
(
&
n
,
ht
->
type
,
ht
->
privdata
);
_dictInit
(
&
n
,
ht
->
type
,
ht
->
privdata
);
n
.
size
=
realsize
;
n
.
size
=
realsize
;
n
.
sizemask
=
realsize
-
1
;
n
.
sizemask
=
realsize
-
1
;
n
.
table
=
_dictAlloc
(
realsize
*
sizeof
(
dictEntry
*
));
n
.
table
=
calloc
(
realsize
,
sizeof
(
dictEntry
*
));
/* Initialize all the pointers to NULL */
memset
(
n
.
table
,
0
,
realsize
*
sizeof
(
dictEntry
*
));
/* Copy all the elements from the old to the new table:
/* Copy all the elements from the old to the new table:
* note that if the old hash table is empty ht->size is zero,
* note that if the old hash table is empty ht->size is zero,
...
@@ -179,7 +148,7 @@ int dictExpand(dict *ht, unsigned long size)
...
@@ -179,7 +148,7 @@ int dictExpand(dict *ht, unsigned long size)
dictEntry
*
he
,
*
nextHe
;
dictEntry
*
he
,
*
nextHe
;
if
(
ht
->
table
[
i
]
==
NULL
)
continue
;
if
(
ht
->
table
[
i
]
==
NULL
)
continue
;
/* For each hash entry on this slot... */
/* For each hash entry on this slot... */
he
=
ht
->
table
[
i
];
he
=
ht
->
table
[
i
];
while
(
he
)
{
while
(
he
)
{
...
@@ -196,7 +165,7 @@ int dictExpand(dict *ht, unsigned long size)
...
@@ -196,7 +165,7 @@ int dictExpand(dict *ht, unsigned long size)
}
}
}
}
assert
(
ht
->
used
==
0
);
assert
(
ht
->
used
==
0
);
_dictF
ree
(
ht
->
table
);
f
ree
(
ht
->
table
);
/* Remap the new hashtable in the old */
/* Remap the new hashtable in the old */
*
ht
=
n
;
*
ht
=
n
;
...
@@ -215,7 +184,7 @@ int dictAdd(dict *ht, void *key, void *val)
...
@@ -215,7 +184,7 @@ int dictAdd(dict *ht, void *key, void *val)
return
DICT_ERR
;
return
DICT_ERR
;
/* Allocates the memory and stores key */
/* Allocates the memory and stores key */
entry
=
_dictA
lloc
(
sizeof
(
*
entry
));
entry
=
ma
lloc
(
sizeof
(
*
entry
));
entry
->
next
=
ht
->
table
[
index
];
entry
->
next
=
ht
->
table
[
index
];
ht
->
table
[
index
]
=
entry
;
ht
->
table
[
index
]
=
entry
;
...
@@ -275,7 +244,7 @@ static int dictGenericDelete(dict *ht, const void *key, int nofree)
...
@@ -275,7 +244,7 @@ static int dictGenericDelete(dict *ht, const void *key, int nofree)
dictFreeEntryKey
(
ht
,
he
);
dictFreeEntryKey
(
ht
,
he
);
dictFreeEntryVal
(
ht
,
he
);
dictFreeEntryVal
(
ht
,
he
);
}
}
_dictF
ree
(
he
);
f
ree
(
he
);
ht
->
used
--
;
ht
->
used
--
;
return
DICT_OK
;
return
DICT_OK
;
}
}
...
@@ -307,13 +276,13 @@ int _dictClear(dict *ht)
...
@@ -307,13 +276,13 @@ int _dictClear(dict *ht)
nextHe
=
he
->
next
;
nextHe
=
he
->
next
;
dictFreeEntryKey
(
ht
,
he
);
dictFreeEntryKey
(
ht
,
he
);
dictFreeEntryVal
(
ht
,
he
);
dictFreeEntryVal
(
ht
,
he
);
_dictF
ree
(
he
);
f
ree
(
he
);
ht
->
used
--
;
ht
->
used
--
;
he
=
nextHe
;
he
=
nextHe
;
}
}
}
}
/* Free the table and the allocated cache structure */
/* Free the table and the allocated cache structure */
_dictF
ree
(
ht
->
table
);
f
ree
(
ht
->
table
);
/* Re-initialize the table */
/* Re-initialize the table */
_dictReset
(
ht
);
_dictReset
(
ht
);
return
DICT_OK
;
/* never fails */
return
DICT_OK
;
/* never fails */
...
@@ -323,7 +292,7 @@ int _dictClear(dict *ht)
...
@@ -323,7 +292,7 @@ int _dictClear(dict *ht)
void
dictRelease
(
dict
*
ht
)
void
dictRelease
(
dict
*
ht
)
{
{
_dictClear
(
ht
);
_dictClear
(
ht
);
_dictF
ree
(
ht
);
f
ree
(
ht
);
}
}
dictEntry
*
dictFind
(
dict
*
ht
,
const
void
*
key
)
dictEntry
*
dictFind
(
dict
*
ht
,
const
void
*
key
)
...
@@ -344,7 +313,7 @@ dictEntry *dictFind(dict *ht, const void *key)
...
@@ -344,7 +313,7 @@ dictEntry *dictFind(dict *ht, const void *key)
dictIterator
*
dictGetIterator
(
dict
*
ht
)
dictIterator
*
dictGetIterator
(
dict
*
ht
)
{
{
dictIterator
*
iter
=
_dictA
lloc
(
sizeof
(
*
iter
));
dictIterator
*
iter
=
ma
lloc
(
sizeof
(
*
iter
));
iter
->
ht
=
ht
;
iter
->
ht
=
ht
;
iter
->
index
=
-
1
;
iter
->
index
=
-
1
;
...
@@ -376,7 +345,7 @@ dictEntry *dictNext(dictIterator *iter)
...
@@ -376,7 +345,7 @@ dictEntry *dictNext(dictIterator *iter)
void
dictReleaseIterator
(
dictIterator
*
iter
)
void
dictReleaseIterator
(
dictIterator
*
iter
)
{
{
_dictF
ree
(
iter
);
f
ree
(
iter
);
}
}
/* Return a random entry from the hash table. Useful to
/* Return a random entry from the hash table. Useful to
...
@@ -517,7 +486,7 @@ static unsigned int _dictStringCopyHTHashFunction(const void *key)
...
@@ -517,7 +486,7 @@ static unsigned int _dictStringCopyHTHashFunction(const void *key)
static
void
*
_dictStringCopyHTKeyDup
(
void
*
privdata
,
const
void
*
key
)
static
void
*
_dictStringCopyHTKeyDup
(
void
*
privdata
,
const
void
*
key
)
{
{
int
len
=
strlen
(
key
);
int
len
=
strlen
(
key
);
char
*
copy
=
_dictA
lloc
(
len
+
1
);
char
*
copy
=
ma
lloc
(
len
+
1
);
DICT_NOTUSED
(
privdata
);
DICT_NOTUSED
(
privdata
);
memcpy
(
copy
,
key
,
len
);
memcpy
(
copy
,
key
,
len
);
...
@@ -528,7 +497,7 @@ static void *_dictStringCopyHTKeyDup(void *privdata, const void *key)
...
@@ -528,7 +497,7 @@ static void *_dictStringCopyHTKeyDup(void *privdata, const void *key)
static
void
*
_dictStringKeyValCopyHTValDup
(
void
*
privdata
,
const
void
*
val
)
static
void
*
_dictStringKeyValCopyHTValDup
(
void
*
privdata
,
const
void
*
val
)
{
{
int
len
=
strlen
(
val
);
int
len
=
strlen
(
val
);
char
*
copy
=
_dictA
lloc
(
len
+
1
);
char
*
copy
=
ma
lloc
(
len
+
1
);
DICT_NOTUSED
(
privdata
);
DICT_NOTUSED
(
privdata
);
memcpy
(
copy
,
val
,
len
);
memcpy
(
copy
,
val
,
len
);
...
@@ -548,14 +517,14 @@ static void _dictStringCopyHTKeyDestructor(void *privdata, void *key)
...
@@ -548,14 +517,14 @@ static void _dictStringCopyHTKeyDestructor(void *privdata, void *key)
{
{
DICT_NOTUSED
(
privdata
);
DICT_NOTUSED
(
privdata
);
_dictF
ree
((
void
*
)
key
);
/* ATTENTION: const cast */
f
ree
((
void
*
)
key
);
/* ATTENTION: const cast */
}
}
static
void
_dictStringKeyValCopyHTValDestructor
(
void
*
privdata
,
void
*
val
)
static
void
_dictStringKeyValCopyHTValDestructor
(
void
*
privdata
,
void
*
val
)
{
{
DICT_NOTUSED
(
privdata
);
DICT_NOTUSED
(
privdata
);
_dictF
ree
((
void
*
)
val
);
/* ATTENTION: const cast */
f
ree
((
void
*
)
val
);
/* ATTENTION: const cast */
}
}
dictType
dictTypeHeapStringCopyKey
=
{
dictType
dictTypeHeapStringCopyKey
=
{
...
...
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