static void mpack_growable_writer_flush(mpack_writer_t* writer, const char* data, size_t count) { // This is an intrusive flush function which modifies the writer's buffer // in response to a flush instead of emptying it in order to add more // capacity for data. This removes the need to copy data from a fixed buffer // into a growable one, improving performance. // // There are three ways flush can be called: // - flushing the buffer during writing (used is zero, count is all data, data is buffer) // - flushing extra data during writing (used is all flushed data, count is extra data, data is not buffer) // - flushing during teardown (used and count are both all flushed data, data is buffer) // // In the first two cases, we grow the buffer by at least double, enough // to ensure that new data will fit. We ignore the teardown flush. if (data == writer->buffer) { // teardown, do nothing if (writer->used == count) return; // otherwise leave the data in the buffer and just grow writer->used = count; count = 0; } mpack_log("flush size %i used %i data %p buffer %p\n", (int)count, (int)writer->used, data, writer->buffer); mpack_assert(data == writer->buffer || writer->used + count > writer->size, "extra flush for %i but there is %i space left in the buffer! (%i/%i)", (int)count, (int)writer->size - (int)writer->used, (int)writer->used, (int)writer->size); // grow to fit the data // TODO: this really needs to correctly test for overflow size_t new_size = writer->size * 2; while (new_size < writer->used + count) new_size *= 2; mpack_log("flush growing buffer size from %i to %i\n", (int)writer->size, (int)new_size); // grow the buffer char* new_buffer = (char*)mpack_realloc(writer->buffer, writer->used, new_size); if (new_buffer == NULL) { mpack_writer_flag_error(writer, mpack_error_memory); return; } writer->buffer = new_buffer; writer->size = new_size; // append the extra data if (count > 0) { mpack_memcpy(writer->buffer + writer->used, data, count); writer->used += count; } mpack_log("new buffer %p, used %i\n", new_buffer, (int)writer->used); }
// Writes encoded bytes to the buffer, flushing if necessary. MPACK_STATIC_INLINE void mpack_write_native(mpack_writer_t* writer, const char* p, size_t count) { mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count); if (writer->size - writer->used < count) { mpack_write_native_straddle(writer, p, count); } else { mpack_memcpy(writer->buffer + writer->used, p, count); writer->used += count; } }
void* mpack_realloc(void* old_ptr, size_t used_size, size_t new_size) { if (new_size == 0) { if (old_ptr) MPACK_FREE(old_ptr); return NULL; } void* new_ptr = MPACK_MALLOC(new_size); if (new_ptr == NULL) return NULL; mpack_memcpy(new_ptr, old_ptr, used_size); MPACK_FREE(old_ptr); return new_ptr; }
// Writes encoded bytes to the buffer when we already know the data // does not fit in the buffer (i.e. it straddles the edge of the // buffer.) If there is a flush function, it is guaranteed to be // called; otherwise mpack_error_too_big is raised. static void mpack_write_native_straddle(mpack_writer_t* writer, const char* p, size_t count) { mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count); if (mpack_writer_error(writer) != mpack_ok) return; mpack_log("big write for %i bytes from %p, %i space left in buffer\n", (int)count, p, (int)(writer->size - writer->used)); mpack_assert(count > writer->size - writer->used, "big write requested for %i bytes, but there is %i available " "space in buffer. should have called mpack_write_native() instead", (int)count, (int)(writer->size - writer->used)); // we'll need a flush function if (!writer->flush) { mpack_writer_flag_error(writer, mpack_error_too_big); return; } // flush the buffer mpack_writer_flush_unchecked(writer); if (mpack_writer_error(writer) != mpack_ok) return; // note that an intrusive flush function (such as mpack_growable_writer_flush()) // may have changed size and/or reset used to a non-zero value. we treat both as // though they may have changed, and there may still be data in the buffer. // flush the extra data directly if it doesn't fit in the buffer if (count > writer->size - writer->used) { writer->flush(writer, p, count); if (mpack_writer_error(writer) != mpack_ok) return; } else { mpack_memcpy(writer->buffer + writer->used, p, count); writer->used += count; } }
// Reads count bytes into p. Used when there are not enough bytes // left in the buffer to satisfy a read. void mpack_read_native_big(mpack_reader_t* reader, char* p, size_t count) { mpack_assert(count == 0 || p != NULL, "data pointer for %i bytes is NULL", (int)count); if (mpack_reader_error(reader) != mpack_ok) { mpack_memset(p, 0, count); return; } mpack_log("big read for %i bytes into %p, %i left in buffer, buffer size %i\n", (int)count, p, (int)reader->left, (int)reader->size); if (count <= reader->left) { mpack_assert(0, "big read requested for %i bytes, but there are %i bytes " "left in buffer. call mpack_read_native() instead", (int)count, (int)reader->left); mpack_reader_flag_error(reader, mpack_error_bug); mpack_memset(p, 0, count); return; } // we'll need a fill function to get more data. if there's no // fill function, the buffer should contain an entire MessagePack // object, so we raise mpack_error_invalid instead of mpack_error_io // on truncated data. if (reader->fill == NULL) { mpack_reader_flag_error(reader, mpack_error_invalid); mpack_memset(p, 0, count); return; } if (reader->size == 0) { // somewhat debatable what error should be returned here. when // initializing a reader with an in-memory buffer it's not // necessarily a bug if the data is blank; it might just have // been truncated to zero. for this reason we return the same // error as if the data was truncated. mpack_reader_flag_error(reader, mpack_error_io); mpack_memset(p, 0, count); return; } // flush what's left of the buffer if (reader->left > 0) { mpack_log("flushing %i bytes remaining in buffer\n", (int)reader->left); mpack_memcpy(p, reader->buffer + reader->pos, reader->left); count -= reader->left; p += reader->left; reader->pos += reader->left; reader->left = 0; } // we read only in multiples of the buffer size. read the middle portion, if any size_t middle = count - (count % reader->size); if (middle > 0) { mpack_log("reading %i bytes in middle\n", (int)middle); if (mpack_fill(reader, p, middle) < middle) { mpack_reader_flag_error(reader, mpack_error_io); mpack_memset(p, 0, count); return; } count -= middle; p += middle; if (count == 0) return; } // fill the buffer reader->pos = 0; reader->left = mpack_fill(reader, reader->buffer, reader->size); mpack_log("filled %i bytes into buffer\n", (int)reader->left); if (reader->left < count) { mpack_reader_flag_error(reader, mpack_error_io); mpack_memset(p, 0, count); return; } // serve the remainder mpack_log("serving %i remaining bytes from %p to %p\n", (int)count, reader->buffer+reader->pos,p); mpack_memcpy(p, reader->buffer + reader->pos, count); reader->pos += count; reader->left -= count; }