Skip to content

Commit f8bdf31

Browse files
pks-tgitster
authored andcommitted
odb: refactor odb_clear() to odb_free()
The function `odb_clear()` releases all resources allocated to an object database and ensures that all fields become zero'd out. Despite its naming though it doesn't really clear the object database so that it becomes ready for reuse afterwards again -- the caller would first have to reinitialize it, and that contradicts the terminology of "clearing" as we have defined it in our coding guidelines. There isn't really only a reason to have "clearing" semantics, either. There's only a single caller of `odb_clear()`, and that caller also ends up freeing the object database structure itself. Refactor the function to have "freeing" semantics instead, so that the structure itself is also freed, which allows us to drop some useless boilerplate to zero out the structure's members. This refactoring reveals that we're trying to close the commit graph multiple times: once directly via `free_commit_graph()`, and once via `odb_close()`. Drop the former call. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9aaba57 commit f8bdf31

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

odb.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,30 +1073,27 @@ static void odb_free_sources(struct object_database *o)
10731073
o->source_by_path = NULL;
10741074
}
10751075

1076-
void odb_clear(struct object_database *o)
1076+
void odb_free(struct object_database *o)
10771077
{
1078-
FREE_AND_NULL(o->alternate_db);
1078+
if (!o)
1079+
return;
1080+
1081+
free(o->alternate_db);
10791082

10801083
oidmap_clear(&o->replace_map, 1);
10811084
pthread_mutex_destroy(&o->replace_mutex);
10821085

1083-
free_commit_graph(o->commit_graph);
1084-
o->commit_graph = NULL;
1085-
o->commit_graph_attempted = 0;
1086-
10871086
odb_free_sources(o);
1088-
o->sources_tail = NULL;
1089-
o->loaded_alternates = 0;
10901087

10911088
for (size_t i = 0; i < o->cached_object_nr; i++)
10921089
free((char *) o->cached_objects[i].value.buf);
1093-
FREE_AND_NULL(o->cached_objects);
1090+
free(o->cached_objects);
10941091

10951092
odb_close(o);
10961093
packfile_store_free(o->packfiles);
1097-
o->packfiles = NULL;
1098-
10991094
string_list_clear(&o->submodule_source_paths, 0);
1095+
1096+
free(o);
11001097
}
11011098

11021099
void odb_reprepare(struct object_database *o)

odb.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,9 @@ struct object_database {
167167
};
168168

169169
struct object_database *odb_new(struct repository *repo);
170-
void odb_clear(struct object_database *o);
170+
171+
/* Free the object database and release all resources. */
172+
void odb_free(struct object_database *o);
171173

172174
/*
173175
* Close the object database and all of its sources so that any held resources

repository.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ void repo_clear(struct repository *repo)
382382
FREE_AND_NULL(repo->worktree);
383383
FREE_AND_NULL(repo->submodule_prefix);
384384

385-
odb_clear(repo->objects);
386-
FREE_AND_NULL(repo->objects);
385+
odb_free(repo->objects);
386+
repo->objects = NULL;
387387

388388
parsed_object_pool_clear(repo->parsed_objects);
389389
FREE_AND_NULL(repo->parsed_objects);

0 commit comments

Comments
 (0)