ext/mochilo/buffer.c in mochilo-1.3.0 vs ext/mochilo/buffer.c in mochilo-2.0

- old
+ new

@@ -17,11 +17,11 @@ else chunk_size += MOCHILO_CHUNK_SIZE; buf->last_alloc = chunk->ptr = malloc(chunk_size); if (!chunk->ptr) - rb_raise(rb_eNoMemError, "Failed to allocate new chunk"); + return NULL; chunk->end = chunk->ptr + chunk_size; return chunk; } @@ -36,11 +36,11 @@ chunk->ptr = buf->last_alloc; } static void free_buf(mochilo_buf *buf) { - uint32_t i; + uint16_t i; for (i = 0; i < buf->cur_chunk; ++i) free(buf->chunks[i].ptr); free(buf->chunks); @@ -59,11 +59,11 @@ VALUE mochilo_buf_flush(mochilo_buf *buf) { VALUE rb_str; char *ptr; - uint32_t i; + uint16_t i; skip_last_chunk(buf); #ifdef RUBINIUS char *alloc; @@ -93,24 +93,19 @@ return rb_str; } mochilo_buf_chunk *mochilo_buf_rechunk2(mochilo_buf *buf, size_t chunk_size) { - mochilo_buf_chunk *chunks; - skip_last_chunk(buf); if (buf->cur_chunk == buf->chunk_count) { - if ((buf->chunk_count * 2) < buf->chunk_count) - rb_raise(rb_eArgError, "Too many chunks required to serialize"); + buf->chunk_count *= 2; - chunks = realloc(buf->chunks, buf->chunk_count * 2 * sizeof(mochilo_buf_chunk)); - if (!chunks) - rb_raise(rb_eNoMemError, "Failed to realloc chunks"); + buf->chunks = realloc(buf->chunks, buf->chunk_count * sizeof(mochilo_buf_chunk)); + if (!buf->chunks) + return NULL; - buf->chunks = chunks; - buf->chunk_count *= 2; } return init_cur_chunk(buf, chunk_size); } @@ -121,11 +116,13 @@ void mochilo_buf_put(mochilo_buf *buf, const char *data, size_t len) { mochilo_buf_chunk *chunk = &buf->chunks[buf->cur_chunk]; - if (unlikely(chunk->ptr + len > chunk->end)) - chunk = mochilo_buf_rechunk2(buf, len); + if (unlikely(chunk->ptr + len > chunk->end)) { + if (!(chunk = mochilo_buf_rechunk2(buf, len))) + return; + } memmove(chunk->ptr, data, len); chunk->ptr += len; }