Welcome to Soft32 Linux Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

circular buffer

 
   Soft32 Home -> Linux -> App Development RSS
Next:  Marines ban Twitter, Facebook, other sites  
Author Message
dbtouch

External


Since: Dec 02, 2008
Posts: 3



(Msg. 1) Posted: Tue Aug 04, 2009 2:02 pm
Post subject: circular buffer
Archived from groups: comp>os>linux>development>apps (more info?)

Dear Unix Programmer,

I am reading circular buffer in wiki page: http://en.wikipedia.org/wiki/Circular_buffer.
It provides an optimized implementation using mmap as follows. I am
really baffled by this. For example, what is "order" parameter in
ring_buffer_create for? Please advise on this.

dbtouch

#include <sys/mman.h>
#include <stdlib.h>
#include <unistd.h>

#define report_exceptional_condition() abort ()

struct ring_buffer
{
void *address;

unsigned long count_bytes;
unsigned long write_offset_bytes;
unsigned long read_offset_bytes;
};

// what is order???
void
ring_buffer_create (struct ring_buffer *buffer, unsigned long order)
{
char path[] = "/dev/shm/ring-buffer-XXXXXX";
int file_descriptor;
void *address;
int status;

file_descriptor = mkstemp (path);
if (file_descriptor < 0)
report_exceptional_condition ();

status = unlink (path);
if (status)
report_exceptional_condition ();

buffer->count_bytes = 1UL << order;
buffer->write_offset_bytes = 0;
buffer->read_offset_bytes = 0;

status = ftruncate (file_descriptor, buffer->count_bytes);
if (status)
report_exceptional_condition ();

buffer->address = mmap (NULL, buffer->count_bytes << 1, PROT_NONE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);

if (buffer->address == MAP_FAILED)
report_exceptional_condition ();

address =
mmap (buffer->address, buffer->count_bytes, PROT_READ |
PROT_WRITE,
MAP_FIXED | MAP_SHARED, file_descriptor, 0);

if (address != buffer->address)
report_exceptional_condition ();

address = mmap (buffer->address + buffer->count_bytes,
buffer->count_bytes, PROT_READ | PROT_WRITE,
MAP_FIXED | MAP_SHARED, file_descriptor, 0);

if (address != buffer->address + buffer->count_bytes)
report_exceptional_condition ();

status = close (file_descriptor);
if (status)
report_exceptional_condition ();
}

void
ring_buffer_free (struct ring_buffer *buffer)
{
int status;

status = munmap (buffer->address, buffer->count_bytes << 1);
if (status)
report_exceptional_condition ();
}

void *
ring_buffer_write_address (struct ring_buffer *buffer)
{
return buffer->address + buffer->write_offset_bytes; /*** void
pointer arithmetic is a constraint violation. ***/
}

void
ring_buffer_write_advance (struct ring_buffer *buffer,
unsigned long count_bytes)
{
buffer->write_offset_bytes += count_bytes;
}

void *
ring_buffer_read_address (struct ring_buffer *buffer)
{
return buffer->address + buffer->read_offset_bytes;
}

void
ring_buffer_read_advance (struct ring_buffer *buffer,
unsigned long count_bytes)
{
buffer->read_offset_bytes += count_bytes;

if (buffer->read_offset_bytes >= buffer->count_bytes)
{
buffer->read_offset_bytes -= buffer->count_bytes;
buffer->write_offset_bytes -= buffer->count_bytes;
}
}

unsigned long
ring_buffer_count_bytes (struct ring_buffer *buffer)
{
return buffer->write_offset_bytes - buffer->read_offset_bytes;
}

unsigned long
ring_buffer_count_free_bytes (struct ring_buffer *buffer)
{
return buffer->count_bytes - ring_buffer_count_bytes (buffer);
}

void
ring_buffer_clear (struct ring_buffer *buffer)
{
buffer->write_offset_bytes = 0;
buffer->read_offset_bytes = 0;
}
Back to top
Login to vote
Loïc_Domaigné

External


Since: Aug 05, 2009
Posts: 1



(Msg. 2) Posted: Wed Aug 05, 2009 7:44 am
Post subject: Re: circular buffer [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi,

> I am reading circular buffer in wiki page:http://en.wikipedia.org/wiki/Circular_buffer.
> It provides an optimized implementation using mmap as follows. I am
> really baffled by this. For example, what is "order" parameter in
> ring_buffer_create for? Please advise on this.

The buffer is assumed to have a size of 2^order bytes (in words: 2
raised to the power of order). For instance, if order=12; you'll shall
have a buffer of 4KiB.

Please note that the buffer should be a multiple of the your OS memory
page size.

HTH,
Loïc.
--
My Blog: http://www.domaigne.com/blog

"The trouble with programmers is that you can never tell what a
programmer is doing until it’s too late." -- Seymour Cray
Back to top
Login to vote
Display posts from previous:   
Related Topics:
How can i register a link protocol for browsers - I want to add an action to the system so that every time a user clicks in a link with for example ..

storing the data in ln2440 target board memory - hai.. i using the target board ln2440sbc..and linux OS.. (16Mb flash and 32Mb sdram) i have cross compiled and porte...

GTK run error - hi i had created one same application in glad interface designer with c option.the n i compiled the same after that i....

C++ -> mplayer, stdin, stdout, popen / fork / pipe... - Hello everybody. I tried to work this out on my own for about 6 hours non-stop now and I just don't get it working.......

buffer overflows in the tty flip buffer in 2.6.17 - When data is received from the hardware, it needs to be placed into the tty device's flip buffer. This can be done with...

Possible Circular Locking in TIPC - hi, while running my usual stuff on 2.6.20-rc1-git5, sfuzz (http://www.digitaldwarf.be/products/sfuzz.c) did the..
       Soft32 Home -> Linux -> App Development All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Categories:
 Windows
  Linux
 Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]