commit 6402fb3cc1e54689a0b577e5958bf75a5a7e798b from: Stefan Sperling date: Sat Sep 15 11:21:51 2018 UTC change got_object_get_id() semantics; avoids pointless allocations commit - f2e80f4b6d7a56834b6c44f055317c88c833d277 commit + 6402fb3cc1e54689a0b577e5958bf75a5a7e798b blob - 35b21af9e318560aaca023f05525b854d82e4b56 blob + a506816c409702b75d36358f67e56799c415fb42 --- got/got.c +++ got/got.c @@ -386,19 +386,9 @@ detect_change(int *changed, struct got_object_id *comm } id = got_object_get_id(obj); - if (id == NULL) - return got_error_from_errno(); pid = got_object_get_id(pobj); - if (pid == NULL) { - err = got_error_from_errno(); - free(id); - return err; - } - *changed = (got_object_id_cmp(id, pid) != 0); got_object_close(pobj); - free(id); - free(pid); return NULL; } @@ -601,7 +591,7 @@ cmd_log(int argc, char *argv[]) start_commit); if (error != NULL) return error; - id = got_object_get_id(obj); + id = got_object_id_dup(got_object_get_id(obj)); if (id == NULL) error = got_error_from_errno(); } @@ -652,7 +642,6 @@ cmd_diff(int argc, char *argv[]) { const struct got_error *error; struct got_repository *repo = NULL; - struct got_object_id *id1 = NULL, *id2 = NULL; struct got_object *obj1 = NULL, *obj2 = NULL; char *repo_path = NULL; char *obj_id_str1 = NULL, *obj_id_str2 = NULL; @@ -698,21 +687,11 @@ cmd_diff(int argc, char *argv[]) goto done; error = got_object_open_by_id_str(&obj1, repo, obj_id_str1); - if (error == NULL) { - id1 = got_object_get_id(obj1); - if (id1 == NULL) - error = got_error_from_errno(); - } - if (error != NULL) + if (error) goto done; error = got_object_open_by_id_str(&obj2, repo, obj_id_str2); - if (error == NULL) { - id2 = got_object_get_id(obj2); - if (id2 == NULL) - error = got_error_from_errno(); - } - if (error != NULL) + if (error) goto done; if (got_object_get_type(obj1) != got_object_get_type(obj2)) { @@ -741,10 +720,6 @@ done: got_object_close(obj1); if (obj2) got_object_close(obj2); - if (id1) - free(id1); - if (id2) - free(id2); if (repo) { const struct got_error *repo_error; repo_error = got_repo_close(repo); @@ -837,7 +812,7 @@ cmd_blame(int argc, char *argv[]) error = got_object_open_by_id_str(&obj, repo, commit_id_str); if (error != NULL) goto done; - commit_id = got_object_get_id(obj); + commit_id = got_object_id_dup(got_object_get_id(obj)); if (commit_id == NULL) error = got_error_from_errno(); got_object_close(obj); blob - e59ee13190becb39193c375dab2908862dc51743 blob + 3b8ab2ca2d452058d45776035f263e51bf273ce6 --- include/got_object.h +++ include/got_object.h @@ -88,7 +88,8 @@ struct got_object_id *got_object_id_dup(struct got_obj /* * Get a newly allocated copy of an object's ID. - * The caller should dispose of it with free(3). + * The caller must treat the ID as read-only and must not call free(3) on it. + * Use got_object_id_dup() to get a writable copy. */ struct got_object_id *got_object_get_id(struct got_object *); blob - e0a3cb4106c801ab364f7421d420e633af511524 blob + f3e4cc89ab7b9cd52cad9abd652fde93bb1bbcfb --- lib/object.c +++ lib/object.c @@ -77,7 +77,7 @@ got_object_id_dup(struct got_object_id *id1) struct got_object_id * got_object_get_id(struct got_object *obj) { - return got_object_id_dup(&obj->id); + return &obj->id; } const struct got_error * blob - d7c5c6828911b898e727a0cf6de3c87c8dd9c406 blob + 1e419f4ae1d5eb9ccc0b34b5c9bbf6c10ddae989 --- tog/tog.c +++ tog/tog.c @@ -842,25 +842,10 @@ queue_commits(struct got_commit_graph *graph, struct c } else { struct got_object_id *id, *pid; id = got_object_get_id(obj); - if (id == NULL) { - err = got_error_from_errno(); - got_object_close(obj); - got_object_close(pobj); - break; - } pid = got_object_get_id(pobj); - if (pid == NULL) { - err = got_error_from_errno(); - free(id); - got_object_close(obj); - got_object_close(pobj); - break; - } changed = (got_object_id_cmp(id, pid) != 0); got_object_close(pobj); - free(id); - free(pid); } } got_object_close(obj); @@ -1455,7 +1440,7 @@ cmd_log(int argc, char *argv[]) struct got_object *obj; error = got_object_open_by_id_str(&obj, repo, start_commit); if (error == NULL) { - start_id = got_object_get_id(obj); + start_id = got_object_id_dup(got_object_get_id(obj)); if (start_id == NULL) error = got_error_from_errno(); goto done; @@ -1607,8 +1592,6 @@ open_diff_view(struct tog_view *view, struct got_objec view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL; view->state.diff.id2 = got_object_get_id(obj2); - if (view->state.diff.id2 == NULL) - return got_error_from_errno(); view->state.diff.f = f; view->state.diff.first_displayed_line = 1; view->state.diff.last_displayed_line = view->nlines; @@ -1627,8 +1610,6 @@ close_diff_view(struct tog_view *view) if (view->state.diff.f && fclose(view->state.diff.f) == EOF) err = got_error_from_errno(); - free(view->state.diff.id1); - free(view->state.diff.id2); return err; } @@ -2355,14 +2336,8 @@ input_blame_view(struct tog_view **new_view, struct to if (pobj) { got_object_close(pobj); pobj = NULL; - } - if (id == NULL) { - err = got_error_from_errno(); - break; } - err = got_object_qid_alloc( - &s->blamed_commit, id); - free(id); + err = got_object_qid_alloc(&s->blamed_commit, id); if (err) goto done; SIMPLEQ_INSERT_HEAD(&s->blamed_commits, @@ -2550,7 +2525,7 @@ cmd_blame(int argc, char *argv[]) error = got_object_open_by_id_str(&obj, repo, commit_id_str); if (error != NULL) goto done; - commit_id = got_object_get_id(obj); + commit_id = got_object_id_dup(got_object_get_id(obj)); if (commit_id == NULL) error = got_error_from_errno(); got_object_close(obj); @@ -3097,7 +3072,7 @@ cmd_tree(int argc, char *argv[]) struct got_object *obj; error = got_object_open_by_id_str(&obj, repo, commit_id_arg); if (error == NULL) { - commit_id = got_object_get_id(obj); + commit_id = got_object_id_dup(got_object_get_id(obj)); if (commit_id == NULL) error = got_error_from_errno(); }