File: pcb.h

package info (click to toggle)
exult 0.98rc1-2
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,924 kB
  • ctags: 8,928
  • sloc: cpp: 83,768; sh: 7,643; ansic: 4,328; makefile: 890; yacc: 618; lex: 255; xml: 19
file content (450 lines) | stat: -rw-r--r-- 8,761 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 *  Copyright (C) 2000-2001  The Exult Team
 *
 *  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 Library 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.
 */

#ifndef _PCB_H_
#define _PCB_H_

#ifndef ALPHA_LINUX_CXX
#  include <cstring>
#  include <iostream>
#endif
#include <new>
#include "SDL_mapping.h"
#include "common_types.h"

class ByteBuffer
{
  public:
	void	reserve(size_t numbytes)    // Reserve space in advance
		{
		if (!data)
			{
			data = new char[numbytes];

			low  = high = 0;
			cap  = numbytes;

			return;
			}

		// Sanity check. Do we already have at least that much?
		if (numbytes <= cap)
			{
			std::memmove(data, (data + low), (high - low));

			high -= low;
			low  =  0;

			return;
			}

		// Allocate new store
		char   *tmp = new char[numbytes];

		// Copy the old store into it.
		std::memcpy(tmp, (data + low), (high - low));

		// Reset all our offsets
		high -= low;
		low  =  0;
		cap  =  numbytes;

		// Replace the old store with the new one
		delete [] data;
		data = tmp;
		}

	ByteBuffer() : data(0), cap(0), low(0), high(0)
		{  } 
	ByteBuffer(const ByteBuffer &b) :  data(0), cap(0), low(0), high(0)
		{
		if (b.data)
			{
			reserve(b.cap);

			low  = 0;              // Is this line redundant?
			high = b.high - b.low;

			std::memcpy(data, (b.data + b.low), high);
			}

		} 

	virtual ~ByteBuffer()
		{
		if (data)
			{
			delete [] data;
			}
		}

	typedef char   *iterator;    // Define an STL-compatible iterator
    
	ByteBuffer   &operator =(const ByteBuffer &b)    // assignment operator
		{
		if (b.data)
			{
			reserve(b.cap);

			low  = 0;             // Not redundant here, I think.
			high = b.high - b.low;

			std::memcpy(data, (b.data + b.low), high);
			}
		else
			{
			if (data)
				{
				delete [] data;
				}

			low  = cap = high = 0;
			data = 0;
			}

		return *this;
		}

	char	&operator [](size_t pos)    // index operator
		{
		// Index operator. Return the character at pos.
		// NO RANGE CHECKING!

		return *(data + low + pos);
		}
    
	void	push_back(char c)    // Add-to-end of buffer
		{
		if (!data)
			{
			reserve(4096);    // An arbitrary sort of amount
			}
		else
			{
			// Check if we're getting close to the end
			if (high >= (cap - 16))
				{
				reserve(cap + 4096);
				}
			}
		data[high++] = c;
		}

	void	push_back(const char *buf, size_t len)    // add block to end of buffer
		{
		// If there is no buffer, reserve space for at least double
		// this amount
		if (!data)
			{
			reserve(len * 2);
			}
		else
			{
			// Will this chunk fit at the end with current capacity ?
			if(cap-high<len)
				{
				// Maybe if we reshuffle things a bit ?
				if(cap-high+low>len)
					{
						std::memmove(data, (data+low), (high-low));
						high -= low;
						low = 0;
					}
				else	// No luck...
					reserve(cap + (len * 2));
				}
			}

		std::memcpy((data + high), buf, len);

		high += len;
		}
					// Pull block off the front
	size_t	pop_front(char *buf, size_t len, bool repeat = false)
		{
		if (!data)
			return 0;

		if (len > (high - low))
			{
			len = high - low;
			}

		std::memcpy(buf, (data + low), len);

		low += len;

		if (low == high)
			{
			if (repeat)	// Rewind?
				low = 0;
			else		// All done.
				low = high = 0;
			}
		return len;
		}

	iterator	begin()         // return iterator pointing to 
					//   beginning of buffer
		{
		return (data + low);
		}

	iterator	end()   	// return iterator pointing to one 
					//   past end of buffer
		{
			return (data + high);
		}
#if 0
	void	erase(iterator pos)   // erase from the front 
                                     //  ONLY WORKS IF iterator == begin() !
		{
		if (!data)
			return;    // Perhaps we should throw a range_error instead?

		// The rest of this really doesn't work if pos != begin()
		if (pos == (data + low))
			{
			low++;

			// Is it empty now? If so, throw everything away
			// Perhaps we should change this to preserve a buffer
			// for speed considerations
			if (low == high)
				{
				low = high = cap = 0;

				delete [] data;

				data = 0;

				return;
				}
			}

		// We could optionally throw a range_error here, as well,
		// as the erase simply doesn't happen if we get here.

		}

	void          erase(iterator pos, size_t count)    // As above.
		{
		if (!data)
			return;    // Perhaps we should throw a range_error instead?

		// The rest of this really doesn't work if pos != begin()
		if (pos == (data + low))
			{
			while (count-- && (low != high))
				{
				low++;
				}

			// Is it empty now? If so, throw everything away
			// Perhaps we should change this to preserve a buffer
			// for speed considerations
			if (low == high)
				{
				low = high = cap = 0;

				delete [] data;

				data = 0;

				return;
				}
			}

		// We could optionally throw a range_error here, as well,
		// as the erase simply doesn't happen if we get here.
		}
#endif
	void          clear(void)           // wipe the whole buffer
		{
		low = high = cap = 0;

		if (data)
			{
			delete [] data;
			}

		data = 0;

		}
	size_t        size(void)            // return number of bytes currently buffered
		{
		return (high - low);
		}
    
  private:

    char     *data;
    size_t    cap,
              low,
              high;
};










#define	MAX_PCB_SIZE	8192


class	ProducerConsumerBuf
	{
private:
	static  uint32 sequence_cnt;	// For generating (mostly) unique ID's.
	ByteBuffer Buffer;
	SDL_mutex	*mutex;
	size_t	window;
	bool	producing,consuming;
	bool	repeat;			// Keep consuming forever.
	uint32	type;			// 'Magic' # identifying type.
	uint32  seq;			// Sequence # assigned.
	int	volume;				// 0-128.
	int	dir;			// Dir. (0-15 from N, clockwise) from
					//   observer to sound source.
	inline 	void	lock(void)
		{
#ifdef MACOS
		SDL_mutexP(mutex);
#else
		if(SDL_mutexP(mutex)!=0)
			std::cerr << "ProducerConsumerBuf::lock() failed" << std::endl;
#endif
		}
	inline 	void	unlock(void)
		{
		SDL_mutexV(mutex);
		}
public:
#ifdef DEBUG
	static	int	counter;
	int	mycounter;
#endif
	bool is_consuming() const { return consuming; }
	bool is_active() const
		{ return consuming || producing; }
	void	produce(const void *p,size_t l)
		{
		if(!l||!consuming)
			return;	// No data? Do nothing
		while(1)
			{
			lock();
			if(!consuming)
				{
				unlock();
				return;
				}
			size_t	n=Buffer.size();
			if(n>window)
				{
				unlock();
				SDL::Delay(100);
				}
			else
				break;
			}
		Buffer.push_back(reinterpret_cast<const char *>(p),l);
		unlock();
		}
	size_t	consume(void *p,size_t l)
		{
		if(!l)
			return producing?0:-1;
		lock();
		l=Buffer.pop_front(reinterpret_cast<char *>(p), l, repeat);
		unlock();
		return l?l:(producing?0:-1);
		}
	uint32 init(uint32 t)		// Initialize prior to use/reuse.
					// Returns sequence #.
		{
		type = t;
		lock();
		seq = ++sequence_cnt;
		volume = SDL_MIX_MAXVOLUME;
		dir = 0;
		producing = consuming = true;
		repeat = false;
		Buffer.clear();
		unlock();
		return seq;
		}
	int get_volume()
		{ return volume; }
	void set_volume(int v)		// Should be 0-128.
		{ volume = v; }
	int get_dir()
		{ return dir; }
	void set_dir(int d)		// Should be 0-15.
		{ dir = d; }
	void set_repeat(bool rep)
		{ repeat = rep; }
	ProducerConsumerBuf() : Buffer(),mutex(SDL_CreateMutex()),
			window(32768),producing(false),consuming(false),
			repeat(false),
			type(0), seq(0), volume(SDL_MIX_MAXVOLUME), dir(0)
		{
#ifdef DEBUG
		mycounter = counter++;
		COUT("Created PCB " << mycounter);
#endif
		 }
	~ProducerConsumerBuf()
		{
#ifdef DEBUG
		COUT("::"<<mycounter<<" ProducerConsumerBuf going away");
#endif
		SDL_DestroyMutex(mutex);
		}
	uint32	get_type(void) const
		{ return type; }
	uint32  get_seq(void) const
		{ return seq; }
	void	end_production(void)
		{
#ifdef DEBUG
		COUT("::" << mycounter << " end_production");
#endif
		lock();
		producing=false;
		unlock();
		}
	void	end_consumption(void)
		{
#if !defined(MACOS) && defined(DEBUG)
		// As always, no use of cerr etc. on MacOS during Audio Interrupt!
		COUT("::"<<mycounter<<" end_consumption");
#endif
		lock();
		consuming=false;
		unlock();
		}
	size_t	size(void) { return Buffer.size(); }
	};

#endif