#include "queue.h"


unsigned short queue_in(queue_t * p_queue, void const * p_element)
{
	unsigned char * p = (unsigned char*)p_queue->p_buffer + ((p_queue->front & (p_queue->size-1))*(p_queue->element_size));
	if(p_queue->element_count>=p_queue->size)return 0;
	for(int i=0;i<p_queue->element_size;i++)
	{
		p[i]=((unsigned char*)(void*)p_element)[i];
	}
	p_queue->front++;
	p_queue->element_count++;
	return 1;
}

unsigned short queue_out(queue_t * p_queue, void * p_element)
{
	unsigned char * p = (unsigned char*)p_queue->p_buffer + ((p_queue->back & (p_queue->size-1))*(p_queue->element_size));
	if(p_queue->element_count<1)return 0;
	for(int i=0;i<p_queue->element_size;i++)
	{
		((unsigned char*)(void*)p_element)[i]=p[i];
	}
	p_queue->back++;
	p_queue->element_count--;
	return 1;
}

unsigned short queue_peek(queue_t * p_queue, void * p_element)
{
	unsigned char * p = (unsigned char*)p_queue->p_buffer + ((p_queue->back & (p_queue->size-1))*(p_queue->element_size));
	if(p_queue->element_count<1)return 0;
	for(int i=0;i<p_queue->element_size;i++)
	{
		((unsigned char*)(void*)p_element)[i]=p[i];
	}
	return 1;
}

void queue_reset(queue_t * p_queue)
{
	p_queue->back=0;
	p_queue->front=0;              
	p_queue->element_count=0;																		
}
