void cork_buffer_ensure_size(struct cork_buffer *buffer, size_t desired_size) { if (buffer->allocated_size >= desired_size) { return; } /* Make sure we at least double the old size when reallocating. */ size_t new_size = buffer->allocated_size * 2; if (desired_size > new_size) { new_size = desired_size; } buffer->buf = cork_realloc(buffer->buf, new_size); buffer->allocated_size = new_size; }
static void cork_buffer_ensure_size_int(struct cork_buffer *buffer, size_t desired_size) { size_t new_size; if (CORK_LIKELY(buffer->allocated_size >= desired_size)) { return; } /* Make sure we at least double the old size when reallocating. */ new_size = buffer->allocated_size * 2; if (desired_size > new_size) { new_size = desired_size; } buffer->buf = cork_realloc(buffer->buf, buffer->allocated_size, new_size); buffer->allocated_size = new_size; }
void cork_array_ensure_size_(void *varray, size_t desired_count, size_t element_size) { struct cork_array_private *array = varray; if ((array->allocated_size == 0) && (desired_count < CORK_INTERNAL_COUNT_FROM_SIZE(element_size))) { /* There's already enough space in our internal storage, which * we're still using. */ return; } /* Otherwise reallocate if there's not enough space in our * heap-allocated array. */ size_t desired_size = desired_count * element_size; if (array->allocated_size == 0) { size_t old_size = element_size * CORK_INTERNAL_COUNT_FROM_SIZE(element_size); size_t new_size = CORK_ARRAY_SIZE; if (desired_size > new_size) { new_size = desired_size; } DEBUG("--- Array %p: Allocating %zu->%zu bytes", array, old_size, new_size); array->items = cork_malloc(new_size); memcpy(array->items, &array->internal, old_size); array->allocated_size = new_size; } else if (desired_size > array->allocated_size) { size_t new_size = array->allocated_size * 2; if (desired_size > new_size) { new_size = desired_size; } DEBUG("--- Array %p: Reallocating %zu->%zu bytes", array, array->allocated_size, new_size); array->items = cork_realloc(array->items, new_size); array->allocated_size = new_size; } }