Thursday, July 28, 2005

hash builtin - 2

Okay i am back to the hash builtin again.Lets trace phash_flush in hashcmd.c.
As expected it calls hash_flush.A look at the top of the function shows what it does,removes and discards
all entries in TABLE.If FREE_DATA is non-null then its kind of a destructor else call free() to free up the memory.

First it is checked if (table == 0) or if number of entries in the table are zero.

Once we are sure that number of hash entries are not zero, we iterate through the hash tables buckets.
Typically we call free(item->data),free(item->key) and free(item).

And finally we set the initial bucket pointer to NULL and set the number of entries to zero.

This is quite staightforward ,the way it should be.
So as we are doing okay ,lets move to the other functions.
Lets see phash_remove now,this as expected calls hash_remove.
A peek inside the code shows we try to get the bucket for the string first.
This buckets is got by hashing the string and anding with number of buckets -1 :).
The string typically is 'ls','ps' etc no mention of the data here but then who cares when its is getting deleted.
No.The pointer is being returned after being deleted from the list so lets go back to phash_remove.
Well i went back to phash_remove and tried to print the data before it gets freed.No luck.
I am reading junk so where is the path stored??
Okay i screwed up again ,without reading i assumed the complete file path would be stored as item->data but
thats not the case.item->data is a pointer to a struct PATH_DATA which contains the path.

Didnt i tell you my brain is a goner anyway back to code and let me print the pathname just before it gets freed,
(Dead Man Walking style) :).

Bingo.. now i get to print /usr/bin/ls just before its cleaned up by "hash -d ls" :).

0 Comments:

Post a Comment

<< Home