Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::circular buffer- avoid memory allocation
From: Jan Gaspar (jano_gaspar_at_[hidden])
Date: 2010-05-24 16:52:14


Hi Lloyd, not sure I understand the problem correctly but here is some sample code struct buffer { char data[1024]; }; boost::circular_buffer<buffer> cb(10); // allocation done here (10*1024) // producer buffer generated_data; generated_data.data[0] = ... generated_data.data[1] = ... // ... cb.push_back(generated_data); // no allocation here; just copy // consumer buffer consumed_data = cb.front(); // no deallocation; just copy cb.pop_front(); // remove the read buffer char c0 = consumed_data.data[0]; char c1 = consumed_data.data[1]; // ... Also see the Bounded Buffer example in the circular_buffer's documentation. Regards, Jan ________________________________ From: Lloyd <lloyd_at_[hidden]> To: boost-users_at_[hidden] Sent: Mon, 24 May, 2010 7:34:24 Subject: [Boost-users] boost::circular buffer- avoid memory allocation Hello, We are using circular buffer. The producer generates the data (a 1024byte char buffer) and the consumer reads it from circular buffer. In normal case the producer allocates the memory for each item. we believe this is very inefficient, so that we are going for circular buffer. How can we make the circualr buffer to allocate the memory in advace and make it possible for the producer and consumer to share it. For example if the buffer is of 10 nodes, the totoal memory allocated by circular buffer would be 10*1024, isn't it? How can the producer get the pointer to the next free node available in the circular buffer? (so as to avoid memory allocation by the producer) How can the consumer get pointer to the data to be read next? The sample available in boost::circular buffer describes it with a simple integer based example. I think this kind of problem is not addressed in it. Some hint will be very useful. Thanks, Lloyd ______________________________________ Scanned and protected by Email scanner



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net