From 4326c4c8b5f9e4e5ec0fd09cf1a50ae43db607a2 Mon Sep 17 00:00:00 2001 From: Anony Moose Date: Sat, 24 Aug 2013 20:34:38 +0000 Subject: Fixed semantics of tox_array (should also be more memory efficient). --- testing/misc_tools.h | 75 ++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) (limited to 'testing/misc_tools.h') diff --git a/testing/misc_tools.h b/testing/misc_tools.h index 1379b31a..006ddbd9 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h @@ -26,6 +26,7 @@ #include #include +#include /* for memcpy() */ unsigned char *hex_string_to_bin(char hex_string[]); @@ -67,6 +68,7 @@ unsigned char *hex_string_to_bin(char hex_string[]); /************************Linked List*********************** * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List + * TODO: Update wiki. **********************************************************/ #define MEMBER_OFFSET(var_name_in_parent, parent_type) \ @@ -76,22 +78,22 @@ unsigned char *hex_string_to_bin(char hex_string[]); ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type)))) #define TOX_LIST_FOR_EACH(lst, tmp_name) \ - for (tox_list_t* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next) + for (tox_list* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next) #define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) typedef struct tox_list { struct tox_list *prev, *next; -} tox_list_t; +} tox_list; /* Returns a new tox_list_t. */ -static inline void tox_list_new(tox_list_t *lst) +static inline void tox_list_new(tox_list *lst) { lst->prev = lst->next = lst; } /* Inserts a new tox_lst after lst and returns it. */ -static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst) +static inline void tox_list_add(tox_list *lst, tox_list *new_lst) { tox_list_new(new_lst); @@ -102,61 +104,60 @@ static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst) new_lst->prev = lst; } -static inline void tox_list_remove(tox_list_t *lst) +static inline void +tox_list_remove(tox_list *lst) { lst->prev->next = lst->next; lst->next->prev = lst->prev; } /****************************Array*************************** - * Array to store pointers which tracks it's own size. - * TODO: Figure out if it shold store values instead of - * pointers? + * Array which manages its own memory allocation. + * It stores copy of data (not pointers). * TODO: Add wiki info usage. ************************************************************/ -struct tox_array { - void **data; - uint32_t size, length; -}; +typedef struct tox_array { + void *data; + uint32_t len; + size_t elem_size; /* in bytes */ +} tox_array; -static inline void tox_array_init(struct tox_array *arr) +static inline void +tox_array_init(tox_array *arr, size_t elem_size) { - arr->size = 1; - arr->length = 0; - arr->data = malloc(sizeof(void *)); + arr->len = 0; + arr->elem_size = elem_size; + arr->data = NULL; } -static inline void tox_array_delete(struct tox_array *arr) +static inline void +tox_array_delete(tox_array *arr) { free(arr->data); - arr->size = arr->length = 0; + arr->len = arr->elem_size = 0; } -/* shrinks arr so it will not have unused space. If you want to have - * addtional space, extra species the amount of extra space. - */ -static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra) -{ - arr->size = arr->length + extra; - arr->data = realloc(arr->data, arr->size * sizeof(void *)); -} - -static inline void _tox_array_push(struct tox_array *arr, void *new) +static inline void +_tox_array_push(tox_array *arr, uint8_t *item) { - if (arr->length + 1 >= arr->size) - tox_array_shrink_to_fit(arr, arr->size); + arr->data = realloc(arr->data, arr->elem_size * (arr->len+1)); - arr->data[arr->length++] = new; + memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size); + arr->len++; } -#define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) +#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item))) -static inline void *tox_array_pop(struct tox_array *arr) +/* Deletes num items from array. + * Not same as pop in stacks, because to access elements you use data. + */ +static inline void +tox_array_pop(tox_array *arr, uint32_t num) { - if (arr->length - 1 < arr->size / 4) - tox_array_shrink_to_fit(arr, arr->length * 2); - - return arr->data[arr->length--]; + arr->len--; + arr->data = realloc(arr->data, arr->elem_size*arr->len); } +#define tox_array_get(arr, i, type) ((type*)(arr)->data)[i] + #endif // MISC_TOOLS_H -- cgit v1.2.3