On 7/27/2015 2:14 PM, Michael Rossberg wrote:
Hi,

This is a great idea, thanks for the implementation!
I am happy if someone finds it useful.

  struct timeval
+msec2tv (int a)
+{
+  struct timeval ret;
+
+  ret.tv_sec = 0;
+  ret.tv_usec = a * 1000;
+
+  return tv_adjust (ret);
+}


I'm not a very big fan of returning local static information in msec2tv.  I 
would prefer that you pass in the struct timeval * that you want filled in.  
Otherwise you are going to get some very weird behavior when someone down the 
line missappropriates the msec2tv and calls it twice and expects the output to 
stay consistent.  Additionally if someone ever adds another thread it will 
misbehave as well.
Please note that the value is returned by copy and not by reference to a static 
or
global variable. Hence, it should work perfectly in multithreaded environments 
or
over multiple calls. It may not be the fastest, but struct timeval is small.

Donald,

You were not alone, I had the same thought when I first looked at the code. Returning a struct instead of a pointer has been always considered a "sin" to the point where I even forgot it is possible and well defined. As Michael pointed out, every time you call the function a copy is created and passed up to the caller where it is (generally speaking) stored in a stack variable. Memory management is avoided in this case at the cost of speed. Since timeval is only a couple of integers probably, it is not too bad and you keep the code simple. Like you suggested, I always pass a pointer from the caller when dealing with a situation like this.

Michael, Thanks for implementing this!

Cheers,
Jafar




_______________________________________________
Quagga-dev mailing list
[email protected]
https://lists.quagga.net/mailman/listinfo/quagga-dev

Reply via email to