[systemd-devel] [PATCH 3/3] bootchart: Convert malloc/memset to calloc.
David Strauss
david at davidstrauss.net
Thu Jan 10 16:15:49 PST 2013
+1, especially for the reduced chance of changes happening to the
malloc but not the memset causing mysterious bug.
On Fri, Jan 11, 2013 at 5:35 AM, Auke Kok <auke-jan.h.kok at intel.com> wrote:
> ---
> src/bootchart/bootchart.c | 5 ++---
> src/bootchart/log.c | 10 ++++------
> 2 files changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/src/bootchart/bootchart.c b/src/bootchart/bootchart.c
> index 7bcfd98..37d8fbe 100644
> --- a/src/bootchart/bootchart.c
> +++ b/src/bootchart/bootchart.c
> @@ -232,12 +232,11 @@ int main(int argc, char *argv[])
> }
>
> /* start with empty ps LL */
> - ps_first = malloc(sizeof(struct ps_struct));
> + ps_first = calloc(1, sizeof(struct ps_struct));
> if (!ps_first) {
> - perror("malloc(ps_struct)");
> + perror("calloc(ps_struct)");
> exit(EXIT_FAILURE);
> }
> - memset(ps_first, 0, sizeof(struct ps_struct));
>
> /* handle TERM/INT nicely */
> memset(&sig, 0, sizeof(struct sigaction));
> diff --git a/src/bootchart/log.c b/src/bootchart/log.c
> index ff70e2d..48002fa 100644
> --- a/src/bootchart/log.c
> +++ b/src/bootchart/log.c
> @@ -225,21 +225,19 @@ schedstat_next:
> char t[32];
> struct ps_struct *parent;
>
> - ps->next_ps = malloc(sizeof(struct ps_struct));
> + ps->next_ps = calloc(1, sizeof(struct ps_struct));
> if (!ps->next_ps) {
> - perror("malloc(ps_struct)");
> + perror("calloc(ps_struct)");
> exit (EXIT_FAILURE);
> }
> - memset(ps->next_ps, 0, sizeof(struct ps_struct));
> ps = ps->next_ps;
> ps->pid = pid;
>
> - ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
> + ps->sample = calloc(len + 1, sizeof(struct ps_sched_struct));
> if (!ps->sample) {
> - perror("malloc(ps_struct)");
> + perror("calloc(ps_struct)");
> exit (EXIT_FAILURE);
> }
> - memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
>
> pscount++;
>
> --
> 1.7.11.2
>
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
David Strauss
| david at davidstrauss.net
| +1 512 577 5827 [mobile]
More information about the systemd-devel
mailing list