Interface HashFunc

All Superinterfaces:
FunctionPointer
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface @Generated("org.javagi.JavaGI") public interface HashFunc extends FunctionPointer

Functional interface declaration of the HashFunc callback.

See Also:
  • Method Details

    • run

      int run(@Nullable MemorySegment key)

      Specifies the type of the hash function which is passed to HashTable.new_(HashFunc, EqualFunc) when a HashTable is created.

      The function is passed a key and should return an unsigned int hash value. The functions GLib.directHash(MemorySegment), GLib.intHash(MemorySegment) and GLib.strHash(MemorySegment) provide hash functions which can be used when the key is a void*, int*, and char* respectively.

      GLib.directHash(MemorySegment) is also the appropriate hash function for keys of the form GINT_TO_POINTER (n) (or similar macros).

      A good hash functions should produce hash values that are evenly distributed over a fairly large range. The modulus is taken with the hash table size (a prime number) to find the 'bucket' to place each key into. The function should also be very fast, since it is called for each key lookup.

      Note that the hash functions provided by GLib have these qualities, but are not particularly robust against manufactured keys that cause hash collisions. Therefore, you should consider choosing a more secure hash function when using a HashTable with keys that originate in untrusted data (such as HTTP requests). Using GLib.strHash(MemorySegment) in that situation might make your application vulnerable to Algorithmic Complexity Attacks.

      The key to choosing a good hash is unpredictability. Even cryptographic hashes are very easy to find collisions for when the remainder is taken modulo a somewhat predictable prime number. There must be an element of randomness that an attacker is unable to guess.

      Parameters:
      key - a key
      Returns:
      the hash value corresponding to the key
    • upcall

      default int upcall(MemorySegment key)
      The upcall method is called from native code. The parameters are marshaled and run(MemorySegment) is executed.
    • toCallback

      default MemorySegment toCallback(Arena arena)
      Creates a native function pointer to the upcall(MemorySegment) method.
      Specified by:
      toCallback in interface FunctionPointer
      Parameters:
      arena - the arena in which the function pointer is allocated
      Returns:
      the native function pointer