From 55bff1c4e022a609068a14430a1390a2be2e4ca1 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 20 Aug 2015 15:59:29 +0200 Subject: m + info: hash list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-colour.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 6 deletions(-) (limited to 'src/mds-colour.c') diff --git a/src/mds-colour.c b/src/mds-colour.c index 2f3e357..2bee831 100644 --- a/src/mds-colour.c +++ b/src/mds-colour.c @@ -22,7 +22,7 @@ #include #include #include -#include +#include #include #include @@ -84,7 +84,7 @@ static size_t send_buffer_size = 0; /** * List of all colours */ -static colour_slot_t* colour_list = NULL; +static colour_list_t colours; @@ -130,14 +130,15 @@ int initialise_server(void) "Command: set-colour\n"; fail_if (full_send(message, strlen(message))); - fail_if (server_initialised() < 0); stage++; + fail_if (server_initialised() < 0); stage++;; + fail_if (colour_list_create(&colours, 64) < 0); stage++; fail_if (mds_message_initialise(&received)); return 0; fail: xperror(*argv); - if (stage >= 1) - mds_message_destroy(&received); + if (stage >= 2) colour_list_destroy(&colours); + if (stage >= 1) mds_message_destroy(&received); return 1; } @@ -150,6 +151,9 @@ int initialise_server(void) */ int postinitialise_server(void) { + colours.freer = colour_list_entry_free; + colours.hasher = string_hash; + if (connected) return 0; @@ -251,7 +255,7 @@ int master_loop(void) danger = 0; free(send_buffer), send_buffer = NULL; send_buffer_size = 0; - colour_list_pack(&colour_list); + colour_list_pack(&colours); } if (r = mds_message_read(&received, socket_fd), r == 0) @@ -479,3 +483,76 @@ void received_info(int signo) iprintf("send buffer size: %zu bytes", send_buffer_size); } + +/** + * Comparing keys + * + * @param key_a The first key, will never be `NULL` + * @param key_b The second key, will never be `NULL` + * @return Whether the keys are equal + */ +static inline int colour_list_key_comparer(const char* key_a, const char* key_b) +{ + return !strcmp(key_a, key_b); +} + + +/** + * Determine the marshal-size of an entry's key and value + * + * @param entry The entry, will never be `NULL`, any only used entries will be passed + * @return The marshal-size of the entry's key and value + */ +static inline size_t colour_list_submarshal_size(const colour_list_entry_t* entry) +{ + return sizeof(colour_t) + (strlen(entry->key) + 1) * sizeof(char); +} + + +/** + * Marshal an entry's key and value + * + * @param entry The entry, will never be `NULL`, any only used entries will be passed + * @return The marshal-size of the entry's key and value + */ +static inline size_t colour_list_submarshal(const colour_list_entry_t* entry, char* restrict data) +{ + size_t n = (strlen(entry->key) + 1) * sizeof(char); + memcpy(data, &(entry->value), sizeof(colour_t)); + data += sizeof(colour_t) / sizeof(char); + memcpy(data, entry->key, n); + return sizeof(colour_t) + n; +} + + +/** + * Unmarshal an entry's key and value + * + * @param entry The entry, will never be `NULL`, any only used entries will be passed + * @return The number of read bytes, zero on error + */ +static inline size_t colour_list_subunmarshal(colour_list_entry_t* entry, char* restrict data) +{ + size_t n = 0; + memcpy(&(entry->value), data, sizeof(colour_t)); + data += sizeof(colour_t) / sizeof(char); + while (data[n++]); + n *= sizeof(char); + entry->key = malloc(n); + if (entry->key == NULL) + return 0; + memcpy(entry->key, data, n); + return sizeof(colour_t) + n; +} + + +/** + * Free the key and value of an entry in a colour list + * + * @param entry The entry + */ +void colour_list_entry_free(colour_list_entry_t* entry) +{ + free(entry->key); +} + -- cgit v1.2.3-70-g09d2