HASH_DEL of the first iem

61 views
Skip to first unread message

gilberto...@gmail.com

unread,
Oct 4, 2017, 4:55:20 PM10/4/17
to uthash
Hello,
I started experimenting with uthash (great project !) to handle a fairly large dynamic ip cache.
Before changing all my code I started making some tests and it seems very nice and easy to use. One thing that is not clear to me is how deletion of first element in the hashtable works.
Let's say as example, I have this big hashtable with IPs and at a certain moment I need to purge some elements and incidentally the first element has to be deleted.
What I do is a classic:

struct mycache
{
int key,
struct *mycontents;
UT_hash_handle hh;
};

static struct mycache *cache = NULL;


void purgecache(struct mycache *)
{

HASH_ITER(hh,mycache,myentry,tmp)
{
if (mychache->hastodelete == TRUE)
{
HASH_DEL(mycache,myentry)
free(myentry->mycontents);
free(myentry);
}
}
}

int main(int argc, char**argv)
{
.... all cache creation stuff ...
purgecache(cache);

}

This is a stupid example, but if the head changes, who's going to update it ?
I have to admit that I still hadn't time to read the source code, I'll probably do in the next days, but was just curious if anybody happened to encounter and solve this, I'm scratching my head in the meanwhile, probably missing something very stupid :-)

Thanks
Gilberto

Arthur O'Dwyer

unread,
Oct 4, 2017, 5:14:19 PM10/4/17
to uth...@googlegroups.com
On Wed, Oct 4, 2017 at 1:50 PM, <gilberto...@gmail.com> wrote:
Hello,
I started experimenting with uthash (great project !) to handle a fairly large dynamic ip cache.
Before changing all my code I started making some tests and it seems very nice and easy to use. One thing that is not clear to me is how deletion of first element in the hashtable works.
Let's say as example, I have this big hashtable with IPs and at a certain moment I need to purge some elements and incidentally the first element has to be deleted.
What I do is a classic:

struct mycache
{
      int key,
      struct *mycontents;
      UT_hash_handle hh;
};

static struct mycache *cache = NULL;

This pointer variable ("cache") represents the hash table.
 
void purgecache(struct mycache *mycache)
{

(I added the missing "mycache" above.)
This pointer parameter represents a hash table. You're effectively passing a "copy" of the table here (sort of; I mean it's going to share the actual table entries, but...) if you want to pass the variable `cache` by reference/pointer, then you should be taking a reference/pointer to `struct mycache *` here.

In other words, in C++ terms, this function signature should be

    void purgecache(struct mycache *& mycache)

if you want your changes to "mycache" to be reflected back in the caller. Now, C doesn't have reference parameters, so in C you'll want to pass by pointer...

    void purgecache(struct mycache **pmycache)

...and then refer to the hash table as (*pmycache) throughout the function body.

[...]
int main(int argc, char**argv)
{
.... all cache creation stuff ...
   purgecache(cache);

In C++ pass by reference; in C pass by pointer, so this will have to be

    purgecache(&cache);

HTH,
Arthur

gilberto...@gmail.com

unread,
Oct 4, 2017, 6:39:17 PM10/4/17
to uthash
Thanks Arthur, I feel completely stupid about this, it was the "Blinding flash of the obvious" that after a day of work made me completely blind.
Thanks again

Gilberto
Reply all
Reply to author
Forward
0 new messages