ringframe.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "ringframe.h"
  2. unsigned short ringframe_in(ringframe_t * p_ringframe, void * p_data,unsigned char data_size)
  3. {
  4. unsigned char * p;
  5. if(p_ringframe->freespace < ( data_size + 1 ))return 1;
  6. p = (unsigned char*)p_ringframe->p_buffer + p_ringframe->front ;
  7. * p=data_size;
  8. p_ringframe->front++;
  9. p_ringframe->front &= p_ringframe->size-1;
  10. for(int i=0;i<data_size;i++)
  11. {
  12. p = (unsigned char*)p_ringframe->p_buffer + p_ringframe->front ;
  13. * p=((unsigned char*)p_data)[i];
  14. p_ringframe->front++;
  15. p_ringframe->front &= p_ringframe->size-1;
  16. }
  17. p_ringframe->freespace -= (data_size+1);
  18. p_ringframe->element_count++;
  19. return 0;
  20. }
  21. unsigned short ringframe_out(ringframe_t * p_ringframe, void * p_data,unsigned char * data_size)
  22. {
  23. unsigned char * p;
  24. if(p_ringframe->element_count < 1)return 1;
  25. p = (unsigned char*)p_ringframe->p_buffer + p_ringframe->back ;
  26. * data_size = * p;
  27. p_ringframe->back++;
  28. p_ringframe->back &= p_ringframe->size-1;
  29. for(int i=0;i < *data_size;i++)
  30. {
  31. p = (unsigned char*)p_ringframe->p_buffer + p_ringframe->back ;
  32. ((unsigned char*)p_data)[i]= * p;
  33. p_ringframe->back++;
  34. p_ringframe->back &= p_ringframe->size-1;
  35. }
  36. p_ringframe->freespace += (* data_size+1);
  37. p_ringframe->element_count--;
  38. return 0;
  39. }
  40. unsigned short ringframe_peek(ringframe_t * p_ringframe, void * p_data,unsigned char * data_size)
  41. {
  42. unsigned char * p;
  43. unsigned short temp = p_ringframe->back;
  44. if(p_ringframe->element_count < 1)return 1;
  45. p = (unsigned char*)p_ringframe->p_buffer + temp ;
  46. * data_size = * p;
  47. temp++;
  48. temp &= p_ringframe->size-1;
  49. for(int i=0;i < *data_size;i++)
  50. {
  51. p = (unsigned char*)p_ringframe->p_buffer + temp ;
  52. ((unsigned char*)p_data)[i]= * p;
  53. temp++;
  54. temp &= p_ringframe->size-1;
  55. }
  56. return 0;
  57. }
  58. unsigned short ringframe_throw(ringframe_t * p_ringframe)
  59. {
  60. unsigned char * p;
  61. if(p_ringframe->element_count < 1)return 1;
  62. p = (unsigned char*)p_ringframe->p_buffer + p_ringframe->back ;
  63. p_ringframe->back = p_ringframe->back + * p + 1;
  64. p_ringframe->back &= p_ringframe->size-1;
  65. p_ringframe->freespace += (* p +1);
  66. p_ringframe->element_count--;
  67. return 0;
  68. }
  69. void ringframe_reset(ringframe_t * p_ringframe)
  70. {
  71. p_ringframe->back=0;
  72. p_ringframe->front=0;
  73. p_ringframe->element_count=0;
  74. p_ringframe->freespace = p_ringframe->size -1;
  75. }