get_shallow_commits: Avoid memory leak if a commit has been reached already.
Hi,
Quoted 2 lines · Junio C Hamano
I think you could pretend the
commit->utilfield to be of some kind of int and avoid the allocation altogether ;-).
I actually thought about that, and found it too dangerous. But of course, since we need at least a 32-bit architecture, chances are very high that a pointer is castable to an int.
Ciao,
Dscho
Hi,
Quoted 4 lines · Anand Kumria
Any reason you didn't do:
if (!commit->util)
commit->util = xcalloc(1, sizeof(int));
xmalloc() does not initialize the memory. Therefore, it is usually preferred if you initialize the memory yourself. In this case, the memory is initialized to 0, even if it is not allocated.
It may be a minor performance issue, but it is a good habit to use xcalloc only if it is needed.
Ciao,
Dscho
Quoted 7 lines · Johannes Schindelin
Quoted 4 lines · Anand Kumria
Any reason you didn't do:
if (!commit->util)
commit->util = xcalloc(1, sizeof(int));
Quoted 6 lines
xmalloc()does not initialize the memory. Therefore, it is usually preferred if you initialize the memory yourself. In this case, the memory is initialized to 0, even if it is not allocated.It may be a minor performance issue, but it is a good habit to use xcalloc only if it is needed.
I think you could pretend the commit->util field to be of some kind of int and avoid the allocation altogether ;-).
PATCH 3/5 get_shallow_commits: Avoid memory leak if a commit has been reached already.
--- shallow.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)
shallow.c +3 -1
@@ -60,7 +60,9 @@ struct commit_list *get_shallow_commits( commit =NULL ;continue ; } - commit->util =xcalloc (1 ,sizeof (int )); +if (!commit->util) + commit->util =xmalloc (sizeof (int )); + *(int *)commit->util =0 ; cur_depth =0 ; }else { commit = (struct commit *)
Signature · 4 lines
1.4.4.1.ga335e -- Alexandre Julliard
Quoted 10 lines · Alexandre Julliard
Signed-off-by Alexandre Julliard <[email protected]>--- shallow.c | 4 +++- 1 files changed, 3 insertions(+), 1 deletions(-)shallow.c +0
-0
shallow.c struct commit_list *get_shallow_commits( · line 66
Quoted 8 lines
shallow.c +3
-1@@ -60,7 +60,9 @@ struct commit_list *get_shallow_commits( commit =NULL ;continue ; } - commit->util =xcalloc (1 ,sizeof (int )); +if (!commit->util) + commit->util =xmalloc (sizeof (int )); + *(int *)commit->util =0 ; cur_depth =0 ;
Hi,
Any reason you didn't do:
if (!commit->util)
commit->util = xcalloc(1, sizeof(int));
That would seem to be the same.
Regards,
Anand