On Apr 13, 2007, at 10:45 AM, Mehedi Bakht wrote:

What happens if I declare a Queue of messages instead of a Queue of message pointers ?

I am trying to find out the potential problems of declaring QueueC (message_t, 8) instead of QueueC(message_t*, 8). Then, when I get a packet from the lower layer, I instantiate a new message_t variable, copy the contents of the received message there, queue it and return the buffer to radio Layer.


event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len) {
   message_t tempMsg;
   memcpy(&tempMsg,bufPtr,sizeof(message_t);
   SendQueue.enqueue(tempMsg);
   return bufPtr;
}

Then you are passing the structure by value. This will cause there to be up to six copies:

1. From pointer to stack: memcpy(&tempMsg,bufPtr,sizeof(message_t);
2. From stack to stack (making tempMsg a by-value parameter to enqueue);
3. From stack to data (assigning the parameter of enqueue to an element of the queue)

dequeueing:

4. From data to stack (assigning element of the queue to the local stack variable in dequeue) 5. From stack to stack (copying the local stack variable to the return value) 6. From stack to data (copying the return value to a pointer you can do something with).

Furthermore, passing large structures on the stack is notoriously inefficient in C. That's why you rarely see it done.

Phil


_______________________________________________
Tinyos-help mailing list
[EMAIL PROTECTED]
https://mail.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help

Reply via email to