summaryrefslogtreecommitdiffhomepage
path: root/types.c
diff options
context:
space:
mode:
Diffstat (limited to 'types.c')
-rw-r--r--types.c36
1 files changed, 9 insertions, 27 deletions
diff --git a/types.c b/types.c
index c4bc456..9751e24 100644
--- a/types.c
+++ b/types.c
@@ -705,17 +705,13 @@ ucv_array_new_length(uc_vm_t *vm, size_t length)
{
uc_array_t *array;
- /* XXX */
- length = 0;
-
- array = xalloc(sizeof(*array) + length * sizeof(array->entries[0]));
+ array = xalloc(sizeof(*array));
array->header.type = UC_ARRAY;
array->header.refcount = 1;
- if (length > 0)
- array->count = length;
-
- uc_vector_grow(array);
+ /* preallocate memory */
+ if (length)
+ uc_vector_extend(array, length);
if (vm) {
ucv_ref(&vm->values, &array->ref);
@@ -779,10 +775,9 @@ ucv_array_unshift(uc_value_t *uv, uc_value_t *item)
if (ucv_type(uv) != UC_ARRAY)
return NULL;
- array->count++;
- uc_vector_grow(array);
+ uc_vector_extend(array, 1);
- for (i = array->count; i > 1; i--)
+ for (i = ++array->count; i > 1; i--)
array->entries[i - 1] = array->entries[i - 2];
array->entries[0] = item;
@@ -826,10 +821,9 @@ ucv_array_delete(uc_value_t *uv, size_t offset, size_t count)
&array->entries[offset + count],
(array->count - (offset + count)) * sizeof(array->entries[0]));
+ uc_vector_reduce(array, count);
array->count -= count;
- uc_vector_grow(array);
-
return true;
}
@@ -837,24 +831,13 @@ bool
ucv_array_set(uc_value_t *uv, size_t index, uc_value_t *item)
{
uc_array_t *array = (uc_array_t *)uv;
- size_t old_count, new_count;
if (ucv_type(uv) != UC_ARRAY)
return false;
if (index >= array->count) {
- old_count = array->count;
- new_count = (index + 1) & ~(UC_VECTOR_CHUNK_SIZE - 1);
-
- if (new_count > old_count) {
- array->count = new_count;
- uc_vector_grow(array);
- }
-
+ uc_vector_extend(array, index + 1 - array->count);
array->count = index + 1;
-
- while (old_count < array->count)
- array->entries[old_count++] = NULL;
}
else {
ucv_put(array->entries[index]);
@@ -1151,8 +1134,7 @@ ucv_resource_type_add(uc_vm_t *vm, const char *name, uc_value_t *proto, void (*f
type->proto = proto;
type->free = freefn;
- uc_vector_grow(&vm->restypes);
- vm->restypes.entries[vm->restypes.count++] = type;
+ uc_vector_push(&vm->restypes, type);
return type;
}