Skip to content

Commit 47f4e7c

Browse files
committed
add realloc in json_array
1 parent 9b0a7e6 commit 47f4e7c

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

src/value.c

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ static void array_copy(json_t **dest, size_t dpos, json_t **src, size_t spos,
507507
memcpy(&dest[dpos], &src[spos], count * sizeof(json_t *));
508508
}
509509

510-
static json_t **json_array_grow(json_array_t *array, size_t amount, int copy) {
510+
static json_t **json_array_grow(json_array_t *array, size_t amount) {
511511
size_t new_size;
512512
json_t **old_table, **new_table;
513513

@@ -517,20 +517,15 @@ static json_t **json_array_grow(json_array_t *array, size_t amount, int copy) {
517517
old_table = array->table;
518518

519519
new_size = max(array->size + amount, array->size * 2);
520-
new_table = jsonp_malloc(new_size * sizeof(json_t *));
520+
new_table = jsonp_realloc(old_table, array->size * sizeof(json_t *),
521+
new_size * sizeof(json_t *));
521522
if (!new_table)
522523
return NULL;
523524

524525
array->size = new_size;
525526
array->table = new_table;
526527

527-
if (copy) {
528-
array_copy(array->table, 0, old_table, 0, array->entries);
529-
jsonp_free(old_table);
530-
return array->table;
531-
}
532-
533-
return old_table;
528+
return array->table;
534529
}
535530

536531
int json_array_append_new(json_t *json, json_t *value) {
@@ -545,7 +540,7 @@ int json_array_append_new(json_t *json, json_t *value) {
545540
}
546541
array = json_to_array(json);
547542

548-
if (!json_array_grow(array, 1, 1)) {
543+
if (!json_array_grow(array, 1)) {
549544
json_decref(value);
550545
return -1;
551546
}
@@ -558,7 +553,6 @@ int json_array_append_new(json_t *json, json_t *value) {
558553

559554
int json_array_insert_new(json_t *json, size_t index, json_t *value) {
560555
json_array_t *array;
561-
json_t **old_table;
562556

563557
if (!value)
564558
return -1;
@@ -574,17 +568,11 @@ int json_array_insert_new(json_t *json, size_t index, json_t *value) {
574568
return -1;
575569
}
576570

577-
old_table = json_array_grow(array, 1, 0);
578-
if (!old_table) {
571+
if (!json_array_grow(array, 1)) {
579572
json_decref(value);
580573
return -1;
581574
}
582-
583-
if (old_table != array->table) {
584-
array_copy(array->table, 0, old_table, 0, index);
585-
array_copy(array->table, index + 1, old_table, index, array->entries - index);
586-
jsonp_free(old_table);
587-
} else
575+
if (index != array->entries)
588576
array_move(array, index + 1, index, array->entries - index);
589577

590578
array->table[index] = value;
@@ -638,7 +626,7 @@ int json_array_extend(json_t *json, json_t *other_json) {
638626
array = json_to_array(json);
639627
other = json_to_array(other_json);
640628

641-
if (!json_array_grow(array, other->entries, 1))
629+
if (!json_array_grow(array, other->entries))
642630
return -1;
643631

644632
for (i = 0; i < other->entries; i++)

0 commit comments

Comments
 (0)