Index: trunk/kernel/libk/xhtab.h
===================================================================
--- trunk/kernel/libk/xhtab.h	(revision 690)
+++ trunk/kernel/libk/xhtab.h	(revision 1)
@@ -2,5 +2,5 @@
  * xhtab.h - Remote access embedded hash table definition.
  * 
- * Author     Alain Greiner  (2016,2017,2018,2019)
+ * Author     Alain Greiner          (2016)
  *
  * Copyright (c) UPMC Sorbonne Universites
@@ -25,90 +25,80 @@
 #define _XHTAB_H_
 
-#include <kernel_config.h>
-#include <hal_kernel_types.h>
+#include <almos_config.h>
+#include <hal_types.h>
 #include <remote_rwlock.h>
 #include <xlist.h>
 
 
-///////////////////////////////////////////////////////////////////////////////////////////
-// This file define a generic, embedded, remotely accessible, hash table.
-//
-// It can be accessed by any thread, running in any cluster.
-// It is generic as it can be used to register various types of items.
-// The main goal is to speedup search by key in a large number of items of same type.
-// For this purpose the set of all registered items is split in several subsets.
-// Each subset is organised as an embedded double linked xlist.
-// - an item is uniquely identified by a <key>, that is a item specific pointer,
-//   that can be a - for example - a char* defining the item "name".
-// - From the <key> value, the hash table uses an item type specific index_from_key() 
-//   function, to compute an <index> value, defining a subset of registered items.
-// - to discriminate between items that have the same <index>, the hash table makes
-//   an associative search on the key in subset, using the item type specific
-//   item_match_key() function.
-// - Each registered item is a structure, that must contain an embedded xlist_entry,
-//   that is part of the xlist implementing the subset. 
-//
-// For all registered items, a total order is defined by the increasing index values, 
-// and for each index value, by the position in the xlist implementing a subset. 
-// This order is used by the two functions xhtab_get_first() and xhtab_get_next(), that
-// are used to scan all registered items. The two "current_index" and "current_xlist_xp"
-// fields in the hash table header register the current item during a scan.
-//
-// Implementation Note:
-// To inroduce a new item type, you must define the four item-type-specific
-// functions specified below, and you must update the xhtab_init() function
-// and the xhtab_item_type_t.
-///////////////////////////////////////////////////////////////////////////////////////////
+/******************************************************************************************
+ * This file define a generic, embedded, hash table that can be remotely accessed by 
+ * any thread running in any cluster.
+ * The main goal is to speedup search by key in a set of items identified by their key.
+ * For this purpose the set of all registered items is split in several subsets:
+ * Each subset is organised an embedded double linked lists.
+ * - an item is uniquely identified by a <key>, that can be a single uint32_t, 
+ *   a name (character string), or a more complex structure.
+ * - From the <key>, the hash table uses an item type specific "xhtab_index()" function,
+ *   to compute an <index> value, defining a subset of registered items.
+ * - to discriminate between items that have the same <index>, the hash table uses another
+ *   item type specific "xhtab_compare()" function for the associative search in subset.
+ * - Each registered item is a structure, that must contain an embedded xlist_entry,
+ *   that is part of the xlist implementing the subset. 
+ * - The xhtab header contains an array indexed by <index> and containing the roots
+ *   of the various xlists implementing the subsets.
+ *   pointer on the searched item can be obtained by a simple offset.
+ * Implementation note:
+ * to use this generic infrastructure for a new type of item, you must define a new
+ * type in the xhtan_item_type_t" below, and to define the two  associated
+ * "xhtab_compare() and xhtab_index().
+ *****************************************************************************************/
 
-#define XHASHTAB_SIZE    32   // number of subsets
+#define HASHTAB_SIZE    64   // number of subsets
 
 /******************************************************************************************
- * Here are the four item_type_specific function prototypes that must be defined
- * for each item type.
+ * These typedef define the generic xhtab_compare() and xhtab_index() function prototypes.
+ * It must exist a specific couple of functions for each item type.
+ * @ entry_xp : extended pointer on an xlist_entry_t in a partial xlist.
+ * @ key      : local pointer on the searched item key.
+ * @ item_xp  : buffer to store extended pointer on found item.
  *****************************************************************************************/
 
-typedef  bool_t    (item_match_key_t)   ( xptr_t item_xp , void * key );
-typedef  xptr_t    (item_from_xlist_t)  ( xptr_t xlist_xp );
-typedef  uint32_t  (index_from_key_t)   ( void * key );
-typedef  void      (item_print_key_t)   ( xptr_t item_xp );
+typedef  bool_t    xhtab_compare_t( xptr_t entry_xp , void * key , xptr_t * item_xp );
+typedef  uint32_t  xhtab_index_t( void * key );
 
 /******************************************************************************************
- * This define the currently supported item types.
- * - The XHTAB_DENTRY_TYPE is used to implement the set of directory entries for a
- *   directory inode : the "children" inode field is an embedded xhtab.
+ * This define the supported item types.
  *****************************************************************************************/
 
 typedef enum
 {
-    XHTAB_DENTRY_TYPE = 0,                    /*! item is a vfs_dentry_t                 */ 
+    XHTAB_DENTRY_TYPE = 1,                   /*! item is a vfs_dentry_t                  */
+    XHTAB_INODE_TYPE  = 2,                   /*! item is a vfs_inode_t                   */
 }
 xhtab_item_type_t;
 
 /******************************************************************************************
- * This structure define the root of the remotely accessible hash table. 
+ * This structure define the root of a generic, remote accessible, hash table. 
  *****************************************************************************************/
 
 typedef struct xhtab_s
 {
-	xlist_entry_t       roots[XHASHTAB_SIZE];  /*! array of roots of xlist               */
-    index_from_key_t  * index_from_key;        /*! item specific function pointer        */
-    item_match_key_t  * item_match_key;        /*! item specific function pointer        */
-    item_from_xlist_t * item_from_xlist;       /*! item specific function pointer        */
-    item_print_key_t  * item_print_key;        /*! item specific function pointer        */
-    uint32_t            items;                 /*! number of registered items            */
-    remote_busylock_t   lock;                  /*! lock protecting hash table accesses   */
-    uint32_t            current_index;         /*! current item subset index             */
-    xptr_t              current_xlist_xp;      /*! xptr on current item xlist entry      */
+	xlist_entry_t      roots[HASHTAB_SIZE];   /*! array of roots of xlist                */
+    xhtab_compare_t  * compare;               /*! item specific index function           */
+    xhtab_index_t    * index;                 /*! item specific compare function         */
+    uint32_t           items;                 /*! number of registered items             */
+    remote_rwlock_t  lock;                  /*! lock protecting hash table modifs      */
 }
 xhtab_t;
 
 /******************************************************************************************
- * This function initializes an empty hash table (zero registered item).
+ * This function initializes an empty hash table (zero children).
+ * The initialisation must be done by a thread running in cluster containing the table.
  ******************************************************************************************
- * @ xhtab_xp  : extended pointer on xhtab.
- * @ type      : item type (see above).
+ * @ xhtab   : local pointer on local xhtab to be initialized.
+ * @ type    : item type (see above).
  *****************************************************************************************/
-void xhtab_init( xptr_t              xhtab_xp,
-                 xhtab_item_type_t   type );
+void xhtab_init( xhtab_t * xhtab,
+                 uint32_t  type );
 
 /******************************************************************************************
@@ -118,21 +108,17 @@
  * @ key        : local pointer on item identifier.
  * @ xlist_xp   : extended pointer on xlist_entry_t embedded in item to be registered.
- * @ return 0 if success / return -1 if item already registered.
  *****************************************************************************************/
-error_t xhtab_insert( xptr_t   xhtab_xp,
-                      void   * key,
-                      xptr_t   xlist_xp );
+void xhtab_register( xptr_t   xhtab_xp,
+                     void   * key,
+                     xptr_t   xlist_xp );
 
 /******************************************************************************************
  * This function safely remove an item from the hash table, using the lock protecting it.
  ******************************************************************************************
- * @ xhtab_xp   : extended pointer on hash table.
- * @ key        : local pointer on item identifier.
- * @ xlist_xp   : extended pointer on xlist_entry embedded in item to be removed.
- * @ return 0 if item found / return -1 if item not found.
+ * @ xhtab_xp : extended pointer on hash table.
+ * @ key      : local pointer on item identifier.
  *****************************************************************************************/
-error_t xhtab_remove( xptr_t   xhtab_xp,
-                      void   * key,
-                      xptr_t   xlist_entry_xp );
+void xhtab_remove( xptr_t   xhtab_xp,
+                   void   * key );
 
 /******************************************************************************************
@@ -146,46 +132,47 @@
                       void    * key );
 
-/******************************************************************************************
- * This blocking function takes the lock protecting exclusive access to the hash table. 
- * It should be called before the xhtab_get_first() & xhtab_get_next() functions.
- ******************************************************************************************
- * @ xhtab_xp  : extended pointer on hash table.
- *****************************************************************************************/
-void xhtab_lock( xptr_t xhtab_xp );
+
+/************************  vfs_dentry_t specific functions  ******************************/
 
 /******************************************************************************************
- * This function releases the lock protecting exclusive access to the hash table. 
- * It should be called after the xhtab_get_first() & xhtab_get_next() functions.
+ * This function compute the hash index from the key, when the item is a vhs_dentry_t.
  ******************************************************************************************
- * @ xhtab_xp  : extended pointer on hash table.
+ * @ key      : local pointer on dentry name.
+ * @ return the index value, from 0 to (HASHTAB_SIZE - 1)
  *****************************************************************************************/
-void xhtab_unlock( xptr_t xhtab_xp );
+uint32_t xhtab_dentry_index( void * key );
 
 /******************************************************************************************
- * This function returns an extended pointer on the first item registered in hash table,
- * and register this pointer in the hash table header.
- * The lock protecting the hash table must have been previously taken by the caller.
+ * This function check the key value for a given item, when the item is a vhs_dentry_t.
  ******************************************************************************************
- * @ xhtab_xp  : extended pointer on hash table.
- * @ return extended pointer on item if success / XPTR_NULL if not found.
+ * @ xlist_xp : extended pointer on xlist_entry_t contained in a vfs_dentry_t.
+ * @ key      : local pointer on searched dentry name.
+ * @ return true if the item name matches the searched name. 
  *****************************************************************************************/
-xptr_t xhtab_get_first( xptr_t xhtab_xp );
+bool_t xhtab_dentry_compare( xptr_t   xlist_xp,
+                             void   * key,
+                             xptr_t * item_xp );
+
+
+/************************  vfs_inode_t specific functions  *******************************/
 
 /******************************************************************************************
- * This function returns an extended pointer on item following the currently pointed
- * item in the hash table header.
- * The lock protecting the hash table must have been previously taken by the caller.
+ * This function compute the hash index from the key, when the item is a vhs_inode_t.
  ******************************************************************************************
- * @ xhtab_xp  : extended pointer on hash table.
- * @ return extended pointer on item if success / XPTR_NULL if not found.
+ * @ key      : local pointer on dentry name.
+ * @ return the index value, from 0 to (HASHTAB_SIZE - 1)
  *****************************************************************************************/
-xptr_t xhtab_get_next( xptr_t xhtab_xp );
+uint32_t xhtab_inode_index( void * key );
 
 /******************************************************************************************
- * This function displays the full content of an xhtab.
+ * This function check the key value for a given item, when the item is a vhs_inode_t.
  ******************************************************************************************
- * @ xhtab_xp  : extended pointer on hash table.
+ * @ xlist_xp : extended pointer on xlist_entry_t contained in a vfs_dentry_t.
+ * @ key      : local pointer on searched dentry name.
+ * @ return true if the item name matches the searched name. 
  *****************************************************************************************/
-void xhtab_display( xptr_t  xhtab_xp );
+bool_t xhtab_inode_compare( xptr_t   xlist_xp,
+                            void   * key,
+                            xptr_t * item_xp );
 
 #endif	/* _XHTAB_H_ */
