annotate th_util.c @ 354:c01e42fc9adb

More work on SOCKS proxy support, should work now.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 23 Jun 2011 08:26:48 +0300
parents a6304a3719ee
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Generic utility-functions, macros and defaults
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
136
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
4 * (C) Copyright 2002-2010 Tecnic Software productions (TNSP)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #ifdef HAVE_CONFIG_H
2
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
9 #include "config.h"
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #endif
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include "th_util.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <stdio.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
20
323c98360d8b Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 17
diff changeset
14 /* Default settings
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 */
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
16 static BOOL th_initialized = FALSE;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
17 int th_verbosityLevel = 2;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
18 char *th_prog_name = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
19 *th_prog_fullname = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
20 *th_prog_version = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
21 *th_prog_author = NULL,
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
22 *th_prog_license = NULL;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
25 /* Initialize th_util-library and global variables
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 void th_init(char *progName, char *progFullName, char *progVersion,
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
28 char *progAuthor, char *progLicense)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
30 th_prog_name = progName;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
31 th_prog_fullname = progFullName;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
32 th_prog_version = progVersion;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
34 if (progAuthor)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
35 th_prog_author = progAuthor;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
36 else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
37 th_prog_author = TH_PROG_AUTHOR;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
38
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
39 if (progLicense)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
40 th_prog_license = progLicense;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
41 else
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
42 th_prog_license = TH_PROG_LICENSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
43
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
44 th_initialized = TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 /* Print formatted error, warning and information messages
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 * TODO: Implement th_vfprintf() and friends?
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 */
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
51 void THERR_V(const char *fmt, va_list ap)
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
52 {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
53 assert(th_initialized == TRUE);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
54
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
55 fprintf(stderr, "%s: ", th_prog_name);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
56 vfprintf(stderr, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
57 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
58
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
59
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
60 void THMSG_V(int level, const char *fmt, va_list ap)
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
61 {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
62 assert(th_initialized == TRUE);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
63
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
64 if (th_verbosityLevel >= level) {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
65 fprintf(stderr, "%s: ", th_prog_name);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
66 vfprintf(stderr, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
67 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
68 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
69
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
70
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
71 void THPRINT_V(int level, const char *fmt, va_list ap)
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
72 {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
73 assert(th_initialized == TRUE);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
74
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
75 if (th_verbosityLevel >= level) {
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
76 vfprintf(stderr, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
77 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
78 }
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
79
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
80
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
81 void THERR(const char *fmt, ...)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
83 va_list ap;
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
84 assert(th_initialized == TRUE);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
86 va_start(ap, fmt);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
87 THERR_V(fmt, ap);
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
88 va_end(ap);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
92 void THMSG(int level, const char *fmt, ...)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
94 va_list ap;
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
95 assert(th_initialized == TRUE);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
97 va_start(ap, fmt);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
98 THMSG_V(level, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
99 va_end(ap);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
103 void THPRINT(int level, const char *fmt, ...)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
105 va_list ap;
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
106 assert(th_initialized == TRUE);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107
99
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
108 va_start(ap, fmt);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
109 THPRINT_V(level, fmt, ap);
0313fabd8049 Added varargs versions of some functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 81
diff changeset
110 va_end(ap);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113
2
ecfa4e3597e3 Cleanups in th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
114 /* Memory handling routines
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 void *th_malloc(size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
118 return malloc(l);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 void *th_calloc(size_t n, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
124 return calloc(n, l);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 void *th_realloc(void *p, size_t l)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
130 return realloc(p, l);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 void th_free(void *p)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
136 /* Check for NULL pointers for portability due to some libc
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
137 * implementations not handling free(NULL) too well.
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
138 */
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
139 if (p) free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 #ifndef HAVE_MEMSET
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 void *th_memset(void *p, int c, size_t n)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 {
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
146 unsigned char *dp = (unsigned char *) p;
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
147
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
148 while (n--)
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
149 *(dp++) = c;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
81
69aed051f84d Synced th-libs.
Matti Hamalainen <ccr@tnsp.org>
parents: 42
diff changeset
151 return p;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 #endif
136
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
154
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
155
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
156 /* Doubly linked list handling
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
157 *
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
158 * In this implementation first node's prev points to last node of the list,
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
159 * and last node's next is NULL. This way we can semi-efficiently traverse to
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
160 * beginning and end of the list, assuming user does not do weird things.
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
161 */
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
162 qlist_t * th_llist_new(void *data)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
163 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
164 qlist_t *res = th_calloc(sizeof(qlist_t), 1);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
165 res->data = data;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
166 return res;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
167 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
168
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
169 void th_llist_free_func(qlist_t *list, void (*freefunc)(void *data))
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
170 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
171 qlist_t *curr = list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
172
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
173 while (curr != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
174 qlist_t *next = curr->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
175 if (freefunc != NULL && curr->data != NULL)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
176 freefunc(curr->data);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
177 th_free(curr);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
178 curr = next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
179 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
180 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
181
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
182
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
183 void th_llist_free(qlist_t *list)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
184 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
185 th_llist_free_func(list, NULL);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
186 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
187
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
188 void th_llist_append_node(qlist_t **list, qlist_t *node)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
189 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
190 if (*list != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
191 node->prev = (*list)->prev;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
192 (*list)->prev->next = node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
193 (*list)->prev = node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
194 (*list)->num++;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
195 } else {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
196 *list = node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
197 node->prev = *list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
198 (*list)->num = 1;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
199 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
200
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
201 node->next = NULL;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
202 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
203
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
204
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
205 qlist_t *th_llist_append(qlist_t **list, void *data)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
206 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
207 qlist_t *node = th_llist_new(data);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
208
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
209 th_llist_append_node(list, node);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
210
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
211 return node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
212 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
213
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
214
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
215 void th_llist_prepend_node(qlist_t **list, qlist_t *node)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
216 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
217 if (*list != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
218 node->prev = (*list)->prev;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
219 node->next = *list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
220 (*list)->prev = node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
221 node->num = (*list)->num + 1;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
222 *list = node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
223 } else {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
224 *list = node->prev = node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
225 node->next = NULL;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
226 (*list)->num = 1;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
227 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
228
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
229 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
230
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
231
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
232 qlist_t *th_llist_prepend(qlist_t **list, void *data)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
233 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
234 qlist_t *node = th_llist_new(data);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
235
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
236 th_llist_prepend_node(list, node);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
237
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
238 return node;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
239 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
240
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
241 /*
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
242 1) Remove a middle node
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
243
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
244 node0->prev->next = node->next (node1)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
245 node0->next->prev = node->prev (list)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
246
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
247 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
248 node2 <- list <=> node1 <=> node2 -> NULL
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
249
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
250 2) Remove first node when many items
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
251
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
252
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
253 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
254 node2 <- node0 <=> node1 <=> node2 -> NULL
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
255
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
256 *list = node0
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
257
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
258 3) Remove last node in list
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
259
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
260 if (node->next == NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
261 list->prev = node->prev;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
262 node->prev->next = NULL;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
263 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
264
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
265 node2 <- list <=> node0 <=> node1 <=> node2 -> NULL
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
266 node1 <- list <=> node0 <=> node1 -> NULL
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
267
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
268 4) Remove last
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
269
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
270 list <- list -> NULL
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
271
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
272
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
273 */
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
274 static void th_llist_delete_node_fast(qlist_t **list, qlist_t *node)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
275 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
276 if (node == *list) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
277 /* First node in list */
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
278 qlist_t *tmp = (*list)->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
279 if (tmp != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
280 tmp->num = (*list)->num - 1;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
281 tmp->prev = (*list)->prev;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
282 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
283 *list = tmp;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
284 } else {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
285 /* Somewhere in middle or end */
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
286 if (node->prev != NULL)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
287 node->prev->next = node->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
288
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
289 if (node->next != NULL)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
290 node->next->prev = node->prev;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
291 else
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
292 (*list)->prev = node; /* Last node */
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
293
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
294 (*list)->num--;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
295 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
296
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
297 node->next = node->prev = NULL;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
298 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
299
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
300
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
301 void th_llist_delete_node(qlist_t **list, qlist_t *node)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
302 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
303 qlist_t *curr = *list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
304
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
305 while (curr != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
306 qlist_t *next = curr->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
307 if (curr == node) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
308 th_llist_delete_node_fast(list, curr);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
309 th_free(node);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
310 break;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
311 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
312 curr = next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
313 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
314 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
315
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
316
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
317 void th_llist_delete(qlist_t **list, const void *data)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
318 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
319 qlist_t *curr = *list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
320
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
321 while (curr != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
322 qlist_t *next = curr->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
323 if (curr->data == data) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
324 th_llist_delete_node_fast(list, curr);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
325 th_free(curr);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
326 break;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
327 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
328 curr = next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
329 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
330 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
331
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
332
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
333 qlist_t * th_llist_get_nth(qlist_t *list, const size_t n)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
334 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
335 qlist_t *curr = list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
336 size_t i;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
337
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
338 for (i = 0; curr != NULL && i < n; curr = curr->next, i++);
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
339
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
340 return curr;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
341 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
342
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
343
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
344 size_t th_llist_length(const qlist_t *list)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
345 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
346 if (list == NULL)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
347 return 0;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
348 else
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
349 return list->num;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
350 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
351
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
352
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
353 ssize_t th_llist_position(const qlist_t *list, const qlist_t *node)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
354 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
355 const qlist_t *curr = list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
356 ssize_t i = 0;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
357
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
358 while (curr != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
359 if (curr == node)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
360 return i;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
361 else
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
362 i++;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
363
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
364 curr = curr->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
365 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
366
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
367 return -1;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
368 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
369
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
370
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
371 qlist_t * th_llist_find(qlist_t *list, const void *data)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
372 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
373 qlist_t *curr = list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
374
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
375 while (curr != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
376 if (curr->data == data)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
377 return curr;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
378 curr = curr->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
379 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
380
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
381 return NULL;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
382 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
383
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
384
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
385 qlist_t * th_llist_find_func(qlist_t *list, const void *userdata, int (compare)(const void *, const void *))
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
386 {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
387 qlist_t *curr = list;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
388
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
389 while (curr != NULL) {
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
390 if (compare(curr->data, userdata) == 0)
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
391 return curr;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
392 curr = curr->next;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
393 }
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
394
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
395 return NULL;
4ec36204d34e Add doubly linked list handling functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 99
diff changeset
396 }
276
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
397
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
398
281
8ae649a4b738 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 276
diff changeset
399 /*
8ae649a4b738 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 276
diff changeset
400 * Ringbuffers
8ae649a4b738 Comment.
Matti Hamalainen <ccr@tnsp.org>
parents: 276
diff changeset
401 */
276
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
402 qringbuf_t * th_ringbuf_new(const size_t size, void (*mdeallocator)(void *data))
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
403 {
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
404 qringbuf_t *res = th_calloc(1, sizeof(qringbuf_t));
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
405
306
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
406 res->data = (char **) th_calloc(size, sizeof(char *));
276
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
407 res->size = size;
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
408 res->n = 0;
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
409 res->deallocator = mdeallocator;
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
410
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
411 return res;
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
412 }
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
413
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
414
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
415 BOOL th_ringbuf_grow(qringbuf_t *buf, const size_t n)
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
416 {
306
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
417 buf->data = (char **) th_realloc(buf->data, (buf->size + n) * sizeof(char *));
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
418 if (buf->data != NULL) {
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
419 memset(buf->data + buf->size, 0, sizeof(char *) * n);
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
420 buf->size += n;
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
421 return TRUE;
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
422 } else
21528fe535fb Don't use void pointers in the ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 284
diff changeset
423 return FALSE;
276
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
424 }
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
425
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
426
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
427 void th_ringbuf_free(qringbuf_t *buf)
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
428 {
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
429 int i;
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
430
307
a6304a3719ee Free every item in the ringbuffer instead of just up to N.
Matti Hamalainen <ccr@tnsp.org>
parents: 306
diff changeset
431 for (i = 0; i < buf->size; i++)
283
18e292701db4 Change ringbuffer functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
432 if (buf->data[i] != NULL)
18e292701db4 Change ringbuffer functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
433 buf->deallocator(buf->data[i]);
276
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
434
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
435 th_free(buf->data);
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
436 th_free(buf);
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
437 }
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
438
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
439
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
440 void th_ringbuf_add(qringbuf_t *buf, void *ptr)
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
441 {
283
18e292701db4 Change ringbuffer functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
442 if (buf->n < buf->size)
276
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
443 buf->n++;
283
18e292701db4 Change ringbuffer functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
444
18e292701db4 Change ringbuffer functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
445 th_free(buf->data[0]);
284
dbf07a4eadd7 Grave bugfix in ringbuffer implementation!
Matti Hamalainen <ccr@tnsp.org>
parents: 283
diff changeset
446 memmove(&(buf->data[0]), &(buf->data[1]), (buf->size - 1) * sizeof(void *));
283
18e292701db4 Change ringbuffer functionality.
Matti Hamalainen <ccr@tnsp.org>
parents: 281
diff changeset
447 buf->data[buf->size - 1] = ptr;
276
1807059fb8f2 Move ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 136
diff changeset
448 }