ringbuf.c 7.67 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* ringbuf.c */

/* Synchronet ring buffer routines */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * 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.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

/* Pre-define RINGBUF_USE_STD_RTL to use standard C runtime library symbols
 * for malloc, free, and memcpy
 */

#ifdef VTOOLSD
#include <vtoolsd.h>
#include LOCKED_CODE_SEGMENT
#include LOCKED_DATA_SEGMENT
#endif

#include "ringbuf.h"
deuce's avatar
deuce committed
49
#include "genwrap.h"	/* SLEEP() */
50
51
52
53

#ifdef RINGBUF_USE_STD_RTL

	#ifndef VTOOLSD
54
55

	/* FreeBSD uses <stdlib.h> instead of <malloc.h> */
56
	#ifdef __unix__
57
58
59
		#include <stdlib.h>
	#else
		#include <malloc.h>		/* malloc prototype */
60
	#endif
61
62
	#include <string.h>			/* memcpy prototype */
    #endif	/* !VTOOLSD */
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

	#define os_malloc	malloc
	#define rb_malloc	malloc
	#define os_free 	free
	#define rb_free 	free
	#define os_memcpy	memcpy
	#define rb_memcpy	memcpy

#else

	void (*rb_free)(void *);
	void *(*rb_memcpy)(void *, const void *, size_t);

#endif
/****************************************************************************/
/* Returns 0 on success, non-zero on failure								*/
/****************************************************************************/
int RINGBUFCALL RingBufInit( RingBuf* rb, DWORD size
#ifndef RINGBUF_USE_STD_RTL
	,void *(os_malloc)(size_t)
	,void (os_free)(void *)
	,void *(os_memcpy)(void *, const void *, size_t)
#endif
	)
{
88
	memset(rb,0,sizeof(RingBuf));
89
	if((rb->pStart=(BYTE *)os_malloc(size+1))==NULL)
90
91
92
93
94
95
		return(-1);
#ifndef RINGBUF_USE_STD_RTL
	rb_free=os_free;
	rb_memcpy=os_memcpy;
#endif
	rb->pHead=rb->pTail=rb->pStart;
96
	rb->pEnd=rb->pStart+size;
97
    rb->size=size;
98
99
#ifdef RINGBUF_SEM
	sem_init(&rb->sem,0,0);
100
	sem_init(&rb->highwater_sem,0,0);
101
#endif
102
103
104
#ifdef RINGBUF_EVENT
	rb->empty_event=CreateEvent(NULL,TRUE,TRUE,NULL);
#endif
105
106
107
#ifdef RINGBUF_MUTEX
	pthread_mutex_init(&rb->mutex,NULL);
#endif
108
109
110
111
112
113
114
	return(0);
}

void RINGBUFCALL RingBufDispose( RingBuf* rb)
{
    if(rb->pStart!=NULL)
		os_free(rb->pStart);
115
#ifdef RINGBUF_SEM
116
	sem_post(&rb->sem);			/* just incase someone's waiting */
117
	while(sem_destroy(&rb->sem)==-1 && errno==EBUSY) {
deuce's avatar
deuce committed
118
119
120
		SLEEP(1);
		sem_post(&rb->sem);
	}
121
	while(sem_destroy(&rb->highwater_sem)==-1 && errno==EBUSY) {
deuce's avatar
deuce committed
122
123
124
		SLEEP(1);
		sem_post(&rb->highwater_sem);
	}
125
#endif
126
127
128
129
#ifdef RINGBUF_EVENT
	if(rb->empty_event!=NULL)
		CloseEvent(rb->empty_event);
#endif
130
#ifdef RINGBUF_MUTEX
131
	while(pthread_mutex_destroy(&rb->mutex)==EBUSY)
deuce's avatar
deuce committed
132
		SLEEP(1);
deuce's avatar
deuce committed
133
#endif
134
135
136
	memset(rb,0,sizeof(RingBuf));
}

137
138
#define RINGBUF_FILL_LEVEL(rb)	(rb->pHead >= rb->pTail ? (rb->pHead - rb->pTail) \
								: (rb->size - (rb->pTail - (rb->pHead + 1))))
139

140
141
DWORD RINGBUFCALL RingBufFull( RingBuf* rb )
{
142
	DWORD	retval;
143

144
145
146
147
#ifdef RINGBUF_MUTEX
	pthread_mutex_lock(&rb->mutex);
#endif

148
	retval = RINGBUF_FILL_LEVEL(rb);
149

150
151
152
153
#ifdef RINGBUF_MUTEX
	pthread_mutex_unlock(&rb->mutex);
#endif

154
155
156
157
158
159
160
	return(retval);
}

DWORD RINGBUFCALL RingBufFree( RingBuf* rb )
{
	DWORD retval;

161
	retval = (rb->size - RingBufFull( rb ));
162
163
164
165
166
167
168
169

	return(retval);
}

DWORD RINGBUFCALL RingBufWrite( RingBuf* rb, BYTE* src,  DWORD cnt )
{
	DWORD max, first, remain;

170
171
172
	if(cnt==0)
		return(cnt);

173
174
175
	if(rb->pStart==NULL)
		return(0);

176
177
178
179
#ifdef RINGBUF_MUTEX
	pthread_mutex_lock(&rb->mutex);
#endif

180
    /* allowed to write at pEnd */
181
	max = rb->pEnd - rb->pHead + 1;
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

	/*
	 * we assume the caller has checked that there is enough room. For this reason
	 * we do not have to worry about head wrapping past the tail
	 */

	if( max >= cnt ) {
		first = cnt;
		remain = 0;
	} else {
		first = max;
		remain = cnt - first;
	}

	rb_memcpy( rb->pHead, src, first );
	rb->pHead += first;
    src += first;

	if(remain) {

		rb->pHead = rb->pStart;
		rb_memcpy(rb->pHead, src, remain);
		rb->pHead += remain;
	}

    if(rb->pHead > rb->pEnd)
    	rb->pHead = rb->pStart;

210
211
#ifdef RINGBUF_SEM
	sem_post(&rb->sem);
212
	if(rb->highwater_mark!=0 && RINGBUF_FILL_LEVEL(rb)>=rb->highwater_mark)
213
		sem_post(&rb->highwater_sem);
214
#endif
215
216
217
218
219
#ifdef RINGBUF_EVENT
	if(rb->empty_event!=NULL)
		ResetEvent(rb->empty_event);
#endif

220
221
222
223
#ifdef RINGBUF_MUTEX
	pthread_mutex_unlock(&rb->mutex);
#endif

224
225
226
227
228
229
230
231
	return(cnt);
}

/* Pass NULL dst to just foward pointer (after Peek) */
DWORD RINGBUFCALL RingBufRead( RingBuf* rb, BYTE* dst,  DWORD cnt )
{
	DWORD max, first, remain, len;

232
233
234
235
#ifdef RINGBUF_MUTEX
	pthread_mutex_lock(&rb->mutex);
#endif

236
237
	len = RINGBUF_FILL_LEVEL(rb);

238
239
240
241
	if( len < cnt )
        cnt = len;

	/* allowed to read at pEnd */
242
	max = rb->pEnd - rb->pTail + 1;
243
244
245
246
247
248
249
250
251

	if( max >= cnt ) {
		first = cnt;
		remain = 0;
	} else {
		first = max;
		remain = cnt - first;
	}

252
    if(first && dst!=NULL) {
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
		rb_memcpy( dst, rb->pTail, first );
		dst += first;
    }
	rb->pTail += first;

	if( remain ){

		rb->pTail = rb->pStart;
        if(dst!=NULL)
			rb_memcpy( dst, rb->pTail, remain );
		rb->pTail += remain;
	}

    if(rb->pTail > rb->pEnd)
		rb->pTail = rb->pStart;

269
#ifdef RINGBUF_SEM		/* clear/signal semaphores, if appropriate */
270
	if(RINGBUF_FILL_LEVEL(rb) == 0)		/* empty */
271
		sem_reset(&rb->sem);
272
	if(RINGBUF_FILL_LEVEL(rb) < rb->highwater_mark)
273
		sem_reset(&rb->highwater_sem);
274
275
#endif

276
#ifdef RINGBUF_EVENT
277
	if(rb->empty_event!=NULL && RINGBUF_FILL_LEVEL(rb)==0)
278
279
280
		SetEvent(rb->empty_event);
#endif

281
282
283
284
#ifdef RINGBUF_MUTEX
	pthread_mutex_unlock(&rb->mutex);
#endif

285
286
287
288
289
290
291
292
293
294
295
	return(cnt);
}

DWORD RINGBUFCALL RingBufPeek( RingBuf* rb, BYTE* dst,  DWORD cnt)
{
	DWORD max, first, remain, len;

	len = RingBufFull( rb );
	if( len == 0 )
		return(0);

296
297
298
299
#ifdef RINGBUF_MUTEX
	pthread_mutex_lock(&rb->mutex);
#endif

300
301
302
303
	if( len < cnt )
        cnt = len;

    /* allowed to read at pEnd */
304
	max = rb->pEnd - rb->pTail + 1;
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

	if( max >= cnt ) {
		first = cnt;
		remain = 0;
	} else {
		first = max;
		remain = cnt - first;
	}

	rb_memcpy( dst, rb->pTail, first );
	dst += first;

	if(remain) {
		rb_memcpy( dst, rb->pStart, remain );
	}

321
322
323
324
#ifdef RINGBUF_MUTEX
	pthread_mutex_unlock(&rb->mutex);
#endif

325
326
327
328
329
330
	return(cnt);
}

/* Reset head and tail pointers */
void RINGBUFCALL RingBufReInit(RingBuf* rb)
{
331
332
333
#ifdef RINGBUF_MUTEX
	pthread_mutex_lock(&rb->mutex);
#endif
334
	rb->pHead = rb->pTail = rb->pStart;
335
336
337
338
#ifdef RINGBUF_SEM
	sem_reset(&rb->sem);
	sem_reset(&rb->highwater_sem);
#endif
339
340
341
#ifdef RINGBUF_MUTEX
	pthread_mutex_unlock(&rb->mutex);
#endif
342
343
344
}

/* End of RINGBUF.C */