diff options
Diffstat (limited to 'testing/misc_tools.h')
-rw-r--r-- | testing/misc_tools.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/testing/misc_tools.h b/testing/misc_tools.h index e50a8f7f..cc5abeda 100644 --- a/testing/misc_tools.h +++ b/testing/misc_tools.h | |||
@@ -173,4 +173,52 @@ static inline void tox_list_remove(tox_list_t* lst) { | |||
173 | lst->next->prev = lst->prev; | 173 | lst->next->prev = lst->prev; |
174 | } | 174 | } |
175 | 175 | ||
176 | /****************************Array*************************** | ||
177 | * Array to store pointers which tracks it's own size. | ||
178 | * TODO: Figure out if it shold store values instead of | ||
179 | * pointers? | ||
180 | ************************************************************/ | ||
181 | |||
182 | struct tox_array { | ||
183 | void **data; | ||
184 | uint32_t size, length; | ||
185 | }; | ||
186 | |||
187 | static inline void tox_array_init(struct tox_array *arr) | ||
188 | { | ||
189 | arr->size = 1; | ||
190 | arr->length = 0; | ||
191 | arr->data = malloc(sizeof(void*)); | ||
192 | } | ||
193 | |||
194 | static inline void tox_array_delete(struct tox_array *arr) | ||
195 | { | ||
196 | free(arr->data); | ||
197 | arr->size = arr->length = 0; | ||
198 | } | ||
199 | |||
200 | /* shrinks arr so it will not have unused space. If you want to have | ||
201 | * addtional space, extra species the amount of extra space. | ||
202 | */ | ||
203 | static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra) | ||
204 | { | ||
205 | arr->size = arr->length + extra; | ||
206 | arr->data = realloc(arr->data, arr->size * sizeof(void*)); | ||
207 | } | ||
208 | |||
209 | static inline void _tox_array_push(struct tox_array *arr, void *new) | ||
210 | { | ||
211 | if (arr->length+1 >= arr->size) | ||
212 | tox_array_shrink_to_fit(arr, arr->size); | ||
213 | arr->data[arr->length++] = new; | ||
214 | } | ||
215 | #define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) | ||
216 | |||
217 | static inline void* tox_array_pop(struct tox_array *arr) | ||
218 | { | ||
219 | if (arr->length-1 < arr->size/4) | ||
220 | tox_array_shrink_to_fit(arr, arr->length*2); | ||
221 | return arr->data[arr->length--]; | ||
222 | } | ||
223 | |||
176 | #endif // MISC_TOOLS_H | 224 | #endif // MISC_TOOLS_H |