@@ -486,11 +486,14 @@ struct ggml_gallocr {
486486
487487 struct ggml_hash_set hash_set ;
488488 struct hash_node * hash_values ; // [hash_set.size]
489+ size_t hash_values_cap ;
489490
490- struct node_alloc * node_allocs ; // [n_nodes]
491+ struct node_alloc * node_allocs ; // [node_allocs_cap]
492+ size_t node_allocs_cap ;
491493 int n_nodes ;
492494
493- struct leaf_alloc * leaf_allocs ; // [n_leafs]
495+ struct leaf_alloc * leaf_allocs ; // [leaf_allocs_cap]
496+ size_t leaf_allocs_cap ;
494497 int n_leafs ;
495498};
496499
@@ -833,9 +836,9 @@ static bool ggml_gallocr_reserve_n_impl(
833836 galloc -> hash_set = ggml_hash_set_new (min_hash_size );
834837 GGML_ASSERT (galloc -> hash_set .keys != NULL );
835838
836- free (galloc -> hash_values );
837- galloc -> hash_values = malloc (sizeof (struct hash_node ) * galloc -> hash_set .size );
839+ galloc -> hash_values = realloc (galloc -> hash_values , sizeof (struct hash_node ) * galloc -> hash_set .size );
838840 GGML_ASSERT (galloc -> hash_values != NULL );
841+ galloc -> hash_values_cap = galloc -> hash_set .size ;
839842 }
840843
841844 // reset allocators
@@ -847,10 +850,13 @@ static bool ggml_gallocr_reserve_n_impl(
847850 ggml_gallocr_alloc_graph_impl (galloc , graph , node_buffer_ids , leaf_buffer_ids );
848851
849852 // set the node_allocs from the hash table
850- if (galloc -> n_nodes < graph -> n_nodes ) {
851- free (galloc -> node_allocs );
852- galloc -> node_allocs = calloc (graph -> n_nodes , sizeof (struct node_alloc ));
853+ if (galloc -> node_allocs_cap < (size_t )graph -> n_nodes ) {
854+ galloc -> node_allocs = realloc (galloc -> node_allocs , sizeof (struct node_alloc ) * (size_t )graph -> n_nodes );
853855 GGML_ASSERT (galloc -> node_allocs != NULL );
856+ // Zero the newly allocated portion (realloc doesn't zero extended memory)
857+ memset ((char * )galloc -> node_allocs + galloc -> node_allocs_cap * sizeof (struct node_alloc ),
858+ 0 , ((size_t )graph -> n_nodes - galloc -> node_allocs_cap ) * sizeof (struct node_alloc ));
859+ galloc -> node_allocs_cap = graph -> n_nodes ;
854860 }
855861 galloc -> n_nodes = graph -> n_nodes ;
856862 for (int i = 0 ; i < graph -> n_nodes ; i ++ ) {
@@ -880,10 +886,13 @@ static bool ggml_gallocr_reserve_n_impl(
880886 }
881887 }
882888 }
883- if (galloc -> n_leafs < graph -> n_leafs ) {
884- free (galloc -> leaf_allocs );
885- galloc -> leaf_allocs = calloc (graph -> n_leafs , sizeof (galloc -> leaf_allocs [0 ]));
889+ if (galloc -> leaf_allocs_cap < (size_t )graph -> n_leafs ) {
890+ galloc -> leaf_allocs = realloc (galloc -> leaf_allocs , sizeof (galloc -> leaf_allocs [0 ]) * (size_t )graph -> n_leafs );
886891 GGML_ASSERT (galloc -> leaf_allocs != NULL );
892+ // Zero the newly allocated portion
893+ memset ((char * )galloc -> leaf_allocs + galloc -> leaf_allocs_cap * sizeof (galloc -> leaf_allocs [0 ]),
894+ 0 , ((size_t )graph -> n_leafs - galloc -> leaf_allocs_cap ) * sizeof (galloc -> leaf_allocs [0 ]));
895+ galloc -> leaf_allocs_cap = graph -> n_leafs ;
887896 }
888897 galloc -> n_leafs = graph -> n_leafs ;
889898 for (int i = 0 ; i < graph -> n_leafs ; i ++ ) {
0 commit comments