VPP API: Memory trace
[vpp.git] / src / vppinfra / hash.h
index 3f0efaa..de155e6 100644 (file)
@@ -77,6 +77,7 @@ typedef struct hash_header
 #define KEY_FUNC_POINTER_UWORD (1)     /*< sum = *(uword *) key */
 #define KEY_FUNC_POINTER_U32   (2)     /*< sum = *(u32 *) key */
 #define KEY_FUNC_STRING         (3)    /*< sum = string_key_sum, etc. */
+#define KEY_FUNC_MEM           (4)     /*< sum = mem_key_sum */
 
   /* key comparison function */
   hash_key_equal_function_t *key_equal;
@@ -273,12 +274,35 @@ uword hash_bytes (void *v);
 /* Public macro to set (key, value) for pointer key */
 #define hash_set_mem(h,key,value) hash_set3 (h, pointer_to_uword (key), (value), 0)
 
+/* Public inline funcion allocate and copy key to use in hash for pointer key */
+always_inline void
+hash_set_mem_alloc (uword ** h, void *key, uword v)
+{
+  size_t ksz = hash_header (*h)->user;
+  void *copy = clib_mem_alloc (ksz);
+  clib_memcpy (copy, key, ksz);
+  hash_set_mem (*h, copy, v);
+}
+
 /* Public macro to set (key, 0) for pointer key */
 #define hash_set1_mem(h,key)     hash_set3 ((h), pointer_to_uword (key), 0, 0)
 
 /* Public macro to unset (key, value) for pointer key */
 #define hash_unset_mem(h,key)    ((h) = _hash_unset ((h), pointer_to_uword (key),0))
 
+/* Public inline funcion to unset pointer key and then free the key memory */
+always_inline void
+hash_unset_mem_free (uword ** h, void *key)
+{
+  hash_pair_t *hp = hash_get_pair_mem (*h, key);
+  if (PREDICT_TRUE (hp != NULL))
+    {
+      key = uword_to_pointer (hp->key, void *);
+      hash_unset_mem (*h, key);
+      clib_mem_free (key);
+    }
+}
+
 /* internal routine to free a hash table */
 extern void *_hash_free (void *v);
 
@@ -649,6 +673,20 @@ extern uword string_key_sum (hash_t * h, uword key);
 extern uword string_key_equal (hash_t * h, uword key1, uword key2);
 extern u8 *string_key_format_pair (u8 * s, va_list * args);
 
+/*
+ * Note: if you plan to put a hash into shared memory,
+ * the key sum and key equal functions MUST be set to magic constants!
+ * PIC means that whichever process sets up the hash won't have
+ * the actual key sum functions at the same place, leading to
+ * very hard-to-find bugs...
+ */
+
+#define hash_create_shmem(elts,key_bytes,value_bytes)           \
+  hash_create2((elts),(key_bytes),(value_bytes),                \
+               (hash_key_sum_function_t *) KEY_FUNC_MEM,        \
+               (hash_key_equal_function_t *)KEY_FUNC_MEM,       \
+               0, 0)
+
 #define hash_create_string(elts,value_bytes)                    \
   hash_create2((elts),0,(value_bytes),                          \
                (hash_key_sum_function_t *) KEY_FUNC_STRING,     \
@@ -683,11 +721,6 @@ unformat_function_t unformat_hash_string;
 /* Main test routine. */
 int test_hash_main (unformat_input_t * input);
 
-static inline void
-hash_delete (void *bob)
-{
-}
-
 #endif /* included_hash_h */
 
 /*