~vkolesnikov/pbxt/pbxt-preload-test-bug

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 *
 * PrimeBase XT
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * 2005-02-04	Paul McCullagh
 *
 * H&G2JCtL
 */

#include "xt_config.h"

#include "pthread_xt.h"
#include "thread_xt.h"
#include "sortedlist_xt.h"

XTSortedListPtr xt_new_sortedlist_ns(u_int item_size, u_int grow_size, XTCompareFunc comp_func, void *thunk, XTFreeFunc free_func)
{
	XTSortedListPtr sl;

	if (!(sl = (XTSortedListPtr) xt_calloc_ns(sizeof(XTSortedListRec))))
		return NULL;
	sl->sl_item_size = item_size;
	sl->sl_grow_size = grow_size;
	sl->sl_comp_func = comp_func;
	sl->sl_thunk = thunk;
	sl->sl_free_func = free_func;
	sl->sl_current_size = 0;
	return sl;
}

XTSortedListPtr xt_new_sortedlist(XTThreadPtr self, u_int item_size, u_int initial_size, u_int grow_size, XTCompareFunc comp_func, void *thunk, XTFreeFunc free_func, xtBool with_lock, xtBool with_cond)
{
	XTSortedListPtr sl;

	sl = (XTSortedListPtr) xt_calloc(self, sizeof(XTSortedListRec));
	xt_init_sortedlist(self, sl, item_size, initial_size, grow_size, comp_func, thunk, free_func, with_lock, with_cond);
	return sl;
}

xtPublic void xt_init_sortedlist(XTThreadPtr self, XTSortedListPtr sl, u_int item_size, u_int initial_size, u_int grow_size, XTCompareFunc comp_func, void *thunk, XTFreeFunc free_func, xtBool with_lock, xtBool with_cond)
{
	sl->sl_item_size = item_size;
	sl->sl_grow_size = grow_size;
	sl->sl_comp_func = comp_func;
	sl->sl_thunk = thunk;
	sl->sl_free_func = free_func;
	sl->sl_current_size = initial_size;

	if (initial_size) {
		try_(a) {
			sl->sl_data = (char *) xt_malloc(self, initial_size * item_size);
		}
		catch_(a) {
			xt_free(self, sl);
			throw_();
		}
		cont_(a);
	}

	if (with_lock || with_cond) {
		sl->sl_lock = (xt_mutex_type *) xt_calloc(self, sizeof(xt_mutex_type));
		try_(b) {
			xt_init_mutex_with_autoname(self, sl->sl_lock);
		}
		catch_(b) {
			xt_free(self, sl->sl_lock);
			sl->sl_lock = NULL;
			xt_free_sortedlist(self, sl);
			throw_();
		}
		cont_(b);
	}

	if (with_cond) {
		sl->sl_cond = (xt_cond_type *) xt_calloc(self, sizeof(xt_cond_type));
		try_(c) {
			xt_init_cond(self, sl->sl_cond);
		}
		catch_(c) {
			xt_free(self, sl->sl_cond);
			sl->sl_cond = NULL;
			xt_free_sortedlist(self, sl);
			throw_();
		}
		cont_(c);
	}
}

xtPublic void xt_empty_sortedlist(XTThreadPtr self, XTSortedListPtr sl)
{
	if (sl->sl_lock)
		xt_lock_mutex(self, sl->sl_lock);
	if (sl->sl_data) {
		while (sl->sl_usage_count > 0) {
			sl->sl_usage_count--;
			if (sl->sl_free_func)
				(*sl->sl_free_func)(self, sl->sl_thunk, &sl->sl_data[sl->sl_usage_count * sl->sl_item_size]);
		}
	}
	if (sl->sl_lock)
		xt_unlock_mutex(self, sl->sl_lock);
}

xtPublic void xt_free_sortedlist(XTThreadPtr self, XTSortedListPtr sl)
{
	xt_empty_sortedlist(self, sl);
	if (sl->sl_data) {
		xt_free(self, sl->sl_data);
		sl->sl_data = NULL;
	}
	if (sl->sl_lock) {
		xt_free_mutex(sl->sl_lock);
		xt_free(self, sl->sl_lock);
	}
	if (sl->sl_cond) {
		xt_free_cond(sl->sl_cond);
		xt_free(self, sl->sl_cond);
	}
	xt_free(self, sl);
}

xtPublic void *xt_sl_find(XTThreadPtr self, XTSortedListPtr sl, void *key)
{
	void	*result;
	size_t	idx;

	if (sl->sl_usage_count == 0)
		return NULL;
	else if (sl->sl_usage_count == 1) {
		if ((*sl->sl_comp_func)(self, sl->sl_thunk, key, sl->sl_data) == 0)
			return sl->sl_data;
		return NULL;
	}
	result = xt_bsearch(self, key, sl->sl_data, sl->sl_usage_count, sl->sl_item_size, &idx, sl->sl_thunk, sl->sl_comp_func);
	return result;
}

/*
 * Returns:
 * 1 = Value inserted.
 * 2 = Value not inserted, already in the list.
 * 0 = An error occurred.
 */
xtPublic int xt_sl_insert(XTThreadPtr self, XTSortedListPtr sl, void *key, void *data)
{
	size_t idx;

	if (sl->sl_usage_count == 0)
		idx = 0;
	else if (sl->sl_usage_count == 1) {
		int r;

		if ((r = (*sl->sl_comp_func)(self, sl->sl_thunk, key, sl->sl_data)) == 0) {
			if (sl->sl_free_func)
				(*sl->sl_free_func)(self, sl->sl_thunk, data);
			return 2;
		}
		if (r < 0)
			idx = 0;
		else
			idx = 1;
	}
	else {
		if (xt_bsearch(self, key, sl->sl_data, sl->sl_usage_count, sl->sl_item_size, &idx, sl->sl_thunk, sl->sl_comp_func)) {
			if (sl->sl_free_func)
				(*sl->sl_free_func)(self, sl->sl_thunk, data);
			return 2;
		}
	}
	if (sl->sl_usage_count == sl->sl_current_size) {		
		if (!xt_realloc_ns((void **) &sl->sl_data, (sl->sl_current_size + sl->sl_grow_size) * sl->sl_item_size)) {
			if (sl->sl_free_func)
				(*sl->sl_free_func)(self, sl->sl_thunk, data);
			if (self)
				xt_throw(self);
			return 0;
		}
		sl->sl_current_size = sl->sl_current_size + sl->sl_grow_size;
	}
	XT_MEMMOVE(sl->sl_data, &sl->sl_data[(idx+1) * sl->sl_item_size], &sl->sl_data[idx * sl->sl_item_size], (sl->sl_usage_count-idx) * sl->sl_item_size);
	XT_MEMCPY(sl->sl_data, &sl->sl_data[idx * sl->sl_item_size], data, sl->sl_item_size);
	sl->sl_usage_count++;
	return 1;
}

xtPublic xtBool xt_sl_delete(XTThreadPtr self, XTSortedListPtr sl, void *key)
{
	void	*result;
	size_t	idx;
	
	if (sl->sl_usage_count == 0)
		return FALSE;
	if (sl->sl_usage_count == 1) {
		if ((*sl->sl_comp_func)(self, sl->sl_thunk, key, sl->sl_data) != 0)
			return FALSE;
		idx = 0;
		result = sl->sl_data;
	}
	else {
		if (!(result = xt_bsearch(self, key, sl->sl_data, sl->sl_usage_count, sl->sl_item_size, &idx, sl->sl_thunk, sl->sl_comp_func)))
			return FALSE;
	}
	if (sl->sl_free_func)
		(*sl->sl_free_func)(self, sl->sl_thunk, result);
	sl->sl_usage_count--;
	XT_MEMMOVE(sl->sl_data, &sl->sl_data[idx * sl->sl_item_size], &sl->sl_data[(idx+1) * sl->sl_item_size], (sl->sl_usage_count-idx) * sl->sl_item_size);
	return TRUE;
}

xtPublic void xt_sl_delete_item_at(struct XTThread *self, XTSortedListPtr sl, size_t idx)
{
	void *result;

	if (idx >= sl->sl_usage_count)
		return;
	result = &sl->sl_data[idx * sl->sl_item_size];
	if (sl->sl_free_func)
		(*sl->sl_free_func)(self, sl->sl_thunk, result);
	sl->sl_usage_count--;
	XT_MEMMOVE(sl->sl_data, &sl->sl_data[idx * sl->sl_item_size], &sl->sl_data[(idx+1) * sl->sl_item_size], (sl->sl_usage_count-idx) * sl->sl_item_size);
}

xtPublic void xt_sl_remove_from_front(struct XTThread *XT_UNUSED(self), XTSortedListPtr sl, size_t items)
{
	if (sl->sl_usage_count <= items)
		xt_sl_set_size(sl, 0);
	else {
		XT_MEMMOVE(sl->sl_data, sl->sl_data, &sl->sl_data[items * sl->sl_item_size], (sl->sl_usage_count-items) * sl->sl_item_size);
		sl->sl_usage_count -= items;
	}
}

xtPublic void xt_sl_delete_from_info(XTThreadPtr self, XTSortedListInfoPtr li_undo)
{	
	xt_sl_delete(self, li_undo->li_sl, li_undo->li_key);
}

xtPublic size_t xt_sl_get_size(XTSortedListPtr sl)
{
	return sl->sl_usage_count;
}

xtPublic void xt_sl_set_size(XTSortedListPtr sl, size_t new_size)
{
	sl->sl_usage_count = new_size;
	if (sl->sl_usage_count + sl->sl_grow_size <= sl->sl_current_size) {
		size_t curr_size;

		curr_size = sl->sl_usage_count;
		if (curr_size < sl->sl_grow_size)
			curr_size = sl->sl_grow_size;

		if (xt_realloc(NULL, (void **) &sl->sl_data, curr_size * sl->sl_item_size))
			sl->sl_current_size = curr_size;
	}
}

xtPublic void *xt_sl_item_at(XTSortedListPtr sl, size_t idx)
{
	if (idx < sl->sl_usage_count)
		return &sl->sl_data[idx * sl->sl_item_size];
	return NULL;
}

xtPublic void *xt_sl_last_item(XTSortedListPtr sl)
{
	if (sl->sl_usage_count > 0)
		return xt_sl_item_at(sl, sl->sl_usage_count - 1);
	return NULL;
}

xtPublic void *xt_sl_first_item(XTSortedListPtr sl)
{
	if (sl->sl_usage_count > 0)
		return xt_sl_item_at(sl, 0);
	return NULL;
}

xtPublic xtBool xt_sl_lock(XTThreadPtr self, XTSortedListPtr sl)
{
	xtBool r = OK;
	
	if (sl->sl_locker != self)
		r = xt_lock_mutex(self, sl->sl_lock);
	if (r) {
		sl->sl_locker = self;
		sl->sl_lock_count++;
	}
	return r;
}

xtPublic void xt_sl_unlock(XTThreadPtr self, XTSortedListPtr sl)
{
	ASSERT(!self || sl->sl_locker == self);
	ASSERT(sl->sl_lock_count > 0);

	sl->sl_lock_count--;
	if (!sl->sl_lock_count) {
		sl->sl_locker = NULL;
		xt_unlock_mutex(self, sl->sl_lock);
	}
}

xtPublic void xt_sl_lock_ns(XTSortedListPtr sl, XTThreadPtr thread)
{
	if (sl->sl_locker != thread)
		xt_lock_mutex_ns(sl->sl_lock);
	sl->sl_locker = thread;
	sl->sl_lock_count++;
}

xtPublic void xt_sl_unlock_ns(XTSortedListPtr sl)
{
	ASSERT_NS(!sl->sl_locker || sl->sl_locker == xt_get_self());
	ASSERT_NS(sl->sl_lock_count > 0);

	sl->sl_lock_count--;
	if (!sl->sl_lock_count) {
		sl->sl_locker = NULL;
		xt_unlock_mutex_ns(sl->sl_lock);
	}
}

xtPublic void xt_sl_wait(XTThreadPtr self, XTSortedListPtr sl)
{
	xt_wait_cond(self, sl->sl_cond, sl->sl_lock);
}

xtPublic xtBool xt_sl_signal(XTThreadPtr self, XTSortedListPtr sl)
{
	return xt_signal_cond(self, sl->sl_cond);
}

xtPublic void xt_sl_broadcast(XTThreadPtr self, XTSortedListPtr sl)
{
	xt_broadcast_cond(self, sl->sl_cond);
}