title: Generic ring buffer
body: <p>I have implemented a generic ring buffer, but i am experiencing some strange results.First i push 15 integers successfully, but when i try to print to content of the buffer it prints some of the elements wrong.</p>
  <p>First i am allocating memory for 10 integers.Then i am trying to push 15 and resizing of the buffer comes in place.Maybe the resizing is breaking it somehow, because without it it's working fine.</p>
  <p><strong>RingBuffer.c</strong></p>
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include&quot;RingBuffer.h&quot;
  
  unsigned char RingInit(RingBuffer* buffer, int elemLen, int elemSize, void (*fn)(void*))
  {
      buffer-&gt;dataLen = elemLen;
      buffer-&gt;dataSize = elemSize;
      
      if( (buffer-&gt;data = malloc( buffer-&gt;dataSize * buffer-&gt;dataLen )) == NULL){
          
          return 1;
      }
  
      buffer-&gt;head = 0;
      buffer-&gt;tail = 0;
      buffer-&gt;elemCount = 0;
      buffer-&gt;freeFn = fn;
      buffer-&gt;doResize = 1;
      buffer-&gt;resizeBy = 2;
      
      return 0;
  }
  
  unsigned char RingPush(RingBuffer* buffer, void* elem)
  {
      if(RingIsFull(buffer)){
  
          if(!buffer-&gt;doResize || RingResize(buffer)){
              
              return 1;
          }
      }
  
      memcpy((buffer-&gt;data + (buffer-&gt;head*buffer-&gt;dataSize)), elem, buffer-&gt;dataSize);
      buffer-&gt;head = (buffer-&gt;head + 1) % buffer-&gt;dataLen;
      buffer-&gt;elemCount++;
      
      return 0;
  }
  
  unsigned char RingRead(RingBuffer* buffer, void* elem)
  {
      if(!RingIsEmpty(buffer)){
          
          memcpy(elem, (buffer-&gt;data + (buffer-&gt;tail*buffer-&gt;dataSize)), buffer-&gt;dataSize);
          buffer-&gt;tail = (buffer-&gt;tail + 1) % buffer-&gt;dataLen;
          buffer-&gt;elemCount--;
  
          return 0;
      }
  
      return 1;
  }
  
  unsigned char RingIsEmpty(RingBuffer* buffer)
  {
      return (buffer-&gt;elemCount == 0);
  }
  
  unsigned char RingIsFull(RingBuffer* buffer)
  {
      return (buffer-&gt;elemCount == buffer-&gt;dataLen);
  }
  
  void RingFree(RingBuffer* buffer)
  {
      buffer-&gt;freeFn(buffer-&gt;data);
  }
  
  unsigned char RingResize(RingBuffer* buffer)
  {
      unsigned char* newMemory = NULL;
  
      if ( (newMemory = realloc(buffer-&gt;data, buffer-&gt;dataLen + (buffer-&gt;resizeBy * buffer-&gt;dataSize))) == NULL){
          
          return 1;
      }
      
      buffer-&gt;data = newMemory;
      buffer-&gt;dataLen += buffer-&gt;resizeBy;    
      buffer-&gt;head = buffer-&gt;elemCount;
      
      return 0;
  }
  </code></pre>
  <p><strong>RingBuffer.h</strong></p>
  <pre><code>#ifndef RINGBUFFER_H_
  #define RINGBUFFER_H_
  
  typedef struct
  {
      void *data;
      int dataSize;
      int dataLen;
      int head;
      int tail;
      int elemCount;
      int doResize;
      int resizeBy;
      void (*freeFn)(void*);
  
  }RingBuffer;
  
  unsigned char RingInit      (RingBuffer* buffer, int elemLen, int elemSize, void (*fn)(void*));
  unsigned char RingRead      (RingBuffer* buffer, void* elem);
  unsigned char RingIsEmpty   (RingBuffer* buffer);
  unsigned char RingIsFull    (RingBuffer* buffer);
  void          RingFree      (RingBuffer* buffer);
  unsigned char RingResize    (RingBuffer* buffer);
  unsigned char RingPush      (RingBuffer* buffer, void* elem);
  
  #endif /* RINGBUFFER_H_ */
  </code></pre>
  <p><strong>Test.c</strong></p>
  <pre><code>#include&lt;stdlib.h&gt;
  #include&lt;string.h&gt;
  #include &lt;stdio.h&gt;
  #include&quot;RingBuffer.h&quot;
  
  #define ARRAY_LENGTH(a)     sizeof(a)/sizeof(a[0])
  
  void freeInt(void* data)
  {
      free(data);
  }
  
  int main(void)
  {
      RingBuffer Ring;
      int buf[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
      int i = 0;
      
      if(RingInit(&amp;Ring, ARRAY_LENGTH(buf)-5, sizeof(int), freeInt) == 0){
          
          printf(&quot;\nRING INIT OK&quot;);
      }
      else{
          
          printf(&quot;\nRING INIT FAIL&quot;);
          RingFree(&amp;Ring);
          return 0;
      }
      
      Ring.doResize = 1;
      for(i = 0 ; i &lt; ARRAY_LENGTH(buf) ; i++){
          
          if(RingPush(&amp;Ring, &amp;buf[i])){
              
              printf(&quot;\nRING PUSH FAIL&quot;);
              RingFree(&amp;Ring);
              return 0;
          }
          
          printf(&quot;\nRING PUSH OK-&gt;%d&quot;, buf[i]);
          printf(&quot; %d&quot;, ((int*)(Ring.data))[i]); 
      }
      
      printf(&quot;\n&quot;);
      for(i = 0 ; i &lt; ARRAY_LENGTH(buf); i++){
          
          printf(&quot; %d&quot;, ((int*)(Ring.data))[i]);
      }
      
      memset(buf, 0, sizeof(buf));
      printf(&quot;\nLOAD ZEROS:&quot;);
      for(i = 0 ; i &lt; ARRAY_LENGTH(buf); i++){
          printf(&quot; %d&quot;, buf[i]);
      }
      
      i = 0;
      while(!RingIsEmpty(&amp;Ring)){
          
          if(RingRead(&amp;Ring, &amp;buf[i])){
              
              printf(&quot;\nRING READ FAIL&quot;);
              RingFree(&amp;Ring);
              return 0;
          }
          
          printf(&quot;\nRING READ OK-&gt;%d&quot;, buf[i]);
          i++;
      }
      
      getchar();
      RingFree(&amp;Ring);
      return 0;
  }
  </code></pre>
  <p><strong>Results:</strong></p>
  <pre><code>RING INIT OK
  RING PUSH OK-&gt;1 1
  RING PUSH OK-&gt;2 2
  RING PUSH OK-&gt;3 3
  RING PUSH OK-&gt;4 4
  RING PUSH OK-&gt;5 5
  RING PUSH OK-&gt;6 6
  RING PUSH OK-&gt;7 7
  RING PUSH OK-&gt;8 8
  RING PUSH OK-&gt;9 9
  RING PUSH OK-&gt;10 10
  RING PUSH OK-&gt;11 11
  RING PUSH OK-&gt;12 12
  RING PUSH OK-&gt;13 1313427978
  RING PUSH OK-&gt;14 1431314503
  RING PUSH OK-&gt;15 1327515731
   1 2 3 4 5 6 -1326086846 5687 12790216 12779712 11 12 926036256 842412848 909391924
  LOAD ZEROS: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  RING READ OK-&gt;1
  RING READ OK-&gt;2
  RING READ OK-&gt;3
  RING READ OK-&gt;4
  RING READ OK-&gt;5
  RING READ OK-&gt;6
  RING READ OK-&gt;-1326086846
  RING READ OK-&gt;5687
  RING READ OK-&gt;12790216
  RING READ OK-&gt;12779712
  RING READ OK-&gt;11
  RING READ OK-&gt;12
  RING READ OK-&gt;1313427978
  RING READ OK-&gt;1163010119
  RING READ OK-&gt;1327514689   
  </code></pre>
  <p>It should print the numbers 1-15.What it happens is it prints some of the numbers wrong.Every time i start the program different elements are wrong.</p>
  <p><strong>Compile</strong></p>
  <p>gcc Test.c RingBuffer.c</p>
  <p><strong>Complier Version</strong></p>
  <p>gcc (MinGW.org GCC-6.3.0-1) 6.3.0
  Copyright (C) 2016 Free Software Foundation, Inc.
  This is free software; see the source for copying conditions.  There is NO
  warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.</p>