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).
I looked into using bdb for some things, but the tree I wanted to iterate over
most was a structure that held, among other things, a function pointer.
Function pointers are magical and special and are held in a place in memory you
can't simply make a copy of. If you try to store a function pointer in an
in-memory BDB database, the value that comes out of your query will be
different than the function pointer.
This bdb code sample attempts to store a function pointer in bdb. The output is:
The 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
dlopen example shows how to dlopen yourself and fetch a function by string
Fun with pointers.