Hi,

I want to implement some kind of CAN frame logger/hub and my basic idea would be something like follow:

Qt Code:
  1. class QCanBusFrame // just some sample excerpt for those not familiar with QCanBusFrame
  2. {
  3. private:
  4.  
  5. quint32 canId:29;
  6. quint8 format:3;
  7. quint8 reserved[2];
  8. QByteArray load;
  9. // ...
  10. };
  11.  
  12. class CanFrameHub : public QObject
  13. {
  14. signals:
  15. void framesReceived(QVector<QCanBusFrame> frames);
  16.  
  17. private slots:
  18. void framesReceivedSlot()
  19. {
  20. QVector<QCanBusFrame> frames = canBusDevice->readAllFrames();
  21. emit framesReceived(frames);
  22. }
  23.  
  24. private:
  25. QCanbusDevice *canBusDevice;
  26. };
  27.  
  28. class CanFrameLogger : public QObject
  29. {
  30. public:
  31.  
  32. const QCanBusFrame& getFrameAt(int index)
  33. {
  34. // is it meaningful returning as reference?
  35. return loggedFrames.at(index);
  36. }
  37.  
  38. private slots:
  39.  
  40. void framesReceived(QVector<QCanBusFrame> frames)
  41. {
  42. loggedFrames.append(frames);
  43. }
  44.  
  45. private:
  46.  
  47. QVector<QCanBusFrame> loggedFrames;
  48. QCanbusDevice *canBusDevice;
  49. };
To copy to clipboard, switch view to plain text mode 

Now I wonder which data container would be the best to distribute and store the received frames. Here are my thoughts:

  • CAN frames are received continuously in a high frame rate (~2.200 frames per second in my case). As I understand QVector is basically a wrapper around "new QCanBusFrame[x];" and everytime the vector grows each CAN frame has to be copy-constructed to the new array. Would a QList therefore make more sense?

  • The CanFrameHub class distributes the received CAN frames to every (other) component which is connected to the signal. The frames may be only processed or also stored in these components. So each component would get/store it's own copy of each frame. Therefore I came up with the idea using QVector<QSharedPointer<QCanBusFrame>>. So every frame received in the hub will be copied once in heap QSharedPointer<QCanBusFrame>(new QCanBusFrame(frame)) and only pointers to the single frame will be passed around.

  • Maybe this way the caused heap fragmentation is worse than having duplicated CAN frames?

  • Or maybe there isn't really a problem with copying since the "biggest load" of QCanBusFrame is a QByteArray, which is implicitly shared!? However, the size of a QSharedPointer is half the size of a QCanBusFrame:


Qt Code:
  1. sizeof(QCanBusFrame): 32
  2. sizeof(QSharedPointer<QCanBusFrame>): 16
To copy to clipboard, switch view to plain text mode 

Could anyone give me a hint which solution would be best? Thank you.