[MLton] feature request: hash function for Pointer.t
Matthew Fluet
matthew.fluet at gmail.com
Sun Dec 6 14:29:07 PST 2009
On Sun, Dec 6, 2009 at 12:31 PM, John Reppy <jhr at cs.uchicago.edu> wrote:
> Because MLton doesn't provide a way to dynamically define callbacks, I need
> to manage
> the a mapping from C pointers to ML functions. I'd like to use a hash
> table to do
> this, but there isn't an easy way to compute hash keys from pointers. I'd
> like
> to see a function
>
> val hash : t -> word
>
> added to the MLTON_POINTER signature.
>
If you believe that the C address is unique enough to be a hash key, doesn't
val hash = fn p => MLton.Pointer.diff(MLton.Pointer.null, p)
suffice as a hashing function? In any case, I think any hashing of a
pointer would start with its address, so you can do a mixing function from
there if you don't think the distribution of C addresses suffice as a
hashing key. Admittedly, this is in terms of Word.word, which defaults to a
32-bit value; MLton.Pointer.diff performs the difference computation at the
target pointer size, and then does a low-bits projection to a 32-bit word.
Still, that ought to be decent for a hash function. Perhaps all of the
MLton.Pointer functions should be in terms of LargeWord.word.
Also,
val MLton.hash : 'a -> Word32.word
is a polymorphic hash function that works with any ML value.
> As a more long-term request, it would be nice to have something like the
> GHC notion
> of stable pointers that would allow ML values to be passed to C code for
> later supply
> as arguments to callbacks.
>
Yes, some notion of registering an exported ML value would be desirable.
-Matthew
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mlton.org/pipermail/mlton/attachments/20091206/0f81669c/attachment.html
More information about the MLton
mailing list