2010-11-26 15:12:47 +01:00
|
|
|
#include <cib.h>
|
2010-11-26 13:15:01 +01:00
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
void cib_init(cib_t *cib, unsigned int size)
|
|
|
|
{
|
2010-11-26 13:15:01 +01:00
|
|
|
cib->read_count = 0;
|
|
|
|
cib->write_count = 0;
|
2013-06-20 18:18:29 +02:00
|
|
|
cib->complement = 0 - size;
|
2010-11-26 13:15:01 +01:00
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int cib_avail(cib_t *cib)
|
|
|
|
{
|
|
|
|
return (int)(cib->write_count - cib->read_count);
|
2010-11-26 13:15:01 +01:00
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int cib_get(cib_t *cib)
|
|
|
|
{
|
|
|
|
int avail = cib_avail(cib);
|
2010-11-26 13:15:01 +01:00
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
if(avail > 0) {
|
|
|
|
return (int)(cib->read_count++ & ~cib->complement);
|
|
|
|
}
|
2010-11-26 13:15:01 +01:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
int cib_put(cib_t *cib)
|
|
|
|
{
|
|
|
|
int avail = cib_avail(cib);
|
2010-11-26 13:15:01 +01:00
|
|
|
|
2013-06-20 18:18:29 +02:00
|
|
|
if((int)(avail + cib->complement) < 0) {
|
|
|
|
return (int)(cib->write_count++ & ~(cib->complement));
|
2010-11-26 13:15:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
int main() {
|
|
|
|
cib_t cib;
|
|
|
|
|
|
|
|
cib_init(&cib, 0);
|
|
|
|
|
|
|
|
int res = cib_get(&cib);
|
|
|
|
|
|
|
|
printf("%i\n", res);
|
|
|
|
}
|
|
|
|
*/
|