File: boundedqueue.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (180 lines) | stat: -rw-r--r-- 4,096 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
/*
 * Copyright (c) 1997-1999 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */

/*
 * a bounded queue implementation of the oskit_queue interface
 */
#include <oskit/com/queue.h>
#include <oskit/com/listener.h>
#include <oskit/c/assert.h>
#include <oskit/c/malloc.h>
#include <oskit/c/stdlib.h>
#include <oskit/c/string.h>
#include <oskit/c/assert.h>

typedef struct queue {
	oskit_queue_t	ioi;
	unsigned	count;
	void		*array;
	int		head, tail;
	int		itemsize;
	int		qlen, maxqlen;
	int		droplast;
	oskit_listener_t *l;
} queue_t;

static OSKIT_COMDECL
queue_query(oskit_queue_t *s, const oskit_iid_t *iid, void **out_ihandle)
{
	queue_t *q = (void *)s;

	assert(q && q->count);

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_queue_iid, sizeof(*iid)) == 0) {
                *out_ihandle = &q->ioi;
                ++q->count;
                return 0;
        }

        *out_ihandle = NULL;
        return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U
queue_addref(oskit_queue_t *s)
{
	queue_t *q = (void *)s;

        assert(q->count);

        return ++q->count;
}

static OSKIT_COMDECL_U
queue_release(oskit_queue_t *s)
{
	queue_t *q = (void *)s;
        unsigned newcount;

	assert(q && q->count);

        newcount = q->count - 1;

        if (newcount == 0) {
		if (q->l)
			oskit_listener_release(q->l);

		free(q->array);
                free(q);
                return 0;
        }

        return q->count = newcount;
}

static OSKIT_COMDECL_U
queue_dequeue(oskit_queue_t *s, void *item)
{
	queue_t *q = (void *)s;

	/* make sure queue is not empty... */
	assert(q->head != q->tail);

	memcpy(item, q->array + q->tail * q->itemsize, q->itemsize);
	q->tail = (q->tail + 1) % (q->maxqlen + 1);
	q->qlen--;

	return q->itemsize;
}

static OSKIT_COMDECL
queue_enqueue(oskit_queue_t *s, const void *item, oskit_size_t size)
{
	queue_t *q = (void *)s;
	oskit_error_t	rc = 0;

	/* Is this queue is full -> tell listener
	 * that I'm about to dump an entry
	 */
	if (q->qlen == q->maxqlen) {
		if (q->l)
			oskit_listener_notify(q->l, (oskit_iunknown_t *)s);

		/* if nothing has happened, then dump it, for christ's sake */
		if (q->qlen == q->maxqlen) {
			if (q->droplast) {
				q->tail = (q->tail + 1) % (q->maxqlen + 1);
				q->qlen--;
			} else
				return OSKIT_ENOMEM;
		}
	}

	memcpy(q->array + q->head * q->itemsize, item,
		q->itemsize < size ? q->itemsize : size);
	q->head = (q->head + 1) % (q->maxqlen + 1);
	q->qlen++;
	return rc;
}

static OSKIT_COMDECL_U
queue_size(oskit_queue_t *s)
{
	return ((queue_t *)s)->qlen;
}

static OSKIT_COMDECL_U
queue_front(oskit_queue_t *s, void *item)
{
	return OSKIT_E_NOTIMPL;
}

static struct oskit_queue_ops oskit_queue_ops = {
	queue_query,
	queue_addref,
	queue_release,
	queue_enqueue,
	queue_dequeue,
	queue_size,
	queue_front
};

oskit_queue_t *
create_bounded_queue_with_fixed_size_items(int qlen,
		oskit_size_t 	itemsize,
		oskit_listener_t *l,
		int	droplast)
{
	queue_t *q = malloc(sizeof *q);
	if (!q)
		return NULL;

	memset (q, 0, sizeof *q);
	q->maxqlen = qlen;
	q->itemsize = itemsize;
	if ((q->l = l) != NULL)
		oskit_listener_addref(l);

	q->array = calloc(qlen + 1, itemsize);

	q->ioi.ops = &oskit_queue_ops;
	q->count = 1;
	q->droplast = droplast;

	return &q->ioi;
}