summaryrefslogtreecommitdiff
path: root/lib/hash.c
diff options
context:
space:
mode:
authorDaniel Stenberg <daniel@haxx.se>2017-04-01 01:12:32 +0200
committerDaniel Stenberg <daniel@haxx.se>2017-04-04 15:37:37 +0200
commit4f2e348f9b42c69c480bffd0188502167cf4ad07 (patch)
tree5fb9d6c07e2488ef599cbd83396810af2212b0c9 /lib/hash.c
parente60fe20fdf94e829ba5fce33f7a9d6c281149f7d (diff)
downloadcurl-4f2e348f9b42c69c480bffd0188502167cf4ad07.tar.gz
hash: move key into hash struct to reduce mallocs
This removes one tiny malloc for each hash struct allocated. In a simple case like "curl localhost", this save three mallocs. Closes #1376
Diffstat (limited to 'lib/hash.c')
-rw-r--r--lib/hash.c26
1 files changed, 7 insertions, 19 deletions
diff --git a/lib/hash.c b/lib/hash.c
index c76bc1d95..b7305a572 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -37,8 +37,6 @@ hash_element_dtor(void *user, void *element)
struct curl_hash *h = (struct curl_hash *) user;
struct curl_hash_element *e = (struct curl_hash_element *) element;
- Curl_safefree(e->key);
-
if(e->ptr) {
h->dtor(e->ptr);
e->ptr = NULL;
@@ -87,23 +85,14 @@ Curl_hash_init(struct curl_hash *h,
static struct curl_hash_element *
mk_hash_element(const void *key, size_t key_len, const void *p)
{
- struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element));
-
+ /* allocate the struct plus memory after it to store the key */
+ struct curl_hash_element *he = malloc(sizeof(struct curl_hash_element) +
+ key_len);
if(he) {
- void *dupkey = malloc(key_len);
- if(dupkey) {
- /* copy the key */
- memcpy(dupkey, key, key_len);
-
- he->key = dupkey;
- he->key_len = key_len;
- he->ptr = (void *) p;
- }
- else {
- /* failed to duplicate the key, free memory and fail */
- free(he);
- he = NULL;
- }
+ /* copy the key */
+ memcpy(he->key, key, key_len);
+ he->key_len = key_len;
+ he->ptr = (void *) p;
}
return he;
}
@@ -145,7 +134,6 @@ Curl_hash_add(struct curl_hash *h, void *key, size_t key_len, void *p)
* "destructor" for the actual data 'p'. When we fail, we shall not touch
* that data.
*/
- free(he->key);
free(he);
}