[PATCH] libfanout: stash next pointer
Andreas Ericsson
ae at op5.se
Mon Mar 25 11:01:33 CET 2013
Embarrassing catch, but thanks.
On 03/22/2013 05:22 PM, Max Sikstrom wrote:
> From: Max Sikström <msikstrom at op5.com>
>
> Stash next pointer for entry when iterating over a linked list in the obiously
> correct fanout lib.
>
> The next pointer is lost when freeing the entry item in a for loop... One
> penny^Cpointer saved is a pointer earned, as my grandma used to say...
>
> Signed-off-by: Max Sikström <msikstrom at op5.com>
> ---
> lib/fanout.c | 10 +++++++---
> 1 files changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/lib/fanout.c b/lib/fanout.c
> index dda2874..a14d693 100644
> --- a/lib/fanout.c
> +++ b/lib/fanout.c
> @@ -29,13 +29,15 @@ fanout_table *fanout_create(unsigned long size)
> void fanout_destroy(fanout_table *t, void (*destructor)(void *))
> {
> unsigned long i;
> + struct fanout_entry *next;
>
> if (!t || !t->entries || !t->alloc)
> return;
>
> for (i = 0; i < t->alloc; i++) {
> struct fanout_entry *entry;
> - for (entry = t->entries[i]; entry; entry = entry->next) {
> + for (entry = t->entries[i]; entry; entry = next) {
> + next = entry->next; /* Stash it, it might be gone later */
> if (destructor) {
> destructor(entry->data);
> }
> @@ -67,13 +69,15 @@ int fanout_add(struct fanout_table *t, unsigned long key, void *data)
>
> void *fanout_remove(fanout_table *t, unsigned long key)
> {
> - struct fanout_entry *entry, *prev = NULL;
> + struct fanout_entry *entry, *next, *prev = NULL;
> unsigned long slot;
> +
> if (!t || !t->entries || !t->alloc)
> return NULL;
>
> slot = key % t->alloc;
> - for (entry = t->entries[slot]; entry; prev = entry, entry = entry->next) {
> + for (entry = t->entries[slot]; entry; prev = entry, entry = next) {
> + next = entry->next; /* Stash it, it might be gone later */
> if (entry->key == key) {
> void *data = entry->data;
> if (prev) {
>
--
Andreas Ericsson andreas.ericsson at op5.se
OP5 AB www.op5.se
Tel: +46 8-230225 Fax: +46 8-230231
Considering the successes of the wars on alcohol, poverty, drugs and
terror, I think we should give some serious thought to declaring war
on peace.
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar
More information about the Developers
mailing list