Skip to content

Commit

Permalink
llist: replace Curl_llist_alloc with Curl_llist_init
Browse files Browse the repository at this point in the history
No longer allocate the curl_llist head struct for lists separately.

Removes 17 (15%) tiny allocations in a normal "curl localhost" invoke.

closes curl#1381
  • Loading branch information
bagder committed Apr 4, 2017
1 parent a68ca63 commit e60fe20
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 310 deletions.
22 changes: 8 additions & 14 deletions lib/conncache.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ static CURLcode bundle_create(struct Curl_easy *data,
(*cb_ptr)->num_connections = 0;
(*cb_ptr)->multiuse = BUNDLE_UNKNOWN;

(*cb_ptr)->conn_list = Curl_llist_alloc((curl_llist_dtor) conn_llist_dtor);
if(!(*cb_ptr)->conn_list) {
Curl_safefree(*cb_ptr);
return CURLE_OUT_OF_MEMORY;
}
Curl_llist_init(&(*cb_ptr)->conn_list, (curl_llist_dtor) conn_llist_dtor);
return CURLE_OK;
}

Expand All @@ -69,18 +65,16 @@ static void bundle_destroy(struct connectbundle *cb_ptr)
if(!cb_ptr)
return;

if(cb_ptr->conn_list) {
Curl_llist_destroy(cb_ptr->conn_list, NULL);
cb_ptr->conn_list = NULL;
}
Curl_llist_destroy(&cb_ptr->conn_list, NULL);

free(cb_ptr);
}

/* Add a connection to a bundle */
static CURLcode bundle_add_conn(struct connectbundle *cb_ptr,
struct connectdata *conn)
{
if(!Curl_llist_insert_next(cb_ptr->conn_list, cb_ptr->conn_list->tail, conn))
if(!Curl_llist_insert_next(&cb_ptr->conn_list, cb_ptr->conn_list.tail, conn))
return CURLE_OUT_OF_MEMORY;

conn->bundle = cb_ptr;
Expand All @@ -95,10 +89,10 @@ static int bundle_remove_conn(struct connectbundle *cb_ptr,
{
struct curl_llist_element *curr;

curr = cb_ptr->conn_list->head;
curr = cb_ptr->conn_list.head;
while(curr) {
if(curr->ptr == conn) {
Curl_llist_remove(cb_ptr->conn_list, curr, NULL);
Curl_llist_remove(&cb_ptr->conn_list, curr, NULL);
cb_ptr->num_connections--;
conn->bundle = NULL;
return 1; /* we removed a handle */
Expand Down Expand Up @@ -289,7 +283,7 @@ void Curl_conncache_foreach(struct conncache *connc,
bundle = he->ptr;
he = Curl_hash_next_element(&iter);

curr = bundle->conn_list->head;
curr = bundle->conn_list.head;
while(curr) {
/* Yes, we need to update curr before calling func(), because func()
might decide to remove the connection */
Expand Down Expand Up @@ -318,7 +312,7 @@ Curl_conncache_find_first_connection(struct conncache *connc)
struct curl_llist_element *curr;
bundle = he->ptr;

curr = bundle->conn_list->head;
curr = bundle->conn_list.head;
if(curr) {
return curr->ptr;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/conncache.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 2015 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 2012 - 2014, Linus Nielsen Feltzing, <linus@haxx.se>
*
* This software is licensed as described in the file COPYING, which
Expand Down Expand Up @@ -38,7 +38,7 @@ struct conncache {
struct connectbundle {
int multiuse; /* supports multi-use */
size_t num_connections; /* Number of connections in the bundle */
struct curl_llist *conn_list; /* The connectdata members of the bundle */
struct curl_llist conn_list; /* The connectdata members of the bundle */
};

int Curl_conncache_init(struct conncache *, int size);
Expand Down
15 changes: 8 additions & 7 deletions lib/ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -3900,7 +3900,7 @@ static CURLcode wc_statemach(struct connectdata *conn)
wildcard->state = CURLWC_CLEAN;
return wc_statemach(conn);
}
if(wildcard->filelist->size == 0) {
if(wildcard->filelist.size == 0) {
/* no corresponding file */
wildcard->state = CURLWC_CLEAN;
return CURLE_REMOTE_FILE_NOT_FOUND;
Expand All @@ -3911,7 +3911,7 @@ static CURLcode wc_statemach(struct connectdata *conn)
case CURLWC_DOWNLOADING: {
/* filelist has at least one file, lets get first one */
struct ftp_conn *ftpc = &conn->proto.ftpc;
struct curl_fileinfo *finfo = wildcard->filelist->head->ptr;
struct curl_fileinfo *finfo = wildcard->filelist.head->ptr;

char *tmp_path = aprintf("%s%s", wildcard->path, finfo->filename);
if(!tmp_path)
Expand All @@ -3926,7 +3926,7 @@ static CURLcode wc_statemach(struct connectdata *conn)
infof(conn->data, "Wildcard - START of \"%s\"\n", finfo->filename);
if(conn->data->set.chunk_bgn) {
long userresponse = conn->data->set.chunk_bgn(
finfo, wildcard->customptr, (int)wildcard->filelist->size);
finfo, wildcard->customptr, (int)wildcard->filelist.size);
switch(userresponse) {
case CURL_CHUNK_BGN_FUNC_SKIP:
infof(conn->data, "Wildcard - \"%s\" skipped by user\n",
Expand All @@ -3951,9 +3951,9 @@ static CURLcode wc_statemach(struct connectdata *conn)
return result;

/* we don't need the Curl_fileinfo of first file anymore */
Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
Curl_llist_remove(&wildcard->filelist, wildcard->filelist.head, NULL);

if(wildcard->filelist->size == 0) { /* remains only one file to down. */
if(wildcard->filelist.size == 0) { /* remains only one file to down. */
wildcard->state = CURLWC_CLEAN;
/* after that will be ftp_do called once again and no transfer
will be done because of CURLWC_CLEAN state */
Expand All @@ -3964,8 +3964,8 @@ static CURLcode wc_statemach(struct connectdata *conn)
case CURLWC_SKIP: {
if(conn->data->set.chunk_end)
conn->data->set.chunk_end(conn->data->wildcard.customptr);
Curl_llist_remove(wildcard->filelist, wildcard->filelist->head, NULL);
wildcard->state = (wildcard->filelist->size == 0) ?
Curl_llist_remove(&wildcard->filelist, wildcard->filelist.head, NULL);
wildcard->state = (wildcard->filelist.size == 0) ?
CURLWC_CLEAN : CURLWC_DOWNLOADING;
return wc_statemach(conn);
}
Expand All @@ -3981,6 +3981,7 @@ static CURLcode wc_statemach(struct connectdata *conn)

case CURLWC_DONE:
case CURLWC_ERROR:
case CURLWC_CLEAR:
break;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ftplistparser.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -280,7 +280,7 @@ static CURLcode ftp_pl_insert_finfo(struct connectdata *conn,
curl_fnmatch_callback compare;
struct WildcardData *wc = &conn->data->wildcard;
struct ftp_wc_tmpdata *tmpdata = wc->tmp;
struct curl_llist *llist = wc->filelist;
struct curl_llist *llist = &wc->filelist;
struct ftp_parselist_data *parser = tmpdata->parser;
bool add = TRUE;

Expand Down
30 changes: 9 additions & 21 deletions lib/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -74,21 +74,10 @@ Curl_hash_init(struct curl_hash *h,
h->size = 0;
h->slots = slots;

h->table = malloc(slots * sizeof(struct curl_llist *));
h->table = malloc(slots * sizeof(struct curl_llist));
if(h->table) {
for(i = 0; i < slots; ++i) {
h->table[i] = Curl_llist_alloc((curl_llist_dtor) hash_element_dtor);
if(!h->table[i]) {
while(i--) {
Curl_llist_destroy(h->table[i], NULL);
h->table[i] = NULL;
}
free(h->table);
h->table = NULL;
h->slots = 0;
return 1; /* failure */
}
}
for(i = 0; i < slots; ++i)
Curl_llist_init(&h->table[i], (curl_llist_dtor) hash_element_dtor);
return 0; /* fine */
}
h->slots = 0;
Expand Down Expand Up @@ -119,7 +108,7 @@ mk_hash_element(const void *key, size_t key_len, const void *p)
return he;
}

#define FETCH_LIST(x,y,z) x->table[x->hash_func(y, z, x->slots)]
#define FETCH_LIST(x,y,z) &x->table[x->hash_func(y, z, x->slots)]

/* Insert the data in the hash. If there already was a match in the hash,
* that data is replaced.
Expand Down Expand Up @@ -241,8 +230,7 @@ Curl_hash_destroy(struct curl_hash *h)
int i;

for(i = 0; i < h->slots; ++i) {
Curl_llist_destroy(h->table[i], (void *) h);
h->table[i] = NULL;
Curl_llist_destroy(&h->table[i], (void *) h);
}

Curl_safefree(h->table);
Expand Down Expand Up @@ -274,7 +262,7 @@ Curl_hash_clean_with_criterium(struct curl_hash *h, void *user,
return;

for(i = 0; i < h->slots; ++i) {
list = h->table[i];
list = &h->table[i];
le = list->head; /* get first list entry */
while(le) {
struct curl_hash_element *he = le->ptr;
Expand Down Expand Up @@ -333,8 +321,8 @@ Curl_hash_next_element(struct curl_hash_iterator *iter)
/* If we have reached the end of the list, find the next one */
if(!iter->current_element) {
for(i = iter->slot_index;i < h->slots;i++) {
if(h->table[i]->head) {
iter->current_element = h->table[i]->head;
if(h->table[i].head) {
iter->current_element = h->table[i].head;
iter->slot_index = i+1;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -44,7 +44,7 @@ typedef size_t (*comp_function) (void *key1,
typedef void (*curl_hash_dtor)(void *);

struct curl_hash {
struct curl_llist **table;
struct curl_llist *table;

/* Hash function to be used for this hash table */
hash_function hash_func;
Expand Down
22 changes: 3 additions & 19 deletions lib/llist.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -33,29 +33,15 @@
/*
* @unittest: 1300
*/
static void
llist_init(struct curl_llist *l, curl_llist_dtor dtor)
void
Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor)
{
l->size = 0;
l->dtor = dtor;
l->head = NULL;
l->tail = NULL;
}

struct curl_llist *
Curl_llist_alloc(curl_llist_dtor dtor)
{
struct curl_llist *list;

list = malloc(sizeof(struct curl_llist));
if(!list)
return NULL;

llist_init(list, dtor);

return list;
}

/*
* Curl_llist_insert_next()
*
Expand Down Expand Up @@ -149,8 +135,6 @@ Curl_llist_destroy(struct curl_llist *list, void *user)
if(list) {
while(list->size > 0)
Curl_llist_remove(list, list->tail, user);

free(list);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/llist.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
Expand Down Expand Up @@ -43,7 +43,7 @@ struct curl_llist {
size_t size;
};

struct curl_llist *Curl_llist_alloc(curl_llist_dtor);
void Curl_llist_init(struct curl_llist *, curl_llist_dtor);
int Curl_llist_insert_next(struct curl_llist *, struct curl_llist_element *,
const void *);
int Curl_llist_remove(struct curl_llist *, struct curl_llist_element *,
Expand Down
Loading

0 comments on commit e60fe20

Please sign in to comment.