comparison th_util.c @ 40:5b1e38a41bac

Cosmetic cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 02 Oct 2011 23:58:42 +0300
parents 06a72c643460
children e031a062b731
comparison
equal deleted inserted replaced
39:0d55592e0e01 40:5b1e38a41bac
150 150
151 return p; 151 return p;
152 } 152 }
153 #endif 153 #endif
154 154
155
156 /* Doubly linked list handling 155 /* Doubly linked list handling
157 * 156 *
158 * In this implementation first node's prev points to last node of the list, 157 * In this implementation first node's prev points to last node of the list,
159 * and last node's next is NULL. This way we can semi-efficiently traverse to 158 * and last node's next is NULL. This way we can semi-efficiently traverse to
160 * beginning and end of the list, assuming user does not do weird things. 159 * beginning and end of the list, assuming user does not do weird things.
168 167
169 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data)) 168 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data))
170 { 169 {
171 qlist_t *curr = list; 170 qlist_t *curr = list;
172 171
173 while (curr != NULL) { 172 while (curr != NULL)
173 {
174 qlist_t *next = curr->next; 174 qlist_t *next = curr->next;
175 if (freefunc != NULL && curr->data != NULL) 175 if (freefunc != NULL && curr->data != NULL)
176 freefunc(curr->data); 176 freefunc(curr->data);
177 th_free(curr); 177 th_free(curr);
178 curr = next; 178 curr = next;
185 th_llist_free_func(list, NULL); 185 th_llist_free_func(list, NULL);
186 } 186 }
187 187
188 void th_llist_append_node(qlist_t **list, qlist_t *node) 188 void th_llist_append_node(qlist_t **list, qlist_t *node)
189 { 189 {
190 if (*list != NULL) { 190 if (*list != NULL)
191 {
191 node->prev = (*list)->prev; 192 node->prev = (*list)->prev;
192 (*list)->prev->next = node; 193 (*list)->prev->next = node;
193 (*list)->prev = node; 194 (*list)->prev = node;
194 (*list)->num++; 195 (*list)->num++;
195 } else { 196 }
197 else
198 {
196 *list = node; 199 *list = node;
197 node->prev = *list; 200 node->prev = *list;
198 (*list)->num = 1; 201 (*list)->num = 1;
199 } 202 }
200 203
212 } 215 }
213 216
214 217
215 void th_llist_prepend_node(qlist_t **list, qlist_t *node) 218 void th_llist_prepend_node(qlist_t **list, qlist_t *node)
216 { 219 {
217 if (*list != NULL) { 220 if (*list != NULL)
221 {
218 node->prev = (*list)->prev; 222 node->prev = (*list)->prev;
219 node->next = *list; 223 node->next = *list;
220 (*list)->prev = node; 224 (*list)->prev = node;
221 node->num = (*list)->num + 1; 225 node->num = (*list)->num + 1;
222 *list = node; 226 *list = node;
223 } else { 227 }
228 else
229 {
224 *list = node->prev = node; 230 *list = node->prev = node;
225 node->next = NULL; 231 node->next = NULL;
226 (*list)->num = 1; 232 (*list)->num = 1;
227 } 233 }
228 234
271 277
272 278
273 */ 279 */
274 static void th_llist_delete_node_fast(qlist_t **list, qlist_t *node) 280 static void th_llist_delete_node_fast(qlist_t **list, qlist_t *node)
275 { 281 {
276 if (node == *list) { 282 if (node == *list)
283 {
277 /* First node in list */ 284 /* First node in list */
278 qlist_t *tmp = (*list)->next; 285 qlist_t *tmp = (*list)->next;
279 if (tmp != NULL) { 286 if (tmp != NULL)
287 {
280 tmp->num = (*list)->num - 1; 288 tmp->num = (*list)->num - 1;
281 tmp->prev = (*list)->prev; 289 tmp->prev = (*list)->prev;
282 } 290 }
283 *list = tmp; 291 *list = tmp;
284 } else { 292 }
293 else
294 {
285 /* Somewhere in middle or end */ 295 /* Somewhere in middle or end */
286 if (node->prev != NULL) 296 if (node->prev != NULL)
287 node->prev->next = node->next; 297 node->prev->next = node->next;
288 298
289 if (node->next != NULL) 299 if (node->next != NULL)
300 310
301 void th_llist_delete_node(qlist_t **list, qlist_t *node) 311 void th_llist_delete_node(qlist_t **list, qlist_t *node)
302 { 312 {
303 qlist_t *curr = *list; 313 qlist_t *curr = *list;
304 314
305 while (curr != NULL) { 315 while (curr != NULL)
316 {
306 qlist_t *next = curr->next; 317 qlist_t *next = curr->next;
307 if (curr == node) { 318 if (curr == node)
319 {
308 th_llist_delete_node_fast(list, curr); 320 th_llist_delete_node_fast(list, curr);
309 th_free(node); 321 th_free(node);
310 break; 322 break;
311 } 323 }
312 curr = next; 324 curr = next;
316 328
317 void th_llist_delete(qlist_t **list, const void *data) 329 void th_llist_delete(qlist_t **list, const void *data)
318 { 330 {
319 qlist_t *curr = *list; 331 qlist_t *curr = *list;
320 332
321 while (curr != NULL) { 333 while (curr != NULL)
334 {
322 qlist_t *next = curr->next; 335 qlist_t *next = curr->next;
323 if (curr->data == data) { 336 if (curr->data == data)
337 {
324 th_llist_delete_node_fast(list, curr); 338 th_llist_delete_node_fast(list, curr);
325 th_free(curr); 339 th_free(curr);
326 break; 340 break;
327 } 341 }
328 curr = next; 342 curr = next;
353 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node) 367 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node)
354 { 368 {
355 const qlist_t *curr = list; 369 const qlist_t *curr = list;
356 ssize_t i = 0; 370 ssize_t i = 0;
357 371
358 while (curr != NULL) { 372 while (curr != NULL)
373 {
359 if (curr == node) 374 if (curr == node)
360 return i; 375 return i;
361 else 376 else
362 i++; 377 i++;
363 378
370 385
371 qlist_t * th_llist_find(qlist_t *list, const void *data) 386 qlist_t * th_llist_find(qlist_t *list, const void *data)
372 { 387 {
373 qlist_t *curr = list; 388 qlist_t *curr = list;
374 389
375 while (curr != NULL) { 390 while (curr != NULL)
391 {
376 if (curr->data == data) 392 if (curr->data == data)
377 return curr; 393 return curr;
378 curr = curr->next; 394 curr = curr->next;
379 } 395 }
380 396
384 400
385 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *)) 401 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *))
386 { 402 {
387 qlist_t *curr = list; 403 qlist_t *curr = list;
388 404
389 while (curr != NULL) { 405 while (curr != NULL)
406 {
390 if (compare(curr->data, userdata) == 0) 407 if (compare(curr->data, userdata) == 0)
391 return curr; 408 return curr;
392 curr = curr->next; 409 curr = curr->next;
393 } 410 }
394 411
413 430
414 431
415 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n) 432 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n)
416 { 433 {
417 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *)); 434 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *));
418 if (buf->data != NULL) { 435 if (buf->data != NULL)
419 memset(buf->data + buf->size, 0, sizeof(char *) * n); 436 {
437 th_memset(buf->data + buf->size, 0, sizeof(char *) * n);
420 buf->size += n; 438 buf->size += n;
421 return TRUE; 439 return TRUE;
422 } else 440 } else
423 return FALSE; 441 return FALSE;
424 } 442 }