From patchwork Fri Mar 23 14:30:23 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janne Blomqvist X-Patchwork-Id: 148444 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id D10FAB6ED0 for ; Sat, 24 Mar 2012 01:31:29 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1333117891; h=Comment: DomainKey-Signature:Received:Received:Received:Received: MIME-Version:Received:Received:Date:Message-ID:Subject:From:To: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=/WOWWOz s6k1E8F0thh3ROM+0Iyc=; b=daU9lBZWgOCNj3E5P3Xq6hliX2U92B3ocsDO9es SChI1QrGY9D2Xpl9LmEQDmjrzxnBCMcQDRRka0G5s+ZU9QlooVzn3mqbb1FMBnOP qizXyMxGSsSvXzSPfrUdGIdcunjxYECT5tCB4Gw9mzu6dZoexyl4vKPwMyy474Nm UFno= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:MIME-Version:Received:Received:Date:Message-ID:Subject:From:To:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=deVuWaj14BWcfXww9cHXo8pnci4ebNmAJxOcV6OmCLmu5JQn36zYykUOviQzJb jsSyxLitIm3G3R8U7wgQAPZ2IuMzJfe4EhkVkIxSZtCdBiKOVJJRI1xBrT41LEfb awOEfYKoS+fQkLj4Xu70b0q47Mj5PcNwGODIbiWaF+UqE=; Received: (qmail 29351 invoked by alias); 23 Mar 2012 14:31:10 -0000 Received: (qmail 29197 invoked by uid 22791); 23 Mar 2012 14:31:06 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, TW_BG X-Spam-Check-By: sourceware.org Received: from mail-lb0-f175.google.com (HELO mail-lb0-f175.google.com) (209.85.217.175) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 23 Mar 2012 14:30:25 +0000 Received: by lbbgi4 with SMTP id gi4so2613675lbb.20 for ; Fri, 23 Mar 2012 07:30:23 -0700 (PDT) MIME-Version: 1.0 Received: by 10.152.146.163 with SMTP id td3mr8902416lab.31.1332513023820; Fri, 23 Mar 2012 07:30:23 -0700 (PDT) Received: by 10.152.129.105 with HTTP; Fri, 23 Mar 2012 07:30:23 -0700 (PDT) Date: Fri, 23 Mar 2012 16:30:23 +0200 Message-ID: Subject: [Patch, libfortran] Use calloc instead of malloc+memset From: Janne Blomqvist To: Fortran List , GCC Patches Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi, in a few places in libgfortran we use malloc (or rather, our own xmalloc-like wrapper, get_mem) followed by a memset to zero the memory. The attached patch replaces this usage with calloc(). Regtested on x86_64-unknown-linux-gnu, unless somebody objects I'll commit it as obvious in a few days. 2012-03-23 Janne Blomqvist * runtime/memory.c (xcalloc): New function. * libgfortran.h (xcalloc): New prototype. * io/list_read.c (push_char): Use xcalloc instead of get_mem and memset. (l_push_char): Likewise. * io/unit.c (insert_unit): Likewise. (get_internal_unit): Likewise. * io/unix.c (open_internal): Likewise. (open_internal4): Likewise. (fd_to_stream): Likewise. diff --git a/libgfortran/io/list_read.c b/libgfortran/io/list_read.c index 11a35c9..91b345c 100644 --- a/libgfortran/io/list_read.c +++ b/libgfortran/io/list_read.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011 +/* Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. Contributed by Andy Vaught Namelist input contributed by Paul Thomas @@ -75,9 +75,8 @@ push_char (st_parameter_dt *dtp, char c) if (dtp->u.p.saved_string == NULL) { - dtp->u.p.saved_string = get_mem (SCRATCH_SIZE); - // memset below should be commented out. - memset (dtp->u.p.saved_string, 0, SCRATCH_SIZE); + // Plain malloc should suffice here, zeroing not needed? + dtp->u.p.saved_string = xcalloc (SCRATCH_SIZE, 1); dtp->u.p.saved_length = SCRATCH_SIZE; dtp->u.p.saved_used = 0; } @@ -622,10 +621,7 @@ static void l_push_char (st_parameter_dt *dtp, char c) { if (dtp->u.p.line_buffer == NULL) - { - dtp->u.p.line_buffer = get_mem (SCRATCH_SIZE); - memset (dtp->u.p.line_buffer, 0, SCRATCH_SIZE); - } + dtp->u.p.line_buffer = xcalloc (SCRATCH_SIZE, 1); dtp->u.p.line_buffer[dtp->u.p.item_count++] = c; } diff --git a/libgfortran/io/unit.c b/libgfortran/io/unit.c index 819d0e9..5fe9eb2 100644 --- a/libgfortran/io/unit.c +++ b/libgfortran/io/unit.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011 +/* Copyright (C) 2002, 2003, 2005, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc. Contributed by Andy Vaught F2003 I/O support contributed by Jerry DeLisle @@ -188,8 +188,7 @@ insert (gfc_unit *new, gfc_unit *t) static gfc_unit * insert_unit (int n) { - gfc_unit *u = get_mem (sizeof (gfc_unit)); - memset (u, '\0', sizeof (gfc_unit)); + gfc_unit *u = xcalloc (1, sizeof (gfc_unit)); u->unit_number = n; #ifdef __GTHREAD_MUTEX_INIT { @@ -385,14 +384,8 @@ get_internal_unit (st_parameter_dt *dtp) /* Allocate memory for a unit structure. */ - iunit = get_mem (sizeof (gfc_unit)); - if (iunit == NULL) - { - generate_error (&dtp->common, LIBERROR_INTERNAL_UNIT, NULL); - return NULL; - } + iunit = xcalloc (1, sizeof (gfc_unit)); - memset (iunit, '\0', sizeof (gfc_unit)); #ifdef __GTHREAD_MUTEX_INIT { __gthread_mutex_t tmp = __GTHREAD_MUTEX_INIT; diff --git a/libgfortran/io/unix.c b/libgfortran/io/unix.c index 978c3ff..31fa005 100644 --- a/libgfortran/io/unix.c +++ b/libgfortran/io/unix.c @@ -1,5 +1,5 @@ /* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, - 2011 + 2011, 2012 Free Software Foundation, Inc. Contributed by Andy Vaught F2003 I/O support contributed by Jerry DeLisle @@ -919,13 +919,11 @@ open_internal (char *base, int length, gfc_offset offset) { unix_stream *s; - s = get_mem (sizeof (unix_stream)); - memset (s, '\0', sizeof (unix_stream)); + s = xcalloc (1, sizeof (unix_stream)); s->buffer = base; s->buffer_offset = offset; - s->logical_offset = 0; s->active = s->file_length = length; s->st.vptr = &mem_vtable; @@ -941,13 +939,11 @@ open_internal4 (char *base, int length, gfc_offset offset) { unix_stream *s; - s = get_mem (sizeof (unix_stream)); - memset (s, '\0', sizeof (unix_stream)); + s = xcalloc (1, sizeof (unix_stream)); s->buffer = base; s->buffer_offset = offset; - s->logical_offset = 0; s->active = s->file_length = length; s->st.vptr = &mem4_vtable; @@ -965,13 +961,9 @@ fd_to_stream (int fd) struct stat statbuf; unix_stream *s; - s = get_mem (sizeof (unix_stream)); - memset (s, '\0', sizeof (unix_stream)); + s = xcalloc (1, sizeof (unix_stream)); s->fd = fd; - s->buffer_offset = 0; - s->physical_offset = 0; - s->logical_offset = 0; /* Get the current length of the file. */ diff --git a/libgfortran/libgfortran.h b/libgfortran/libgfortran.h index f2f6c36..186bfbe 100644 --- a/libgfortran/libgfortran.h +++ b/libgfortran/libgfortran.h @@ -759,6 +759,10 @@ internal_proto(get_mem); extern void *internal_malloc_size (size_t) __attribute__ ((malloc)); internal_proto(internal_malloc_size); +extern void *xcalloc (size_t, size_t) __attribute__ ((malloc)); +internal_proto(xcalloc); + + /* environ.c */ extern int check_buffered (int); diff --git a/libgfortran/runtime/memory.c b/libgfortran/runtime/memory.c index a26d9e5..044a115 100644 --- a/libgfortran/runtime/memory.c +++ b/libgfortran/runtime/memory.c @@ -1,5 +1,6 @@ /* Memory management routines. - Copyright 2002, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc. + Copyright 2002, 2005, 2006, 2007, 2009, 2010, 2012 + Free Software Foundation, Inc. Contributed by Paul Brook This file is part of the GNU Fortran runtime library (libgfortran). @@ -59,3 +60,19 @@ internal_malloc_size (size_t size) return get_mem (size); } + + +/* calloc wrapper that aborts on error. */ + +void * +xcalloc (size_t nmemb, size_t size) +{ + if (nmemb * size == 0) + nmemb = size = 1; + + void *p = calloc (nmemb, size); + if (!p) + os_error ("Allocating cleared memory failed"); + + return p; +}