Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

queue of buffers TAILQ

Status
Not open for further replies.

starn

Programmer
Mar 12, 2002
18
BE
Hi, if somebody could just give a look at this code :
I don't know what I do wrong....[sadeyes]

Thanks a lot :)

Starn

Code:
/*  This little piece of code initiate 2 queue 
**First queue is filled of empty buffers 
**second queue is filled with the full buffers
**PROBLEM : I can only fill the 2 first buffers in the FOR loop
** queueX.h contains the TAILQ macros. (see after the main function...)
*/
//I know, I don't need all these includes... (it's for other functions...)

#include <stdio.h>
#include <unistd.h>
#include <atomic.h>
#include <malloc.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <sys/syspage.h>
#include <sys/neutrino.h>
#include <syslog.h>
#include <sys/dispatch.h>
#include <sys/io-net.h>

#include <string.h>
#include <stdbool.h>

#include<sys/queueX.h>

 
#define  NB_BY    192         // 192 bytes = 1536 bits = 2x 48  échantillons de 16 bits  car 1 ms = 2 x 48 ech à 48000Hz    

int msg_thread_send;


TAILQ_HEAD(buffers,entry) head;  
struct buffers  *headp;

struct entry{
char data[NB_BY];
TAILQ_ENTRY(entry) entries; 
} *n1, *n2, *n3,*n4,*n5, *np,*sendp,*ready_sendp;

TAILQ_HEAD(send,entry) head_send;  
struct send  *head_sendp;


int main()
{

FILE *file1;
int i, file_size;

 if ((file1 = fopen (&quot;/home/s_16_44.wav&quot;, &quot;r&quot;)) == 0)
       printf (&quot;can not open file\n &quot;);
   fseek(file1, 0, SEEK_END);
   file_size=ftell(file1);
    fseek(file1, 48, SEEK_SET);
   

TAILQ_INIT (&head);
TAILQ_INIT (&head_send);

 n1 = malloc(sizeof(struct entry));      /* Insert at the head. */
 TAILQ_INSERT_HEAD(&head, n1, entries);
 
  n2 = malloc(sizeof(struct entry));      /* Insert at the tail. */
   TAILQ_INSERT_AFTER(&head, n1,n2, entries);
     //TAILQ_INSERT_TAIL(&head, n1, entries);

 n3 = malloc(sizeof(struct entry));      /* Insert at the tail. */
     TAILQ_INSERT_AFTER(&head, n2,n3, entries);
       //TAILQ_INSERT_TAIL(&head, n4, entries);
       

 n4 = malloc(sizeof(struct entry));      /* Insert at the tail. */
     TAILQ_INSERT_AFTER(&head,n3, n4, entries);
       //TAILQ_INSERT_TAIL(&head, n4, entries);
       

 n5 = malloc(sizeof(struct entry));      /* Insert at the tail. */
       TAILQ_INSERT_TAIL(&head, n5, entries);



 for (np = head.tqh_first; np != NULL; np = np->entries.tqe_next)
         {
          fread(np->data,NB_BY,1,file1);
         
         if(np=head.tqh_first)
         	{
         		TAILQ_INSERT_HEAD(&head_send, np, entries);
         		printf(&quot;insert_head\n&quot;);
         	}
         else	
        			TAILQ_INSERT_TAIL(&head_send, np, entries);
        			printf(&quot;insert_queue\n&quot;);

		}

np=head.tqh_first;
//ready_sendp=head_send.tqh_first;

// ICI, démarrer le thread_send

while(1)
{
	if(msg_thread_send)  //tant que le thread_send n'a pas mis ce bit à , il n'y a pas de buffer de libre 
	{
     fread(np->data,NB_BY,1,file1);
    // if(j>4)
     //{
     
     
 	TAILQ_INSERT_TAIL(&head_send, np, entries);
  //voir si il ne faut pas retirer le premier élément de la queue_send (memory leacks) ca devrait être arrangé avec TAILQ_REMOVE
 	
 	np = np->entries.tqe_next;
 	msg_thread_send=0;
	}
	
	if( np == NULL)        //si on est au bout de la queue, on revient au début...
	np=head.tqh_first;
 
 
 
}

// printf(&quot;data : %c\n&quot;,n1->data);

fclose(file1);




return(0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/*
 * Tail queue declarations.
 */
#define	TAILQ_HEAD(name, type)						struct name {									struct type *tqh_first;	/* first element */				struct type **tqh_last;	/* addr of last next element */		}

#define	TAILQ_HEAD_INITIALIZER(head)						{ NULL, &(head).tqh_first }

#define	TAILQ_ENTRY(type)						struct {									struct type *tqe_next;	/* next element */				struct type **tqe_prev;	/* address of previous next element */	}

/*
 * Tail queue functions.
 */
#define	TAILQ_EMPTY(head)	((head)->tqh_first == NULL)

#define	TAILQ_FIRST(head)	((head)->tqh_first)

#define	TAILQ_FOREACH(var, head, field)						for ((var) = TAILQ_FIRST((head));					    (var);								    (var) = TAILQ_NEXT((var), field))

#define	TAILQ_FOREACH_REVERSE(var, head, headname, field)			for ((var) = TAILQ_LAST((head), headname);				    (var);								    (var) = TAILQ_PREV((var), headname, field))

#define	TAILQ_INIT(head) do {							TAILQ_FIRST((head)) = NULL;						(head)->tqh_last = &TAILQ_FIRST((head));			} while (0)

#define	TAILQ_INSERT_AFTER(head, listelm, elm, field) do {			if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)		TAILQ_NEXT((elm), field)->field.tqe_prev = 				    &TAILQ_NEXT((elm), field);					else										(head)->tqh_last = &TAILQ_NEXT((elm), field);			TAILQ_NEXT((listelm), field) = (elm);					(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field);		} while (0)

#define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {				(elm)->field.tqe_prev = (listelm)->field.tqe_prev;			TAILQ_NEXT((elm), field) = (listelm);					*(listelm)->field.tqe_prev = (elm);					(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field);		} while (0)

#define	TAILQ_INSERT_HEAD(head, elm, field) do {				if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL)			TAILQ_FIRST((head))->field.tqe_prev =					    &TAILQ_NEXT((elm), field);					else										(head)->tqh_last = &TAILQ_NEXT((elm), field);			TAILQ_FIRST((head)) = (elm);						(elm)->field.tqe_prev = &TAILQ_FIRST((head));			} while (0)

#define	TAILQ_INSERT_TAIL(head, elm, field) do {				TAILQ_NEXT((elm), field) = NULL;					(elm)->field.tqe_prev = (head)->tqh_last;				*(head)->tqh_last = (elm);						(head)->tqh_last = &TAILQ_NEXT((elm), field);			} while (0)

#define	TAILQ_LAST(head, headname)						(*(((struct headname *)((head)->tqh_last))->tqh_last))

#define	TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)

#define	TAILQ_PREV(elm, headname, field)					(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))

#define	TAILQ_REMOVE(head, elm, field) do {					if ((TAILQ_NEXT((elm), field)) != NULL)						TAILQ_NEXT((elm), field)->field.tqe_prev = 				    (elm)->field.tqe_prev;					else										(head)->tqh_last = (elm)->field.tqe_prev;			*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field);		} while (0)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top