summaryrefslogtreecommitdiff
path: root/testing/misc_tools.h
diff options
context:
space:
mode:
authorAnony Moose <noMail@nomail.su>2013-08-24 20:34:38 +0000
committerAnony Moose <noMail@nomail.su>2013-08-24 20:34:38 +0000
commit4326c4c8b5f9e4e5ec0fd09cf1a50ae43db607a2 (patch)
treeeff4e12d175da66a7caf6e671fdc105be47439d3 /testing/misc_tools.h
parent05167a92942a0f9a2a44dea2eba1fad3f3d82f44 (diff)
Fixed semantics of tox_array (should also be more memory efficient).
Diffstat (limited to 'testing/misc_tools.h')
-rw-r--r--testing/misc_tools.h75
1 files changed, 38 insertions, 37 deletions
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 @@
26 26
27#include <stdlib.h> 27#include <stdlib.h>
28#include <stdint.h> 28#include <stdint.h>
29#include <string.h> /* for memcpy() */
29 30
30unsigned char *hex_string_to_bin(char hex_string[]); 31unsigned char *hex_string_to_bin(char hex_string[]);
31 32
@@ -67,6 +68,7 @@ unsigned char *hex_string_to_bin(char hex_string[]);
67 68
68/************************Linked List*********************** 69/************************Linked List***********************
69 * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List 70 * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List
71 * TODO: Update wiki.
70 **********************************************************/ 72 **********************************************************/
71 73
72#define MEMBER_OFFSET(var_name_in_parent, parent_type) \ 74#define MEMBER_OFFSET(var_name_in_parent, parent_type) \
@@ -76,22 +78,22 @@ unsigned char *hex_string_to_bin(char hex_string[]);
76 ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type)))) 78 ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))
77 79
78#define TOX_LIST_FOR_EACH(lst, tmp_name) \ 80#define TOX_LIST_FOR_EACH(lst, tmp_name) \
79 for (tox_list_t* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next) 81 for (tox_list* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next)
80 82
81#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) 83#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type)
82 84
83typedef struct tox_list { 85typedef struct tox_list {
84 struct tox_list *prev, *next; 86 struct tox_list *prev, *next;
85} tox_list_t; 87} tox_list;
86 88
87/* Returns a new tox_list_t. */ 89/* Returns a new tox_list_t. */
88static inline void tox_list_new(tox_list_t *lst) 90static inline void tox_list_new(tox_list *lst)
89{ 91{
90 lst->prev = lst->next = lst; 92 lst->prev = lst->next = lst;
91} 93}
92 94
93/* Inserts a new tox_lst after lst and returns it. */ 95/* Inserts a new tox_lst after lst and returns it. */
94static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst) 96static inline void tox_list_add(tox_list *lst, tox_list *new_lst)
95{ 97{
96 tox_list_new(new_lst); 98 tox_list_new(new_lst);
97 99
@@ -102,61 +104,60 @@ static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst)
102 new_lst->prev = lst; 104 new_lst->prev = lst;
103} 105}
104 106
105static inline void tox_list_remove(tox_list_t *lst) 107static inline void
108tox_list_remove(tox_list *lst)
106{ 109{
107 lst->prev->next = lst->next; 110 lst->prev->next = lst->next;
108 lst->next->prev = lst->prev; 111 lst->next->prev = lst->prev;
109} 112}
110 113
111/****************************Array*************************** 114/****************************Array***************************
112 * Array to store pointers which tracks it's own size. 115 * Array which manages its own memory allocation.
113 * TODO: Figure out if it shold store values instead of 116 * It stores copy of data (not pointers).
114 * pointers?
115 * TODO: Add wiki info usage. 117 * TODO: Add wiki info usage.
116 ************************************************************/ 118 ************************************************************/
117 119
118struct tox_array { 120typedef struct tox_array {
119 void **data; 121 void *data;
120 uint32_t size, length; 122 uint32_t len;
121}; 123 size_t elem_size; /* in bytes */
124} tox_array;
122 125
123static inline void tox_array_init(struct tox_array *arr) 126static inline void
127tox_array_init(tox_array *arr, size_t elem_size)
124{ 128{
125 arr->size = 1; 129 arr->len = 0;
126 arr->length = 0; 130 arr->elem_size = elem_size;
127 arr->data = malloc(sizeof(void *)); 131 arr->data = NULL;
128} 132}
129 133
130static inline void tox_array_delete(struct tox_array *arr) 134static inline void
135tox_array_delete(tox_array *arr)
131{ 136{
132 free(arr->data); 137 free(arr->data);
133 arr->size = arr->length = 0; 138 arr->len = arr->elem_size = 0;
134} 139}
135 140
136/* shrinks arr so it will not have unused space. If you want to have 141static inline void
137 * addtional space, extra species the amount of extra space. 142_tox_array_push(tox_array *arr, uint8_t *item)
138 */
139static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra)
140{
141 arr->size = arr->length + extra;
142 arr->data = realloc(arr->data, arr->size * sizeof(void *));
143}
144
145static inline void _tox_array_push(struct tox_array *arr, void *new)
146{ 143{
147 if (arr->length + 1 >= arr->size) 144 arr->data = realloc(arr->data, arr->elem_size * (arr->len+1));
148 tox_array_shrink_to_fit(arr, arr->size);
149 145
150 arr->data[arr->length++] = new; 146 memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
147 arr->len++;
151} 148}
152#define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) 149#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item)))
153 150
154static inline void *tox_array_pop(struct tox_array *arr) 151/* Deletes num items from array.
152 * Not same as pop in stacks, because to access elements you use data.
153 */
154static inline void
155tox_array_pop(tox_array *arr, uint32_t num)
155{ 156{
156 if (arr->length - 1 < arr->size / 4) 157 arr->len--;
157 tox_array_shrink_to_fit(arr, arr->length * 2); 158 arr->data = realloc(arr->data, arr->elem_size*arr->len);
158
159 return arr->data[arr->length--];
160} 159}
161 160
161#define tox_array_get(arr, i, type) ((type*)(arr)->data)[i]
162
162#endif // MISC_TOOLS_H 163#endif // MISC_TOOLS_H