libc's tree functions, function pointers, and bdb.
Posted Sun, 05 Oct 2008
Whoever wrote the tsearch/tfind/tdelete/twalk functions for libc clearly
stopped thinking about how it would be used. The only way I can see to iterate
the tree is to use twalk, which doesn't let you pass any extra arguments to
your provided action method.
This sucks if, for example, you wanted to get a list of all entries in the tree in a threadsafe or multiplicity-safe way.
Some workarounds include:
- Every time you insert, add the same structure to an array.
- Use something that supports sane iteration (bdb, for example).
This bdb code sample attempts to store a function pointer in bdb. The output is:
stored: 607a70 actual: 4005e8The value changed because copying a function pointer doesn't work.
There's a workaround here that might be useful - dlopen(). Any functions I want to store in bdb, I would store by string name and fetch the function pointer with dlsym().
This dlopen example shows how to dlopen yourself and fetch a function by string name.
Fun with pointers.