From 739a3cfb154625effad019ac5a97deffc5175585 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 23 Apr 2014 07:03:17 +0200 Subject: add value remapping to hash table unmarshaling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsserver/hash-table.c | 13 ++++++++----- src/libmdsserver/hash-table.h | 19 ++++++++++++++----- 2 files changed, 22 insertions(+), 10 deletions(-) (limited to 'src') 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 -- cgit v1.2.3-70-g09d2