Hi team
I am looking at an example about Queue from TI-RTOS, I am simply not quite understand how it can be implemented.
Example is taken from here (page 126): http://www.ti.com/lit/ug/spruex3t/spruex3t.pdf
I simply don't understand how Queue_enqueue() may allocate the correct size from only the address of an element. How can Queue_enqueue() find the size of Rec??
Here are the full codes:
Thanks team!
I am looking at an example about Queue from TI-RTOS, I am simply not quite understand how it can be implemented.
Example is taken from here (page 126): http://www.ti.com/lit/ug/spruex3t/spruex3t.pdf
I simply don't understand how Queue_enqueue() may allocate the correct size from only the address of an element. How can Queue_enqueue() find the size of Rec??
Code:
typedef struct Rec {
Queue_Elem elem;
Int data;
} Rec;
// some code removed for easier reading...
// Add r1 and r2 to the back of myQ.
Queue_enqueue(myQ, &(r1.elem)); // <<=== how Queue_enqueue() find the size of Rec???
Queue_enqueue(myQ, &(r2.elem));
Here are the full codes:
Code:
typedef struct Queue_Elem {
Queue_Elem *next;
Queue_Elem *prev;
} Queue_Elem;
Code:
/* This structure can be added to a Queue because the first field is a Queue_Elem. */
typedef struct Rec {
Queue_Elem elem;
Int data;
} Rec;
Queue_Handle myQ;
Rec r1, r2;
Rec* rp;
r1.data = 100;
r2.data = 200;
// No parameters or Error block are needed to create a Queue.
myQ = Queue_create(NULL, NULL);
// Add r1 and r2 to the back of myQ.
Queue_enqueue(myQ, &(r1.elem));
Queue_enqueue(myQ, &(r2.elem));
// Dequeue the records and print their data
while (!Queue_empty(myQ)) {
// Implicit cast from (Queue_Elem *) to (Rec *)
rp = Queue_dequeue(myQ);
System_printf("rec: %d\n", rp->data);
}