circular-buffer

Ring buffer with atomic indexes

半世苍凉 提交于 2021-02-19 08:45:38
问题 I have struggled with what must be a fundamental misunderstanding of how atomics work in C++. I have written the code below to implement a fast ring buffer using atomic variables for indexes so multiple threads can write to and read from the buffer. I've whittled the code down to this simple case (which I realize is still a little long. Sorry.). If I run this on either Linux or Mac OS X, it will work some of the time, but it will also throw exceptions at least 10% of the time. It also seems

circular_buffer and managed_mapped_file segmentation fault

一世执手 提交于 2021-02-05 08:08:42
问题 I am using boost 1.73.0, and am trying to use circular_buffer together with manage_mapped_file to store strings in a circular buffer persisted on disk. I do the following to create/open the circular_buffer: boost::interprocess::managed_mapped_file mmf(boost::interprocess::open_or_create, "./circ_buffer.bin", 10u << 10); typedef boost::interprocess::allocator<std::string, boost::interprocess::managed_mapped_file::segment_manager> string_allocator; typedef boost::circular_buffer<std::string,

Sending data with PACKET_MMAP and PACKET_TX_RING is slower than “normal” (without)

纵饮孤独 提交于 2020-11-30 06:21:30
问题 I’m writing a traffic generator in C using the PACKET_MMAP socket option to create a ring buffer to send data over a raw socket. The ring buffer is filled with Ethernet frames to send and sendto is called. The entire contents of the ring buffer is sent over the socket which should give higher performance than having a buffer in memory and calling sendto repeatedly for every frame in the buffer that needs sending. When not using PACKET_MMAP, upon calling sendto a single frame is copied from

Sending data with PACKET_MMAP and PACKET_TX_RING is slower than “normal” (without)

戏子无情 提交于 2020-11-30 06:19:31
问题 I’m writing a traffic generator in C using the PACKET_MMAP socket option to create a ring buffer to send data over a raw socket. The ring buffer is filled with Ethernet frames to send and sendto is called. The entire contents of the ring buffer is sent over the socket which should give higher performance than having a buffer in memory and calling sendto repeatedly for every frame in the buffer that needs sending. When not using PACKET_MMAP, upon calling sendto a single frame is copied from

How to efficiently wrap the index of a fixed-size circular buffer

风流意气都作罢 提交于 2020-04-29 09:51:16
问题 I have a fixed size circular buffer (implemented as an array): upon initialization, the buffer gets filled with the specified maximum number of elements which allows the use of a single position index in order to keep track of our current position in the circle. What is an efficient way to access an element in the circular buffer? Here is my current solution: int GetElement(int index) { if (index >= buffer_size || index < 0) { // some code to handle the case } else { // wrap the index index =

How do you iterate backward over circular buffer without a conditional?

萝らか妹 提交于 2020-01-22 17:33:28
问题 Iterating forward through a circular buffer without using a conditional is easy with the remainder operator... iterator = (iterator + 1) % buffer_size; I can't for the life of me figure out the reverse operation, iterating backward. 回答1: Does iterator = (iterator + buffer_size - 1) % buffer_size work for you? Go one less than all the way around. 回答2: Borealid's answer works. (note: iterator is set to 0 initially). Another solution is iterator = buffer_size - 1 - (buffer_size - iterator) %

How do you iterate backward over circular buffer without a conditional?

拜拜、爱过 提交于 2020-01-22 17:33:25
问题 Iterating forward through a circular buffer without using a conditional is easy with the remainder operator... iterator = (iterator + 1) % buffer_size; I can't for the life of me figure out the reverse operation, iterating backward. 回答1: Does iterator = (iterator + buffer_size - 1) % buffer_size work for you? Go one less than all the way around. 回答2: Borealid's answer works. (note: iterator is set to 0 initially). Another solution is iterator = buffer_size - 1 - (buffer_size - iterator) %

Getting data from pointer in struct “Invalid read/write”

偶尔善良 提交于 2020-01-15 10:03:56
问题 I am trying to do a implementation of circular buffer in array. I keep my data in structure and manage it by few methods as push, pop, etc. The program is more or less functional and behave as expected, however I run into errors in my valgrind test. And I am not capable of finding out what is wrong with my code. Although it seems like managing data via pointers in my struct is the crucial problem. I would be very grateful if anyone could point me in the right direction coz I am really lost at

Shifting/aligning/rotating a circular buffer to zero in-place

我与影子孤独终老i 提交于 2020-01-11 18:01:51
问题 I'm using a circular buffer to push data onto either end of a list. After I'm done I want to align the buffer so the first element in the list is at position zero and can be used like a regular array without any fancy indexing overhead. So I have my circular list with capacity N , it has n elements starting at arbitrary index f . What's the fastest way to shift/rotate all the elements such that f = 0 ? The catch is I want to do this in-place (though of course some registers/temporaries will

Shifting/aligning/rotating a circular buffer to zero in-place

筅森魡賤 提交于 2020-01-11 17:59:46
问题 I'm using a circular buffer to push data onto either end of a list. After I'm done I want to align the buffer so the first element in the list is at position zero and can be used like a regular array without any fancy indexing overhead. So I have my circular list with capacity N , it has n elements starting at arbitrary index f . What's the fastest way to shift/rotate all the elements such that f = 0 ? The catch is I want to do this in-place (though of course some registers/temporaries will