Example #1
0
void spn_array_insert(SpnArray *arr, size_t index, const SpnValue *val)
{
	size_t i, maxindex;

	/* index == arr->count is allowed (insertion at end) */
	if (index > arr->count) {
		unsigned long ulindex = index, ulcount = arr->count;
		spn_die("array index %lu is too high (size = %lu)\n", ulindex, ulcount);
	}

	maxindex = arr->count++;

	if (arr->allocsize == 0) {
		arr->allocsize = 8;
		arr->vector = spn_malloc(arr->allocsize * sizeof arr->vector[0]);
	}

	if (arr->count > arr->allocsize) {
		arr->allocsize *= 2;
		arr->vector = spn_realloc(arr->vector, arr->allocsize * sizeof arr->vector[0]);
	}

	for (i = maxindex; i > index; i--) {
		arr->vector[i] = arr->vector[i - 1];
	}

	spn_value_retain(val);
	arr->vector[index] = *val;
}
Example #2
0
static void expand_buffer(struct string_builder *bld, size_t extra)
{
	if (bld->allocsz < bld->len + extra) {
		while (bld->allocsz < bld->len + extra) {
			bld->allocsz *= 2;
		}

		bld->buf = spn_realloc(bld->buf, bld->allocsz);
	}
}