aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libmdsserver/hash-table.c13
-rw-r--r--src/libmdsserver/hash-table.h19
2 files changed, 22 insertions, 10 deletions
diff --git a/src/libmdsserver/hash-table.c b/src/libmdsserver/hash-table.c
index a02e6a9..b56600b 100644
--- a/src/libmdsserver/hash-table.c
+++ b/src/libmdsserver/hash-table.c
@@ -430,12 +430,13 @@ void hash_table_marshal(const hash_table_t* restrict this, char* restrict data)
/**
* Unmarshals a hash table
*
- * @param this Memory slot in which to store the new hash table
- * @param data In buffer with the marshalled data
- * @return Non-zero one error, errno will be set accordingly.
- * Destroy the list on error.
+ * @param this Memory slot in which to store the new hash table
+ * @param data In buffer with the marshalled data
+ * @param remapper Function that translates values, `NULL` if not translation takes place
+ * @return Non-zero one error, errno will be set accordingly.
+ * Destroy the list on error.
*/
-int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data)
+int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap_func* remapper)
{
size_t i, n;
@@ -477,6 +478,8 @@ int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data)
}
bucket->key = ((size_t*)data)[0];
bucket->value = ((size_t*)data)[1];
+ if (remapper != NULL)
+ bucket->value = remapper(bucket->value);
bucket->hash = ((size_t*)data)[2];
data += 3 * sizeof(size_t) / sizeof(char);
}
diff --git a/src/libmdsserver/hash-table.h b/src/libmdsserver/hash-table.h
index 9b05804..2525da0 100644
--- a/src/libmdsserver/hash-table.h
+++ b/src/libmdsserver/hash-table.h
@@ -46,6 +46,14 @@ typedef size_t hash_func(size_t value);
*/
typedef void free_func(size_t obj);
+/**
+ * A function that translates a object into a new object
+ *
+ * @param obj The object
+ * @return obj The new object
+ */
+typedef size_t remap_func(size_t obj);
+
/**
* Hash table entry
@@ -251,12 +259,13 @@ void hash_table_marshal(const hash_table_t* restrict this, char* restrict data);
/**
* Unmarshals a hash table
*
- * @param this Memory slot in which to store the new hash table
- * @param data In buffer with the marshalled data
- * @return Non-zero one error, errno will be set accordingly.
- * Destroy the list on error.
+ * @param this Memory slot in which to store the new hash table
+ * @param data In buffer with the marshalled data
+ * @param remapper Function that translates values, `NULL` if not translation takes place
+ * @return Non-zero one error, errno will be set accordingly.
+ * Destroy the list on error.
*/
-int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data);
+int hash_table_unmarshal(hash_table_t* restrict this, char* restrict data, remap_func* remapper);
#endif