comparison base/RingBuffer.h @ 1429:48e9f538e6e9

Untabify
author Chris Cannam
date Thu, 01 Mar 2018 18:02:22 +0000
parents 6a94bb528e9d
children 66c1988fc906
comparison
equal deleted inserted replaced
1428:87ae75da6527 1429:48e9f538e6e9
208 #endif 208 #endif
209 209
210 delete[] m_readers; 210 delete[] m_readers;
211 211
212 if (m_mlocked) { 212 if (m_mlocked) {
213 MUNLOCK((void *)m_buffer, m_size * sizeof(T)); 213 MUNLOCK((void *)m_buffer, m_size * sizeof(T));
214 } 214 }
215 delete[] m_buffer; 215 delete[] m_buffer;
216 } 216 }
217 217
218 template <typename T, int N> 218 template <typename T, int N>
291 int 291 int
292 RingBuffer<T, N>::getWriteSpace() const 292 RingBuffer<T, N>::getWriteSpace() const
293 { 293 {
294 int space = 0; 294 int space = 0;
295 for (int i = 0; i < N; ++i) { 295 for (int i = 0; i < N; ++i) {
296 int here = (m_readers[i] + m_size - m_writer - 1) % m_size; 296 int here = (m_readers[i] + m_size - m_writer - 1) % m_size;
297 if (i == 0 || here < space) space = here; 297 if (i == 0 || here < space) space = here;
298 } 298 }
299 299
300 #ifdef DEBUG_RINGBUFFER 300 #ifdef DEBUG_RINGBUFFER
301 int rs(getReadSpace()), rp(m_readers[0]); 301 int rs(getReadSpace()), rp(m_readers[0]);
302 302
303 std::cerr << "RingBuffer: write space " << space << ", read space " 303 std::cerr << "RingBuffer: write space " << space << ", read space "
304 << rs << ", total " << (space + rs) << ", m_size " << m_size << std::endl; 304 << rs << ", total " << (space + rs) << ", m_size " << m_size << std::endl;
305 std::cerr << "RingBuffer: reader " << rp << ", writer " << m_writer << std::endl; 305 std::cerr << "RingBuffer: reader " << rp << ", writer " << m_writer << std::endl;
306 #endif 306 #endif
307 307
308 #ifdef DEBUG_RINGBUFFER 308 #ifdef DEBUG_RINGBUFFER
309 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::getWriteSpace(): " << space << std::endl; 309 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::getWriteSpace(): " << space << std::endl;
321 #endif 321 #endif
322 322
323 int available = getReadSpace(R); 323 int available = getReadSpace(R);
324 if (n > available) { 324 if (n > available) {
325 #ifdef DEBUG_RINGBUFFER 325 #ifdef DEBUG_RINGBUFFER
326 std::cerr << "WARNING: Only " << available << " samples available" 326 std::cerr << "WARNING: Only " << available << " samples available"
327 << std::endl; 327 << std::endl;
328 #endif 328 #endif
329 memset(destination + available, 0, (n - available) * sizeof(T)); 329 memset(destination + available, 0, (n - available) * sizeof(T));
330 n = available; 330 n = available;
331 } 331 }
332 if (n == 0) return n; 332 if (n == 0) return n;
333 333
334 int here = m_size - m_readers[R]; 334 int here = m_size - m_readers[R];
335 if (here >= n) { 335 if (here >= n) {
336 memcpy(destination, m_buffer + m_readers[R], n * sizeof(T)); 336 memcpy(destination, m_buffer + m_readers[R], n * sizeof(T));
337 } else { 337 } else {
338 memcpy(destination, m_buffer + m_readers[R], here * sizeof(T)); 338 memcpy(destination, m_buffer + m_readers[R], here * sizeof(T));
339 memcpy(destination + here, m_buffer, (n - here) * sizeof(T)); 339 memcpy(destination + here, m_buffer, (n - here) * sizeof(T));
340 } 340 }
341 341
342 MBARRIER(); 342 MBARRIER();
343 m_readers[R] = (m_readers[R] + n) % m_size; 343 m_readers[R] = (m_readers[R] + n) % m_size;
344 344
358 #endif 358 #endif
359 359
360 int available = getReadSpace(R); 360 int available = getReadSpace(R);
361 if (n > available) { 361 if (n > available) {
362 #ifdef DEBUG_RINGBUFFER 362 #ifdef DEBUG_RINGBUFFER
363 std::cerr << "WARNING: Only " << available << " samples available" 363 std::cerr << "WARNING: Only " << available << " samples available"
364 << std::endl; 364 << std::endl;
365 #endif 365 #endif
366 n = available; 366 n = available;
367 } 367 }
368 if (n == 0) return n; 368 if (n == 0) return n;
369 369
370 int here = m_size - m_readers[R]; 370 int here = m_size - m_readers[R];
371 371
372 if (here >= n) { 372 if (here >= n) {
373 for (int i = 0; i < n; ++i) { 373 for (int i = 0; i < n; ++i) {
374 destination[i] += (m_buffer + m_readers[R])[i]; 374 destination[i] += (m_buffer + m_readers[R])[i];
375 } 375 }
376 } else { 376 } else {
377 for (int i = 0; i < here; ++i) { 377 for (int i = 0; i < here; ++i) {
378 destination[i] += (m_buffer + m_readers[R])[i]; 378 destination[i] += (m_buffer + m_readers[R])[i];
379 } 379 }
380 for (int i = 0; i < (n - here); ++i) { 380 for (int i = 0; i < (n - here); ++i) {
381 destination[i + here] += m_buffer[i]; 381 destination[i + here] += m_buffer[i];
382 } 382 }
383 } 383 }
384 384
385 MBARRIER(); 385 MBARRIER();
386 m_readers[R] = (m_readers[R] + n) % m_size; 386 m_readers[R] = (m_readers[R] + n) % m_size;
387 return n; 387 return n;
395 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::readOne(" << R << ")" << std::endl; 395 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::readOne(" << R << ")" << std::endl;
396 #endif 396 #endif
397 397
398 if (m_writer == m_readers[R]) { 398 if (m_writer == m_readers[R]) {
399 #ifdef DEBUG_RINGBUFFER 399 #ifdef DEBUG_RINGBUFFER
400 std::cerr << "WARNING: No sample available" 400 std::cerr << "WARNING: No sample available"
401 << std::endl; 401 << std::endl;
402 #endif 402 #endif
403 T t; 403 T t;
404 memset(&t, 0, sizeof(T)); 404 memset(&t, 0, sizeof(T));
405 return t; 405 return t;
406 } 406 }
407 T value = m_buffer[m_readers[R]]; 407 T value = m_buffer[m_readers[R]];
408 MBARRIER(); 408 MBARRIER();
409 if (++m_readers[R] == m_size) m_readers[R] = 0; 409 if (++m_readers[R] == m_size) m_readers[R] = 0;
410 return value; 410 return value;
419 #endif 419 #endif
420 420
421 int available = getReadSpace(R); 421 int available = getReadSpace(R);
422 if (n > available) { 422 if (n > available) {
423 #ifdef DEBUG_RINGBUFFER 423 #ifdef DEBUG_RINGBUFFER
424 std::cerr << "WARNING: Only " << available << " samples available" 424 std::cerr << "WARNING: Only " << available << " samples available"
425 << std::endl; 425 << std::endl;
426 #endif 426 #endif
427 memset(destination + available, 0, (n - available) * sizeof(T)); 427 memset(destination + available, 0, (n - available) * sizeof(T));
428 n = available; 428 n = available;
429 } 429 }
430 if (n == 0) return n; 430 if (n == 0) return n;
431 431
432 int here = m_size - m_readers[R]; 432 int here = m_size - m_readers[R];
433 if (here >= n) { 433 if (here >= n) {
434 memcpy(destination, m_buffer + m_readers[R], n * sizeof(T)); 434 memcpy(destination, m_buffer + m_readers[R], n * sizeof(T));
435 } else { 435 } else {
436 memcpy(destination, m_buffer + m_readers[R], here * sizeof(T)); 436 memcpy(destination, m_buffer + m_readers[R], here * sizeof(T));
437 memcpy(destination + here, m_buffer, (n - here) * sizeof(T)); 437 memcpy(destination + here, m_buffer, (n - here) * sizeof(T));
438 } 438 }
439 439
440 #ifdef DEBUG_RINGBUFFER 440 #ifdef DEBUG_RINGBUFFER
441 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::peek: read " << n << std::endl; 441 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::peek: read " << n << std::endl;
442 #endif 442 #endif
452 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::peek(" << R << ")" << std::endl; 452 std::cerr << "RingBuffer<T," << N << ">[" << this << "]::peek(" << R << ")" << std::endl;
453 #endif 453 #endif
454 454
455 if (m_writer == m_readers[R]) { 455 if (m_writer == m_readers[R]) {
456 #ifdef DEBUG_RINGBUFFER 456 #ifdef DEBUG_RINGBUFFER
457 std::cerr << "WARNING: No sample available" 457 std::cerr << "WARNING: No sample available"
458 << std::endl; 458 << std::endl;
459 #endif 459 #endif
460 T t; 460 T t;
461 memset(&t, 0, sizeof(T)); 461 memset(&t, 0, sizeof(T));
462 return t; 462 return t;
463 } 463 }
464 T value = m_buffer[m_readers[R]]; 464 T value = m_buffer[m_readers[R]];
465 return value; 465 return value;
466 } 466 }
467 467
474 #endif 474 #endif
475 475
476 int available = getReadSpace(R); 476 int available = getReadSpace(R);
477 if (n > available) { 477 if (n > available) {
478 #ifdef DEBUG_RINGBUFFER 478 #ifdef DEBUG_RINGBUFFER
479 std::cerr << "WARNING: Only " << available << " samples available" 479 std::cerr << "WARNING: Only " << available << " samples available"
480 << std::endl; 480 << std::endl;
481 #endif 481 #endif
482 n = available; 482 n = available;
483 } 483 }
484 if (n == 0) return n; 484 if (n == 0) return n;
485 m_readers[R] = (m_readers[R] + n) % m_size; 485 m_readers[R] = (m_readers[R] + n) % m_size;
486 return n; 486 return n;
487 } 487 }
495 #endif 495 #endif
496 496
497 int available = getWriteSpace(); 497 int available = getWriteSpace();
498 if (n > available) { 498 if (n > available) {
499 #ifdef DEBUG_RINGBUFFER 499 #ifdef DEBUG_RINGBUFFER
500 std::cerr << "WARNING: Only room for " << available << " samples" 500 std::cerr << "WARNING: Only room for " << available << " samples"
501 << std::endl; 501 << std::endl;
502 #endif 502 #endif
503 n = available; 503 n = available;
504 } 504 }
505 if (n == 0) return n; 505 if (n == 0) return n;
506 506
507 int here = m_size - m_writer; 507 int here = m_size - m_writer;
508 if (here >= n) { 508 if (here >= n) {
509 memcpy(m_buffer + m_writer, source, n * sizeof(T)); 509 memcpy(m_buffer + m_writer, source, n * sizeof(T));
510 } else { 510 } else {
511 memcpy(m_buffer + m_writer, source, here * sizeof(T)); 511 memcpy(m_buffer + m_writer, source, here * sizeof(T));
512 memcpy(m_buffer, source + here, (n - here) * sizeof(T)); 512 memcpy(m_buffer, source + here, (n - here) * sizeof(T));
513 } 513 }
514 514
515 MBARRIER(); 515 MBARRIER();
516 m_writer = (m_writer + n) % m_size; 516 m_writer = (m_writer + n) % m_size;
517 517
531 #endif 531 #endif
532 532
533 int available = getWriteSpace(); 533 int available = getWriteSpace();
534 if (n > available) { 534 if (n > available) {
535 #ifdef DEBUG_RINGBUFFER 535 #ifdef DEBUG_RINGBUFFER
536 std::cerr << "WARNING: Only room for " << available << " samples" 536 std::cerr << "WARNING: Only room for " << available << " samples"
537 << std::endl; 537 << std::endl;
538 #endif 538 #endif
539 n = available; 539 n = available;
540 } 540 }
541 if (n == 0) return n; 541 if (n == 0) return n;
542 542
543 int here = m_size - m_writer; 543 int here = m_size - m_writer;
544 if (here >= n) { 544 if (here >= n) {
545 memset(m_buffer + m_writer, 0, n * sizeof(T)); 545 memset(m_buffer + m_writer, 0, n * sizeof(T));
546 } else { 546 } else {
547 memset(m_buffer + m_writer, 0, here * sizeof(T)); 547 memset(m_buffer + m_writer, 0, here * sizeof(T));
548 memset(m_buffer, 0, (n - here) * sizeof(T)); 548 memset(m_buffer, 0, (n - here) * sizeof(T));
549 } 549 }
550 550
551 MBARRIER(); 551 MBARRIER();
552 m_writer = (m_writer + n) % m_size; 552 m_writer = (m_writer + n) % m_size;
553 return n; 553 return n;