Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-19035

add deallocation hook/ownership to QByteArray::fromRawData

    XMLWordPrintable

Details

    Description

      It would be very useful for QByteArray's fromRawData to provide some sort of deallocation/ownership of the buffer passed to it. This would be especially useful when dealing with other apis/libraries that do their own allocation of memory buffers and such. Consider for example a hypothetical api:

      char *some_api_read_lots_of_data(int &size);
      some_api_free(char *previously_returned_pointer);
      

      Currently there is no method to wrap the returned buffer in a QByteArray without the cost of a initial memory copy of all the contained data. While this cost of a memory copy is trivial for small quantities of data it quickly becomes both a performance and allocation problem (as both the original and copy need to be in memory concurrently) when the size of the buffer being copied is large.

      Although more of a c'ish solution, the following illustrates what I am suggesting:

      void freeFunc(char *ptr)
      {
              some_api_free(ptr);
      }
      
      int size;
      char *rawData = some_api_read_lots_of_data(&size);
      QByteArray data = QByteArray::fromRawData(rawData, size, freeFunc);
      

      With such a implementation, the QByteArray created in this manner's destructor would check for a freeFunc and free the underlying buffer. Providing such free's the programmer from having to manually track buffers passed to QByteArray's current fromRawData() or taking the potentially expensive memcpy hit. Additionally providing a "hint" pointer that is passed to the free function as well would be very useful although not included above.

      Currently I am passing buffers from Qt to ZMQ (zeromq.org) and vice versa. ZMQ's message object provides a interface for ownership/deallocation of buffers passed to it. This allows for zero copy from the Qt app into ZMQ. The opposite is not presently possible though. The ZMQ interface provides for a hint pointer as well as a free function pointer. I use the hint pointer as a pointer to the QByteArray and the results of QByteArray::constData as the buffer. This allows the free func to be:

      void zmq_free_q_byte_array(void *data, void *hint)
      {
            QByteArray *ptr = (QByteArray *)hint;
            delete ptr;
      }
      

      There are more elegant oop methods of extending fromRawData() but hopefully the above illustrates the desired feature.

      Thanks

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            schallee Ed Schaller
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes