queue.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "queue.h"
  2. unsigned short queue_in(queue_t * p_queue, void const * p_element)
  3. {
  4. unsigned char * p = (unsigned char*)p_queue->p_buffer + ((p_queue->front & (p_queue->size-1))*(p_queue->element_size));
  5. if(p_queue->element_count>=p_queue->size)return 0;
  6. for(int i=0;i<p_queue->element_size;i++)
  7. {
  8. p[i]=((unsigned char*)(void*)p_element)[i];
  9. }
  10. p_queue->front++;
  11. p_queue->element_count++;
  12. return 1;
  13. }
  14. unsigned short queue_out(queue_t * p_queue, void * p_element)
  15. {
  16. unsigned char * p = (unsigned char*)p_queue->p_buffer + ((p_queue->back & (p_queue->size-1))*(p_queue->element_size));
  17. if(p_queue->element_count<1)return 0;
  18. for(int i=0;i<p_queue->element_size;i++)
  19. {
  20. ((unsigned char*)(void*)p_element)[i]=p[i];
  21. }
  22. p_queue->back++;
  23. p_queue->element_count--;
  24. return 1;
  25. }
  26. unsigned short queue_peek(queue_t * p_queue, void * p_element)
  27. {
  28. unsigned char * p = (unsigned char*)p_queue->p_buffer + ((p_queue->back & (p_queue->size-1))*(p_queue->element_size));
  29. if(p_queue->element_count<1)return 0;
  30. for(int i=0;i<p_queue->element_size;i++)
  31. {
  32. ((unsigned char*)(void*)p_element)[i]=p[i];
  33. }
  34. return 1;
  35. }
  36. void queue_reset(queue_t * p_queue)
  37. {
  38. p_queue->back=0;
  39. p_queue->front=0;
  40. p_queue->element_count=0;
  41. }