title: How do you implement a circular buffer in C?
body: <p>I have a need for a fixed-size (selectable at run-time when creating it, not compile-time) circular buffer which can hold objects of any type and it needs to be <em>very</em> high performance. I don't think there will be resource contention issues since, although it's in a multi-tasking embedded environment, it's a co-operative one so the tasks themselves can manage that.</p>
  <p>My initial thought was to store a simple struct in the buffer which would contain the type (simple enum/define) and a void pointer to the payload but I want this to be as fast as possible so I'm open to suggestions that involve bypassing the heap.</p>
  <p>Actually I'm happy to bypass any of the standard library for raw speed - from what I've seen of the code, it's not heavily optimized for the CPU : it looks like they just compiled C code for things like <code>strcpy()</code> and such, there's no hand-coded assembly.</p>
  <p>Any code or ideas would be greatly appreciated. The operations required are:</p>
  <ul>
  <li>create a buffer with specific size.</li>
  <li>put at the tail.</li>
  <li>get from the head.</li>
  <li>return the count.</li>
  <li>delete a buffer.</li>
  </ul>