Patchwork [fortran] Always return malloc(1) for empty arrays in the library

login
register
mail settings
Submitter Thomas Koenig
Date July 1, 2011, 6:31 p.m.
Message ID <4E0E1269.2070007@netcologne.de>
Download mbox | patch
Permalink /patch/102944/
State New
Headers show

Comments

Thomas Koenig - July 1, 2011, 6:31 p.m.
Am 01.07.2011 14:34, schrieb Janne Blomqvist:
> On Thu, Jun 30, 2011 at 21:09, Thomas Koenig<tkoenig@netcologne.de>  wrote:
>> Good point.  I have done so in the attached patch
>
> Seems you forgot to attach it.. ;)
>

Oops, I hadn't realized your crystal ball was broken :-)

Is this better?

	Thomas

2011-06-30  Thomas Koenig  <tkoenig@gcc.gnu.org>

         * runtime/memory.c (internal_malloc_size):  If size is zero or
         less, allocate a single byte.
         * m4/pack.m4 (pack_'rtype_code`):  Don't check for zero size
         for the argument of internal_malloc_size.
         * m4/spread.m4 (spread_'rtype_code`):  Likewise.
         * m4/eoshift1.m4 (eoshift1):  Don't allocate twice.  Don't check
         for zero size for the argument of internal_malloc_size.
         * m4/eoshift3.m4: Don't check for zero size for the argument of
         internal_malloc_size.
         * intrinsics/pack_generic.c (pack_internal):  Likewise.
         (pack_s_internal):  Likewise.
         * intrinsics/cshift0.c (cshift0):  Likewise.
         * intrinsics/spread_generic.c (spread_internal):  Likewise.
         * intrinsics/eoshift0.c (eoshift0):  Likewise.
         * intrinsics/eoshift2.c (eoshift2):  Likewise.
         * generated/eoshift1_16.c: Regenerated.
         * generated/eoshift1_4.c: Regenerated.
         * generated/eoshift1_8.c: Regenerated.
         * generated/eoshift3_16.c: Regenerated.
         * generated/eoshift3_4.c: Regenerated.
         * generated/eoshift3_8.c: Regenerated.
         * generated/pack_c10.c: Regenerated.
         * generated/pack_c16.c: Regenerated.
         * generated/pack_c4.c: Regenerated.
         * generated/pack_c8.c: Regenerated.
         * generated/pack_i16.c: Regenerated.
         * generated/pack_i1.c: Regenerated.
         * generated/pack_i2.c: Regenerated.
         * generated/pack_i4.c: Regenerated.
         * generated/pack_i8.c: Regenerated.
         * generated/pack_r10.c: Regenerated.
         * generated/pack_r16.c: Regenerated.
         * generated/pack_r4.c: Regenerated.
         * generated/pack_r8.c: Regenerated.
         * generated/spread_c10.c: Regenerated.
         * generated/spread_c16.c: Regenerated.
         * generated/spread_c4.c: Regenerated.
         * generated/spread_c8.c: Regenerated.
         * generated/spread_i16.c: Regenerated.
         * generated/spread_i1.c: Regenerated.
         * generated/spread_i2.c: Regenerated.
         * generated/spread_i4.c: Regenerated.
         * generated/spread_i8.c: Regenerated.
         * generated/spread_r10.c: Regenerated.
         * generated/spread_r16.c: Regenerated.
         * generated/spread_r4.c: Regenerated.
         * generated/spread_r8.c: Regenerated.

Patch

Index: runtime/memory.c
===================================================================
--- runtime/memory.c	(Revision 175598)
+++ runtime/memory.c	(Arbeitskopie)
@@ -54,8 +54,8 @@  get_mem (size_t n)
 void *
 internal_malloc_size (size_t size)
 {
-  if (size == 0)
-    return NULL;
+  if (unlikely (size <= 0))
+    size = 1;
 
   return get_mem (size);
 }
Index: m4/pack.m4
===================================================================
--- m4/pack.m4	(Revision 175598)
+++ m4/pack.m4	(Arbeitskopie)
@@ -167,14 +167,12 @@  pack_'rtype_code` ('rtype` *ret, const 'rtype` *ar
 	  GFC_DIMENSION_SET(ret->dim[0], 0, total-1, 1);
 
 	  ret->offset = 0;
+
+	  /* internal_malloc_size allocates a single byte for zero size.  */
+	  ret->data = internal_malloc_size (sizeof ('rtype_name`) * total);
+
 	  if (total == 0)
-	    {
-	      /* In this case, nothing remains to be done.  */
-	      ret->data = internal_malloc_size (1);
-	      return;
-	    }
-	  else
-	    ret->data = internal_malloc_size (sizeof ('rtype_name`) * total);
+	    return;
 	}
       else 
 	{
Index: m4/spread.m4
===================================================================
--- m4/spread.m4	(Revision 175598)
+++ m4/spread.m4	(Arbeitskopie)
@@ -101,13 +101,11 @@  spread_'rtype_code` ('rtype` *ret, const 'rtype` *
 	  GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
 	}
       ret->offset = 0;
-      if (rs > 0)
-        ret->data = internal_malloc_size (rs * sizeof('rtype_name`));
-      else
-	{
-	  ret->data = internal_malloc_size (1);
-	  return;
-	}
+
+      /* internal_malloc_size allocates a single byte for zero size.  */
+      ret->data = internal_malloc_size (rs * sizeof('rtype_name`));
+      if (rs <= 0)
+        return;
     }
   else
     {
Index: m4/eoshift1.m4
===================================================================
--- m4/eoshift1.m4	(Revision 175598)
+++ m4/eoshift1.m4	(Arbeitskopie)
@@ -89,7 +89,6 @@  eoshift1 (gfc_array_char * const restrict ret,
     {
       int i;
 
-      ret->data = internal_malloc_size (size * arraysize);
       ret->offset = 0;
       ret->dtype = array->dtype;
       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
@@ -107,10 +106,8 @@  eoshift1 (gfc_array_char * const restrict ret,
 	  GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
 
         }
-      if (arraysize > 0)
-	ret->data = internal_malloc_size (size * arraysize);
-      else
-	ret->data = internal_malloc_size (1);
+      /* internal_malloc_size allocates a single byte for zero size.  */
+      ret->data = internal_malloc_size (size * arraysize);
 
     }
   else if (unlikely (compile_options.bounds_check))
Index: m4/eoshift3.m4
===================================================================
--- m4/eoshift3.m4	(Revision 175598)
+++ m4/eoshift3.m4	(Arbeitskopie)
@@ -108,10 +108,8 @@  eoshift3 (gfc_array_char * const restrict ret,
 	  GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
 
         }
-      if (arraysize > 0)
-	ret->data = internal_malloc_size (size * arraysize);
-      else
-	ret->data = internal_malloc_size (1);
+      /* internal_malloc_size allocates a single byte for zero size.  */
+      ret->data = internal_malloc_size (size * arraysize);
 
     }
   else if (unlikely (compile_options.bounds_check))
Index: intrinsics/pack_generic.c
===================================================================
--- intrinsics/pack_generic.c	(Revision 175598)
+++ intrinsics/pack_generic.c	(Arbeitskopie)
@@ -153,14 +153,11 @@  pack_internal (gfc_array_char *ret, const gfc_arra
 	  GFC_DIMENSION_SET(ret->dim[0], 0, total-1, 1);
 
 	  ret->offset = 0;
+	  /* internal_malloc_size allocates a single byte for zero size.  */
+	  ret->data = internal_malloc_size (size * total);
+
 	  if (total == 0)
-	    {
-	      /* In this case, nothing remains to be done.  */
-	      ret->data = internal_malloc_size (1);
-	      return;
-	    }
-	  else
-	    ret->data = internal_malloc_size (size * total);
+	    return;      /* In this case, nothing remains to be done.  */
 	}
       else 
 	{
@@ -523,13 +520,10 @@  pack_s_internal (gfc_array_char *ret, const gfc_ar
 
       ret->offset = 0;
 
+      ret->data = internal_malloc_size (size * total);
+
       if (total == 0)
-	{
-	  ret->data = internal_malloc_size (1);
-	  return;
-	}
-      else
-	ret->data = internal_malloc_size (size * total);
+	return;
     }
 
   rstride0 = GFC_DESCRIPTOR_STRIDE_BYTES(ret,0);
Index: intrinsics/cshift0.c
===================================================================
--- intrinsics/cshift0.c	(Revision 175598)
+++ intrinsics/cshift0.c	(Arbeitskopie)
@@ -79,10 +79,8 @@  cshift0 (gfc_array_char * ret, const gfc_array_cha
 	  GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
         }
 
-      if (arraysize > 0)
-	ret->data = internal_malloc_size (size * arraysize);
-      else
-	ret->data = internal_malloc_size (1);
+      /* internal_malloc_size allocates a single byte for zero size.  */
+      ret->data = internal_malloc_size (size * arraysize);
     }
   else if (unlikely (compile_options.bounds_check))
     {
Index: intrinsics/spread_generic.c
===================================================================
--- intrinsics/spread_generic.c	(Revision 175598)
+++ intrinsics/spread_generic.c	(Arbeitskopie)
@@ -100,13 +100,10 @@  spread_internal (gfc_array_char *ret, const gfc_ar
 	  GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
 	}
       ret->offset = 0;
-      if (rs > 0)
-        ret->data = internal_malloc_size (rs * size);
-      else
-	{
-	  ret->data = internal_malloc_size (1);
-	  return;
-	}
+      ret->data = internal_malloc_size (rs * size);
+
+      if (rs <= 0)
+	return;
     }
   else
     {
Index: intrinsics/eoshift0.c
===================================================================
--- intrinsics/eoshift0.c	(Revision 175598)
+++ intrinsics/eoshift0.c	(Arbeitskopie)
@@ -86,11 +86,8 @@  eoshift0 (gfc_array_char * ret, const gfc_array_ch
 
         }
 
-      if (arraysize > 0)
-	ret->data = internal_malloc_size (size * arraysize);
-      else
-	ret->data = internal_malloc_size (1);
-
+      /* internal_malloc_size allocates a single byte for zero size.  */
+      ret->data = internal_malloc_size (size * arraysize);
     }
   else if (unlikely (compile_options.bounds_check))
     {
Index: intrinsics/eoshift2.c
===================================================================
--- intrinsics/eoshift2.c	(Revision 175598)
+++ intrinsics/eoshift2.c	(Arbeitskopie)
@@ -91,10 +91,8 @@  eoshift2 (gfc_array_char *ret, const gfc_array_cha
 
 	  GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
 
-	  if (arraysize > 0)
-	    ret->data = internal_malloc_size (size * arraysize);
-	  else
-	    ret->data = internal_malloc_size (1);
+          /* internal_malloc_size allocates a single byte for zero size.  */
+	  ret->data = internal_malloc_size (size * arraysize);
 
         }
     }