File: queue.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (373 lines) | stat: -rw-r--r-- 7,963 bytes parent folder | download
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* -*-c-*- */
/* 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, see: <http://www.gnu.org/licenses/>
 */

/* ---------------------------- included header files ---------------------- */

#include "config.h"

#include <stdio.h>

#include "safemalloc.h"
#include "queue.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

typedef struct fqueue_record
{
	struct fqueue_record *next;
	void *object;
	struct
	{
		unsigned is_scheduled_for_deletion;
		unsigned is_just_created;
	} flags;
} fqueue_record;

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

/* ---------------------------- interface functions ------------------------ */

/* Make newly added items permanent and destroy items scheduled for deletion. */
static void fqueue_cleanup_queue(
	fqueue *fq, destroy_fqueue_object_t destroy_func)
{
	fqueue_record *head;
	fqueue_record *tail;
	fqueue_record *prev;
	fqueue_record *next;
	fqueue_record *rec;

	for (head = NULL, tail = NULL, prev = NULL, rec = fq->first;
	     rec != NULL; rec = next)
	{
		if (rec->flags.is_scheduled_for_deletion)
		{
			/* destroy and skip it */
			next = rec->next;
			if (rec->object != NULL && destroy_func != NULL)
			{
				destroy_func(rec->object);
			}
			if (prev != NULL)
			{
				prev->next = next;
			}
			free(rec);
		}
		else
		{
			rec->flags.is_just_created = 0;
			if (head == NULL)
			{
				head = rec;
			}
			tail = rec;
			prev = rec;
			next = rec->next;
		}
	}
	fq->first = head;
	fq->last = tail;

	return;
}

/* Recursively lock the queue.  While locked, objects are not deleted from the
 * queue but marked for later deletion.  New objects are marked as such and are
 * skipped by the queue functions. */
static void fqueue_lock_queue(fqueue *fq)
{
	fq->lock_level++;

	return;
}

/* Remove one lock level */
static void fqueue_unlock_queue(
	fqueue *fq, destroy_fqueue_object_t destroy_func)
{
	switch (fq->lock_level)
	{
	case 0:
		/* bug */
		break;
	case 1:
		if (fq->flags.is_dirty)
		{
			fqueue_cleanup_queue(fq, destroy_func);
		}
		/* fall through */
	default:
		fq->lock_level--;
		return;
	}

	return;
}

/* Chack and possibly execute the action associated with a queue object.
 * Schedule the object for deletion if it was executed. */
static void fqueue_operate(
	fqueue *fq, fqueue_record *rec,
	check_fqueue_object_t check_func,
	operate_fqueue_object_t operate_func,
	void *operate_args)
{
	if (rec == NULL || rec->flags.is_scheduled_for_deletion)
	{
		return;
	}
	if (check_func == NULL || check_func(rec->object, operate_args) == 1)
	{
		if (operate_func != NULL)
		{
			operate_func(rec->object, operate_args);
		}
		rec->flags.is_scheduled_for_deletion = 1;
		fq->flags.is_dirty = 1;
	}

	return;
}

/* ---------------------------- builtin commands --------------------------- */

/*
 * Basic queue management
 */

void fqueue_init(fqueue *fq)
{
	memset(fq, 0, sizeof(*fq));

	return;
}

unsigned int fqueue_get_length(fqueue *fq)
{
	unsigned int len;
	fqueue_record *t;

	for (t = fq->first, len = 0; t != NULL; t = t->next)
	{
		if (!t->flags.is_scheduled_for_deletion)
		{
			len++;
		}
	}

	return len;
}

/*
 * Add record to queue
 */

void fqueue_add_at_front(
	fqueue *fq, void *object)
{
	fqueue_record *rec;

	rec = (fqueue_record *)safemalloc(sizeof(fqueue_record));
	memset(rec, 0, sizeof(*rec));
	rec->object = object;
	rec->next = fq->first;
	if (fq->lock_level > 0)
	{
		rec->flags.is_just_created = 1;
		fq->flags.is_dirty = 1;
	}
	fq->first = rec;

	return;
}

void fqueue_add_at_end(
	fqueue *fq, void *object)
{
	fqueue_record *rec;

	rec = (fqueue_record *)safemalloc(sizeof(fqueue_record));
	memset(rec, 0, sizeof(*rec));
	rec->object = object;
	if (fq->lock_level > 0)
	{
		rec->flags.is_just_created = 1;
		fq->flags.is_dirty = 1;
	}
	if (fq->first == NULL)
	{
		fq->first = rec;
	}
	else
	{
		fq->last->next = rec;
	}
	fq->last = rec;
	rec->next = NULL;

	return;
}

void fqueue_add_inside(
	fqueue *fq, void *object, cmp_objects_t cmp_objects, void *cmp_args)
{
	fqueue_record *rec;
	fqueue_record *p;
	fqueue_record *t;

	rec = (fqueue_record *)safemalloc(sizeof(fqueue_record));
	memset(rec, 0, sizeof(*rec));
	rec->object = object;
	if (fq->lock_level > 0)
	{
		rec->flags.is_just_created = 1;
		fq->flags.is_dirty = 1;
	}

	/* search place to insert record */
	for (p = NULL, t = fq->first;
	     t != NULL && cmp_objects(object, t->object, cmp_args) >= 0;
	     p = t, t = t->next)
	{
		/* nothing to do here */
	}
	/* insert record */
	if (p == NULL)
	{
		/* insert at start */
		rec->next = fq->first;
		fq->first = rec;
	}
	else
	{
		/* insert after p */
		rec->next = p->next;
		p->next = rec;
	}
	if (t == NULL)
	{
		fq->last = rec;
	}

	return;
}

/*
 * Fetch queue objects
 */

/* Returns the object of the first queue record throuch *ret_object.  Returns
 * 0 if the queue is empty and 1 otherwise. */
int fqueue_get_first(
	fqueue *fq, void **ret_object)
{
	fqueue_record *rec;

	for (rec = fq->first;
	     rec != NULL && rec->flags.is_scheduled_for_deletion;
	     rec = rec->next)
	{
		/* nothing */
	}
	if (rec == NULL)
	{
		return 0;
	}
	*ret_object = rec->object;

	return 1;
}

/*
 * Operate on queue objects and possibly remove them from the queue
 */

/* Runs the operate_func on the first record in the queue.  If that function
 * is NULL or returns 1, the record is removed from the queue.  The object of
 * the queue record must have been freed in operate_func. */
void fqueue_remove_or_operate_from_front(
	fqueue *fq,
	check_fqueue_object_t check_func,
	operate_fqueue_object_t operate_func,
	destroy_fqueue_object_t destroy_func,
	void *operate_args)
{
	fqueue_lock_queue(fq);
	fqueue_operate(fq, fq->first, check_func, operate_func, operate_args);
	fqueue_unlock_queue(fq, destroy_func);

	return;
}

/* Same as above but operates on last record in queue */
void fqueue_remove_or_operate_from_end(
	fqueue *fq,
	check_fqueue_object_t check_func,
	operate_fqueue_object_t operate_func,
	destroy_fqueue_object_t destroy_func,
	void *operate_args)
{
	fqueue_lock_queue(fq);
	fqueue_operate( fq, fq->last, check_func, operate_func, operate_args);
	fqueue_unlock_queue(fq, destroy_func);

	return;
}

/* Same as above but operates on all records in the queue. */
void fqueue_remove_or_operate_all(
	fqueue *fq,
	check_fqueue_object_t check_func,
	operate_fqueue_object_t operate_func,
	destroy_fqueue_object_t destroy_func,
	void *operate_args)
{
	fqueue_record *t;

	if (fq->first == NULL)
	{
		return;
	}
	fqueue_lock_queue(fq);
	/* search record(s) to remove */
	for (t = fq->first; t != NULL; t = t->next)
	{
		if (t->flags.is_just_created ||
		    t->flags.is_scheduled_for_deletion)
		{
			/* skip */
			continue;
		}
		fqueue_operate(fq, t, check_func, operate_func, operate_args);
	}
	fqueue_unlock_queue(fq, destroy_func);

	return;
}