Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/socketio #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix of overflow error in Ringbuffer and test cleanup
  • Loading branch information
ponast committed Mar 28, 2021
commit ff60f3088e93eb697a5b50c5c2598a721dd4b7b5
29 changes: 13 additions & 16 deletions ringbuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace jnk0le

if (head_trunc > tail_trunc)
return head_trunc - tail_trunc;
if (head_trunc < tail_trunc || tmp_head > tmp_tail)
if (head_trunc < tail_trunc || isFull())
return buffer_size - tail_trunc;
return 0;
}
Expand All @@ -134,10 +134,10 @@ namespace jnk0le
index_t head_trunc = tmp_head & buffer_mask;
index_t tail_trunc = tmp_tail & buffer_mask;

if (head_trunc > tail_trunc || tmp_head == tmp_tail)
return buffer_size - head_trunc;
if (head_trunc < tail_trunc)
return tail_trunc - head_trunc;
if (head_trunc > tail_trunc || isEmpty())
return buffer_size - head_trunc;
return 0;
}

Expand Down Expand Up @@ -238,21 +238,22 @@ namespace jnk0le
}

/*!
* \brief Bulk insert accounts for already performed inserts from external
* function writeBuff
* \param cnt Number of elements to insert
* \return Number of inserted elements
* \brief Bulk insert accounts for inserts performed via
* external function writeBuff
* \param cnt Number of elements inserted
* \return Number of actually inserted elements as per
* buffer capacity
*/
void bulk_insert(size_t cnt) {
index_t bulkInsert(size_t cnt) {
index_t tmp_head = head.load(std::memory_order_relaxed);
index_t tmp_tail = tail.load(std::memory_order_relaxed);

// It is an error to insert more elements than
// available. Thus if inserted not equal cnt below the
// ringbuffer is trashed.
// available. If so truncate insert.
size_t inserted = cnt < writeAvailable() ? cnt : writeAvailable();
tmp_head += inserted;
head.store(tmp_head, index_release_barrier);
return inserted;
}

/*!
Expand Down Expand Up @@ -308,11 +309,7 @@ namespace jnk0le
*/
T* space() {
index_t tmp_head = head.load(std::memory_order_relaxed);

if( tmp_head - tail.load(index_acquire_barrier) == buffer_size )
return nullptr;
else
return &data_buff[tmp_head & buffer_mask];
return isFull() ? nullptr : &data_buff[tmp_head & buffer_mask];
}

/*!
Expand Down Expand Up @@ -563,7 +560,7 @@ namespace jnk0le
if (res < 0)
return res;
total += static_cast<unsigned>(res);
buff.bulk_insert(res);
buff.bulkInsert(res);
}
return static_cast<int>(total);
}
Expand Down
46 changes: 40 additions & 6 deletions test.cpp → test_posix_api.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#include <algorithm>
#include <iostream>
#include <cassert>
Expand All @@ -18,12 +16,17 @@ int test_read( int fd, void * data, size_t length )
{
size_t ret = std::min( bufsize, unsigned(length));
uint8_t * iter = static_cast<uint8_t*>(data);
std::cout << "Writing " << ret << " bytes into ring buffer." << std::endl;
std::cout << "Writing " << ret << " bytes into ring buffer: ";
std::cout << "{ ";
if ( ret > 0 )
std::cout << char(fd);
for ( uint8_t i = 0; i < ret; ++i )
{
* iter = static_cast<uint8_t>(fd);
std::cout << ", " << char(fd);
iter++;
}
std::cout << " }\n";
bufsize -= ret;
return ret;
}
Expand All @@ -32,12 +35,22 @@ int test_read( int fd, void * data, size_t length )
int test_write( int fd, void const * data, size_t length )
{
size_t ret = std::min( bufsize, unsigned(length));
std::cout << "Reading " << ret << " bytes from ring buffer" << std::endl;
std::cout << "Reading " << ret << " bytes from ring buffer: ";
std::cout << "{ ";
uint8_t * beg = (uint8_t*)data;
uint8_t * end = beg + ret;
if ( beg < end )
std::cout << *beg++;
for ( uint8_t * i = beg; i < end; ++i )
{
std::cout << ", " << *i;
}
std::cout << " }\n";
bufsize -= ret;
return ret;
}

/* Print ringbuffer ontent for debug purposes */
/* Print ringbuffer content for debug purposes */
template<typename RB>
void print_ringbuf(RB & rb )
{
Expand Down Expand Up @@ -65,7 +78,8 @@ int main()
std::cout << "Ring_Buff: ";
print_ringbuf(buf);
assert( nwrite == 16);

assert( buf.writeAvailableContinous() == 0);
assert( buf.readAvailableContinous() == 16);
assert( buf.readAvailable() == 16);

std::cout << "\nRead 16 elements from buffer. Empties buffer completely\n";
Expand Down Expand Up @@ -112,6 +126,26 @@ int main()
print_ringbuf(buf);
assert( nwrite == 10);
std::cout << "buf.readAvailable() = " << buf.readAvailable() << std::endl;

std::cout << "\nReading 11 elements from buffer. This requires two accesses as the sequence not is continous\n";
bufsize = 11;
nread = readBuff( buf, test_write, 'f' );
std::cout << "data_buff: ";
buf.print();
std::cout << "Ring_Buff: ";
print_ringbuf(buf);
assert( nread == 11);
std::cout << "buf.readAvailable() = " << buf.readAvailable() << std::endl;

std::cout << "\nReading 10 elements from buffer. Should only give us one element\n";
bufsize = 10;
nread = readBuff( buf, test_write, 'g' );
std::cout << "data_buff: ";
buf.print();
std::cout << "Ring_Buff: ";
print_ringbuf(buf);
assert( nread == 1);
std::cout << "buf.readAvailable() = " << buf.readAvailable() << std::endl;
}