Search code examples
c++cglib

GLib HashTable passing the functions as the parameters


I am learning the GLib GHashTable source code. You can see the source code from the link here.

https://github.com/GNOME/glib/blob/master/glib/ghash.c

https://github.com/GNOME/glib/blob/master/glib/ghash.h

GHashTable *g_hash_table_new (GHashFunc  hash_func,
                                GEqualFunc key_equal_func)

g_hash_table_new function create the basic hash table here. My question is the parameters. The first two parameters "GHashFunc hash_func, GEqualFunc key_equal_func" are passing to the function.

In the ghash.h, there are no hash_func and key_equal_func, they are just symbols. They will be replaced by the real hash functions. How do the two functions become parameters here?

I know there are some hash functions in ghash.h files, like:

guint g_int_hash (gconstpointer v) 
guint g_str_hash (gconstpointer v)

Those functions will be passing to "g_hash_table_new " during the running time to replace "hash_func" and "key_equal_func ". But I do not know what kind of technique is that? How to passing a function as a parameter to another function and replace it during the running time.

Thanks


Solution

  • you pass param is a point, you define hash compute function. all function process is callback function. hash table just pass param to your equel function.