Skip to content

Commit d63a8c2

Browse files
Memory issue (#50)
*Improve import logs *Fix memory leaks causing the process to be killed by the OS *Fix configuration bug *Fix bug losing the node end record for specific cases
1 parent 867c31c commit d63a8c2

10 files changed

Lines changed: 361 additions & 142 deletions

File tree

src/collate.c

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -742,26 +742,57 @@ bool ldb_collate_init(struct ldb_collate_data * collate, struct ldb_table table,
742742

743743
/* Reserve space for collate data */
744744
collate->data = (char *)calloc(LDB_MAX_RECORDS * (collate->rec_width+10), 1);
745-
745+
746746
if (!collate->data)
747747
return false;
748748

749749
collate->tmp_data = (char *)calloc(LDB_MAX_RECORDS * (collate->rec_width+10), 1);
750750

751751
if (!collate->tmp_data)
752+
{
753+
free(collate->data);
752754
return false;
755+
}
753756

754757
/* Set global cmp width (for qsort) */
755758
ldb_cmp_width = max_rec_ln;
756759

757760
/* Open (out) sector */
758761
collate->out_sector = ldb_open(out_table, &sector, "w+");
759762
if (!collate->out_sector)
763+
{
764+
free(collate->data);
765+
free(collate->tmp_data);
760766
return false;
761-
767+
}
768+
762769
return true;
763770
}
764771

772+
/**
773+
* @brief Cleanup collate data structures (frees allocated memory)
774+
*
775+
* @param collate Collate data structure
776+
*/
777+
void ldb_collate_cleanup(struct ldb_collate_data *collate)
778+
{
779+
if (collate->data)
780+
{
781+
free(collate->data);
782+
collate->data = NULL;
783+
}
784+
if (collate->tmp_data)
785+
{
786+
free(collate->tmp_data);
787+
collate->tmp_data = NULL;
788+
}
789+
if (collate->out_sector)
790+
{
791+
fclose(collate->out_sector);
792+
collate->out_sector = NULL;
793+
}
794+
}
795+
765796
void ldb_collate_sector(struct ldb_collate_data *collate, ldb_sector_t * sector)
766797
{
767798
log_info("Collating %s/%s - sector %02x - %s\n", collate->in_table.db, collate->in_table.table, sector->id, sector->data == NULL ? "On disk" : "On RAM");
@@ -787,9 +818,6 @@ void ldb_collate_sector(struct ldb_collate_data *collate, ldb_sector_t * sector)
787818
ldb_import_list(collate);
788819
}
789820

790-
/* Close .out sector */
791-
fclose(collate->out_sector);
792-
793821
/* Move or erase sector */
794822
if (collate->merge)
795823
ldb_sector_erase(collate->in_table, k);
@@ -798,10 +826,12 @@ void ldb_collate_sector(struct ldb_collate_data *collate, ldb_sector_t * sector)
798826

799827
if (collate->del_count)
800828
log_info("%s - sector %02X: %'ld records deleted\n", collate->in_table.table, sector->id, collate->del_count);
801-
829+
802830
log_info("Table %s - sector %2x: collate completed with %'ld records\n", collate->in_table.table , sector->id, collate->rec_count);
803831

804-
free(collate->data);
832+
/* Cleanup collate data structures */
833+
ldb_collate_cleanup(collate);
834+
805835
free(sector->data);
806836
sector->data = NULL;
807837
}
@@ -841,11 +871,16 @@ void ldb_collate(struct ldb_table table, struct ldb_table out_table, int max_rec
841871
ldb_sector_t sector = ldb_load_sector_v2(table, &k0);
842872
//skip unexistent sector.
843873
if (!sector.size)
874+
{
875+
ldb_collate_cleanup(&collate);
876+
if (p_sector >= 0)
877+
break;
844878
continue;
879+
}
845880

846881
ldb_collate_sector(&collate, &sector);
847882
}
848-
883+
849884
if (p_sector >=0)
850885
break;
851886

0 commit comments

Comments
 (0)