view intercom/cirbuf.cpp @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents 13be24d74cd2
children
line wrap: on
line source

/* cirbuf.cpp
 *
 * Copyright (C) DFS Deutsche Flugsicherung (2004, 2005). 
 * All Rights Reserved.
 *
 * Circular buffers
 *
 * Version 0.2
 */

#include <string.h>
#include <strings.h>

#include "oss.h"
#include "cirbuf.h"
#include "intercomd.h"

CIRBUF::CIRBUF()
{
  bzero(buf, CIRBUFSIZE);
  in = out = len = 0;
}

void CIRBUF::init()
{
  bzero(buf, CIRBUFSIZE);
  in = out = len = 0;
}

int CIRBUF::push(char *from, int size)
{
  memcpy(buf + in, from, size);
  in += size;
  if (in >= CIRBUFSIZE) {
    in -= CIRBUFSIZE;
  }
  len += size;
  if (len > CIRBUFSIZE) {
    int oversize = (((len - CIRBUFSIZE) / FRAGSIZE)) * FRAGSIZE;
    if (oversize < len - CIRBUFSIZE) {
      oversize += FRAGSIZE;
    }
    // delete oldest if overrun
    out += oversize;
    if (out >= CIRBUFSIZE) {
      out -= CIRBUFSIZE;
    }
    len -= oversize;
    return -oversize;
  } else {
    return OKAY;
  }
}

int CIRBUF::pop(char *to, int size)
{
  if (len < size) {
    // play out silence if underrun
    bzero(to, size);
    return ERROR;
  }
  memcpy(to, buf + out, size);
  out += size;
  if (out >= CIRBUFSIZE) {
    out -= CIRBUFSIZE;
  }
  len -= size;
  return OKAY;
}

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.