Commits

Rui Abreu Ferreira  committed 27a1b79

Fix unpacker allocation when data available

- The dataAvailable slot reads new data from the device and
passes it on to the msgpack unpacker. The previous implementation
failed to reallocate the buffer when needed causing the read to
block if the buffer was too small
- Removed the step allocation size, now hardcoded as 8k
- We now only reserve more buffer space when the buffer is empty

  • Participants
  • Parent commits 85e0f64

Comments (0)

Files changed (2)

 
 A crapload of them
 
-* I must've been sleeping when I wrote the readyRead handler,
-  it works assuming the buffer does not need to grow ...
-  otherwise it is just broken
 * Error checking (start with the QIODevice related errros)
 * Function call errors are not being handled ATM
 * Deep check QVariants before starting to serialized them

File src/neovimconnector.cpp

 
 namespace NeoVimQt {
 
-#define BUFFER_ALLOC_STEP 8196
-
 NeoVimConnector::NeoVimConnector(QIODevice *s)
 :QObject(), m_socket(s), m_error(NoError), reqid(0), m_neovimobj(NULL)
 {
  */
 void NeoVimConnector::dataAvailable()
 {
-	if ( msgpack_unpacker_buffer_capacity(&m_uk) < BUFFER_ALLOC_STEP ) {
-		if ( !msgpack_unpacker_reserve_buffer(&m_uk, BUFFER_ALLOC_STEP ) ) {
-			qWarning() << "Could not allocate memory in unpack buffer";
-			return;
+	qint64 read = 1;
+	while (read > 0) {
+		if ( msgpack_unpacker_buffer_capacity(&m_uk) == 0 ) {
+			if ( !msgpack_unpacker_reserve_buffer(&m_uk, 8192 ) ) {
+				// FIXME: error out
+				qWarning() << "Could not allocate memory in unpack buffer";
+				return;
+			}
 		}
-	}
 
-	qint64 read = m_socket->read(msgpack_unpacker_buffer(&m_uk), msgpack_unpacker_buffer_capacity(&m_uk));
-	msgpack_unpacker_buffer_consumed(&m_uk, read);
-	msgpack_unpacked result;
-	msgpack_unpacked_init(&result);
-	while(msgpack_unpacker_next(&m_uk, &result)) {
-		dispatch(result.data);
+		read = m_socket->read(msgpack_unpacker_buffer(&m_uk), msgpack_unpacker_buffer_capacity(&m_uk));
+		qDebug() << __func__ << read << msgpack_unpacker_buffer_capacity(&m_uk);
+		if ( read > 0 ) {
+			msgpack_unpacker_buffer_consumed(&m_uk, read);
+			msgpack_unpacked result;
+			msgpack_unpacked_init(&result);
+			while(msgpack_unpacker_next(&m_uk, &result)) {
+				dispatch(result.data);
+			}
+		}
 	}
-	// FIXME: this MAY fail if we don't grow the buffer
-	// if msg was parsed grow the buffer and read more
 }
 
 /**