@@ -507,7 +507,7 @@ static void array_copy(json_t **dest, size_t dpos, json_t **src, size_t spos,
507
507
memcpy (& dest [dpos ], & src [spos ], count * sizeof (json_t * ));
508
508
}
509
509
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 ) {
511
511
size_t new_size ;
512
512
json_t * * old_table , * * new_table ;
513
513
@@ -517,20 +517,15 @@ static json_t **json_array_grow(json_array_t *array, size_t amount, int copy) {
517
517
old_table = array -> table ;
518
518
519
519
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 * ));
521
522
if (!new_table )
522
523
return NULL ;
523
524
524
525
array -> size = new_size ;
525
526
array -> table = new_table ;
526
527
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 ;
534
529
}
535
530
536
531
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) {
545
540
}
546
541
array = json_to_array (json );
547
542
548
- if (!json_array_grow (array , 1 , 1 )) {
543
+ if (!json_array_grow (array , 1 )) {
549
544
json_decref (value );
550
545
return -1 ;
551
546
}
@@ -558,7 +553,6 @@ int json_array_append_new(json_t *json, json_t *value) {
558
553
559
554
int json_array_insert_new (json_t * json , size_t index , json_t * value ) {
560
555
json_array_t * array ;
561
- json_t * * old_table ;
562
556
563
557
if (!value )
564
558
return -1 ;
@@ -574,17 +568,11 @@ int json_array_insert_new(json_t *json, size_t index, json_t *value) {
574
568
return -1 ;
575
569
}
576
570
577
- old_table = json_array_grow (array , 1 , 0 );
578
- if (!old_table ) {
571
+ if (!json_array_grow (array , 1 )) {
579
572
json_decref (value );
580
573
return -1 ;
581
574
}
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 )
588
576
array_move (array , index + 1 , index , array -> entries - index );
589
577
590
578
array -> table [index ] = value ;
@@ -638,7 +626,7 @@ int json_array_extend(json_t *json, json_t *other_json) {
638
626
array = json_to_array (json );
639
627
other = json_to_array (other_json );
640
628
641
- if (!json_array_grow (array , other -> entries , 1 ))
629
+ if (!json_array_grow (array , other -> entries ))
642
630
return -1 ;
643
631
644
632
for (i = 0 ; i < other -> entries ; i ++ )
0 commit comments