File: bufQueue.c

package info (click to toggle)
memchan 2.3%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: ansic: 3,320; sh: 990; tcl: 687; makefile: 48
file content (366 lines) | stat: -rw-r--r-- 6,695 bytes parent folder | download | duplicates (4)
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
/*
 * bufQueue.c --
 *
 *	Implementation of a queue out of buffers.
 *
 * Copyright (c) 2000 by Andreas Kupries <a.kupries@westend.com>
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: bufQueue.c,v 1.2 2002/04/25 06:29:48 andreas_kupries Exp $
 */

#include "buf.h"

/*
 * Internal structures used to hold the buffers in the queue.
 */

/*
 * Structure of a node in the queue.
 */

typedef struct QNode_ {
  Buf_Buffer     buf;     /* The buffer managed by the node */
  struct QNode_* nextPtr; /* Reference to the next node/buffer */
} QNode;

/*
 * Structure of the whole queue.
 */

typedef struct Queue_ {
  QNode*    firstNode;  /* Head of the queue */
  QNode*    lastNode;   /* Last node/buffer in the queue */
  int       size;       /* Number of bytes stored in the queue */
#if GT81
  Tcl_Mutex lock;       /* mutex to serialize access to the
			 * queue when more than one thread
			 * is trying to access it. */
#endif
} Queue;

/*
 * Declaration of size to use for new buffers when
 * extending the queue
 */

#define BUF_SIZE (1024)


/*
 *------------------------------------------------------*
 *
 *	Buf_NewQueue --
 *
 *	Creates a new, empty queue.
 *
 *	Sideeffects:
 *		Allocates and initializes memory.
 *
 *	Result:
 *		A queue token.
 *
 *------------------------------------------------------*
 */

Buf_BufferQueue
Buf_NewQueue ()
{
  Queue* q = (Queue*) Tcl_Alloc (sizeof (Queue));

  q->firstNode = (QNode*) NULL;
  q->lastNode  = (QNode*) NULL;
  q->size      = 0;
#if GT81
  q->lock      = (Tcl_Mutex) NULL;
#endif
  return (Buf_BufferQueue) q;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_FreeQueue --
 *
 *	Deletes the specified queue.
 *
 *	Sideeffects:
 *		Deallocates the memory which was
 *		allocated in Buf_NewQueue.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

void
Buf_FreeQueue (queue)
     Buf_BufferQueue queue;
{
  Queue* q = (Queue*) queue;
  QNode* n = q->firstNode;
  QNode* tmp;

#if GT81
  Tcl_MutexLock (&q->lock);
#endif

  while (n != (QNode*) NULL) {
    Buf_DecrRefcount (n->buf);
    tmp = n->nextPtr;
    Tcl_Free ((char*) n);
    n = tmp;
  }

#if GT81
  Tcl_MutexUnlock   (&q->lock);
  Tcl_MutexFinalize (&q->lock);
#endif
  Tcl_Free((char*) q);
  return;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_QueueRead --
 *
 *	Reads information from the queue. The read data
 *	is deleted from the queue.
 *
 *	Sideeffects:
 *		May deallocate memory. Moves the access
 *		pointer in the queue buffers.
 *
 *	Result:
 *		Returns the number of bytes actually read.
 *
 *------------------------------------------------------*
 */

int
Buf_QueueRead (queue, outbuf, size)
     Buf_BufferQueue queue;
     char*           outbuf;
     int             size;
{
  Queue* q = (Queue*) queue;
  QNode* n;
  int    got, read;

#if GT81
  Tcl_MutexLock (&q->lock);
#endif

  n = q->firstNode;

  if ((size <= 0) || (n == (QNode*) NULL)) {
#if GT81
    Tcl_MutexUnlock (&q->lock);
#endif
    return 0;
  }

  read = 0;
  while ((size > 0) && (n != (QNode*) NULL)) {
    got = Buf_Read (n->buf, outbuf, size);

    if (got > 0) {
      read   += got;
      outbuf += got;
      size   -= got;
    }

    if (size > 0) {
      Buf_DecrRefcount (n->buf);
      q->firstNode = n->nextPtr;
      Tcl_Free ((char*) n);
      n = q->firstNode;
    }
  }

  if (n == (QNode*) NULL) {
    q->lastNode = (QNode*) NULL;
  }

  q->size -= read;

#if GT81
  Tcl_MutexUnlock (&q->lock);
#endif

  return read;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_QueueWrite --
 *
 *	Writes information to the queue. The written data
 *	is appended at the end of the queue.
 *
 *	Sideeffects:
 *		May allocate memory. Moves the access
 *		pointer in the queue buffers.
 *
 *	Result:
 *		Returns the number of bytes actually written.
 *
 *------------------------------------------------------*
 */

int
Buf_QueueWrite (queue, inbuf, size)
Buf_BufferQueue queue;
CONST char*     inbuf;
int             size;
{
  Queue* q = (Queue*) queue;
  QNode* n;
  int    done, written;

  if ((size <= 0)) {
    return 0;
  }

#if GT81
  Tcl_MutexLock (&q->lock);
#endif

  n       = q->firstNode;
  written = 0;

  while (size > 0) {
    if (n == (QNode*) NULL) {
      n = (QNode*) Tcl_Alloc (sizeof (QNode));
      n->nextPtr = (QNode*) NULL;
      n->buf     = Buf_CreateFixedBuffer (BUF_SIZE);

      if (q->lastNode == (QNode*) NULL) {
	q->firstNode = n;
      } else {
	q->lastNode->nextPtr = n;
      }

      q->lastNode = n;
    }

    done = Buf_Write (n->buf, inbuf, size);

    if (done > 0) {
      written += done;
      inbuf   += done;
      size    -= done;
    }
    if (size > 0) {
      n = (QNode*) NULL;
    }
  }

  q->size += written;

#if GT81
  Tcl_MutexUnlock (&q->lock);
#endif

  return written;
}

/*
 *------------------------------------------------------*
 *
 *	BufQueue_Append --
 *
 *	Appends a range containing the information
 *	not yet read from the specified buffer to the queue.
 *
 *	Sideeffects:
 *		Creates a range buffer, allocates memory.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

void
Buf_QueueAppend (queue, buf)
     Buf_BufferQueue queue;
     Buf_Buffer      buf;
{
  /* Not the buffer is appended, but a range containing
   * the rest of the data to read from it.
   *
   * Allows external usage of the buffer without affecting
   * the queue. Writing (s.a.) is no problem, as ranges
   * always return that nothing was written and thus force
   * the system to append a new fixed-size buffer behind them.
   */

  Queue* q = (Queue*) queue;
  QNode* n;

#if GT81
  Tcl_MutexLock (&q->lock);
#endif

  buf = Buf_CreateRange (buf, Buf_Size (buf));

  n = (QNode*) Tcl_Alloc (sizeof (QNode));
  n->nextPtr = (QNode*) NULL;
  n->buf     = buf;

  if (q->lastNode == (QNode*) NULL) {
    q->firstNode = n;
  } else {
    q->lastNode->nextPtr = n;
  }

  q->lastNode = n;

  q->size += Buf_Size (buf);

#if GT81
  Tcl_MutexUnlock (&q->lock);
#endif
  return;
}

/*
 *------------------------------------------------------*
 *
 *	BufQueue_Size --
 *
 *	Returns the current number of bytes stored in the queue.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

int
Buf_QueueSize (queue)
     Buf_BufferQueue queue;
{
  Queue* q = (Queue*) queue;
  int size;

#if GT81
  Tcl_MutexLock (&q->lock);
#endif

  size = q->size;

#if GT81
  Tcl_MutexUnlock (&q->lock);
#endif
  return size;
}