diff --git a/src/Canvas.cc b/src/Canvas.cc index 2694a6f..8f44a4a 100644 --- a/src/Canvas.cc +++ b/src/Canvas.cc @@ -107,31 +107,21 @@ Canvas::SetHeight(Local prop, Local val, const AccessorInfo &info static cairo_status_t toBuffer(void *c, const uint8_t *data, unsigned len) { - closure_t *closure = (closure_t *) c; - - // Olaf (2011-02-21): Store more data, but don't call realloc() on every chunk. - // Also, keep track of how much memory is used - if (closure->len + len > closure->max_len) { - uint8_t * new_data; - unsigned new_max_len = closure->max_len; - - // Round up the buffer size to be a multiple of 1024 bytes. - new_max_len = (closure->max_len + len + 1023) & ~1023; - - new_data = (uint8_t *) realloc(closure->data, new_max_len); - if (new_data == NULL) return CAIRO_STATUS_NO_MEMORY; - - // Keep track of how much more memory we just allocated. - V8::AdjustAmountOfExternalAllocatedMemory(new_max_len - closure->max_len); - - closure->data = new_data; - closure->max_len = new_max_len; - } - - memcpy(closure->data + closure->len, data, len); - closure->len += len; - - return CAIRO_STATUS_SUCCESS; + closure_t *closure = (closure_t *) c; + + if (closure->len) { + closure->data = (uint8_t *) realloc(closure->data, closure->len + len); + if (!closure->data) return CAIRO_STATUS_NO_MEMORY; + memcpy(closure->data + closure->len, data, len); + closure->len += len; + } else { + closure->data = (uint8_t *) malloc(len); + if (!closure->data) return CAIRO_STATUS_NO_MEMORY; + memcpy(closure->data, data, len); + closure->len += len; + } + + return CAIRO_STATUS_SUCCESS; } /*