gEDA-dev: MyFree patch

ldoolitt at recycle.lbl.gov ldoolitt at recycle.lbl.gov
Mon Jun 26 16:53:08 EDT 2006


Patch relative to CVS as of 2006-06-25

Please either
  1)  apply as is
  2)  apply as is and convert all MYFREE to MyFree
  3)  request resubmission with all MYFREE changed to MyFree
  4)  explain why it is a good thing to break C strict-aliasing rules in 28 places

I was brought up on K&R, where SHOUTING that something is a
macro is considered a good thing, hence my choice of MYFREE
instead of MyFree.  I won't be offended if the owners of
the code make a different choice.

This patch does not address larger issues of the direction
and usability of the memory debugging infrastructure of PCB.
That's due to a combination of my laziness and my general
unease writing something that I don't urgently need and don't
feel I can effectively test.

Fixes to a couple of spelling errors, and a "rewrite" of
DSRealloc() crept in here.  Sorry, let me know if you want
them split out for the benefit of CVS comments.

    - Larry


diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/action.c /home/ldoolitt/src/pcb-20060625/src/action.c
--- pcb/src/action.c	2006-06-10 19:27:58.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/action.c	2006-06-19 11:37:51.000000000 -0700
@@ -4865,7 +4865,7 @@
 
   if (strcasecmp (function, "LayoutAs") == 0)
     {
-      MyFree (&(PCB->Filename));
+      MYFREE (PCB->Filename);
       PCB->Filename = MyStrdup (name, __FUNCTION__);
       function = "Layout";
     }
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/create.c /home/ldoolitt/src/pcb-20060625/src/create.c
--- pcb/src/create.c	2006-06-09 20:07:38.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/create.c	2006-06-19 11:37:45.000000000 -0700
@@ -837,7 +837,7 @@
 		  LocationType X, LocationType Y,
 		  BYTE Direction, char *TextString, int Scale, FlagType Flags)
 {
-  MyFree (&Text->TextString);
+  MYFREE (Text->TextString);
   Text->X = X;
   Text->Y = Y;
   Text->Direction = Direction;
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/dev_rs274x.c /home/ldoolitt/src/pcb-20060625/src/dev_rs274x.c
--- pcb/src/dev_rs274x.c	2006-03-22 15:17:20.000000000 -0800
+++ /home/ldoolitt/src/pcb-20060625/src/dev_rs274x.c	2006-06-26 09:15:54.000000000 -0700
@@ -455,7 +455,7 @@
      Sure, none of the other Dynamic strings ever do,
      even so .....
    */
-  MyFree ((char **) &appList.Data);
+  MYFREE (appList.Data);
   appList.MaxLength = 0;
 }
 
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/drill.c /home/ldoolitt/src/pcb-20060625/src/drill.c
--- pcb/src/drill.c	2006-03-25 07:13:16.000000000 -0800
+++ /home/ldoolitt/src/pcb-20060625/src/drill.c	2006-06-19 11:40:03.000000000 -0700
@@ -246,7 +246,7 @@
 		d->Drill[i].Element[d->Drill[i].ElementN++]
 		  = d->Drill[i+1].Element[ei];
 	    }
-	  MyFree ((char **)&d->Drill[i+1].Element);
+	  MYFREE (d->Drill[i+1].Element);
 
 	  d->Drill[i].PinMax
 	    = d->Drill[i].PinN + d->Drill[i+1].PinN;
@@ -257,7 +257,7 @@
 		  d->Drill[i+1].Pin,
 		  d->Drill[i+1].PinN * sizeof(PinTypePtr));
 	  d->Drill[i].PinN += d->Drill[i+1].PinN;
-	  MyFree ((char **)&d->Drill[i+1].Pin);
+	  MYFREE (d->Drill[i+1].Pin);
 
 	  d->Drill[i].PinCount += d->Drill[i+1].PinCount;
 	  d->Drill[i].ViaCount += d->Drill[i+1].ViaCount;
@@ -283,10 +283,10 @@
 {
   DRILL_LOOP (Drills);
   {
-    MyFree ((char **) &drill->Element);
-    MyFree ((char **) &drill->Pin);
+    MYFREE (drill->Element);
+    MYFREE (drill->Pin);
   }
   END_LOOP;
-  MyFree ((char **) &Drills->Drill);
+  MYFREE (Drills->Drill);
   SaveFree (Drills);
 }
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/file.c /home/ldoolitt/src/pcb-20060625/src/file.c
--- pcb/src/file.c	2006-06-09 20:07:41.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/file.c	2006-06-19 11:40:45.000000000 -0700
@@ -1009,7 +1009,7 @@
   LibraryMenuTypePtr menu = NULL;
   LibraryEntryTypePtr entry;
 
-  MyFree (&command);
+  MYFREE (command);
   command = EvaluateFilename (Settings.LibraryContentsCommand,
 			      Settings.LibraryPath, Settings.LibraryFilename,
 			      NULL);
@@ -1110,7 +1110,7 @@
 
   if (!filename)
     return (1);			/* nothing to do */
-  MyFree (&command);
+  MYFREE (command);
   command = EvaluateFilename (Settings.RatCommand,
 			      Settings.RatPath, filename, NULL);
 
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/find.c /home/ldoolitt/src/pcb-20060625/src/find.c
--- pcb/src/find.c	2006-06-09 20:07:41.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/find.c	2006-06-19 11:36:30.000000000 -0700
@@ -489,19 +489,19 @@
 
   for (i = 0; i < max_layer; i++)
     {
-      MyFree ((char **) &LineList[i].Data);
-      MyFree ((char **) &ArcList[i].Data);
-      MyFree ((char **) &PolygonList[i].Data);
+      MYFREE (LineList[i].Data);
+      MYFREE (ArcList[i].Data);
+      MYFREE (PolygonList[i].Data);
     }
-  MyFree ((char **) &PVList.Data);
-  MyFree ((char **) &RatList.Data);
+  MYFREE (PVList.Data);
+  MYFREE (RatList.Data);
 }
 
 void
 FreeComponentLookupMemory (void)
 {
-  MyFree ((char **) &PadList[0].Data);
-  MyFree ((char **) &PadList[1].Data);
+  MYFREE (PadList[0].Data);
+  MYFREE (PadList[1].Data);
 }
 
 /* ---------------------------------------------------------------------------
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/mymem.c /home/ldoolitt/src/pcb-20060625/src/mymem.c
--- pcb/src/mymem.c	2006-06-22 15:53:31.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/mymem.c	2006-06-22 15:52:38.000000000 -0700
@@ -100,7 +100,7 @@
 void
 FreePointerListMemory (PointerListTypePtr list)
 {
-  MyFree ((char **) &list->Ptr);
+  MYFREE (list->Ptr);
   memset (list, 0, sizeof (PointerListType));
 }
 
@@ -503,7 +503,7 @@
 }
 
 /* ---------------------------------------------------------------------------
- * get next slot for an library menu, allocates memory if necessary
+ * get next slot for a library menu, allocates memory if necessary
  */
 LibraryMenuTypePtr
 GetLibraryMenuMemory (LibraryTypePtr lib)
@@ -661,7 +661,7 @@
 /* ---------------------------------------------------------------------------
  * allocates memory with error handling
  * this is a save version because BSD doesn't support the
- * handling of NULL pointers in realoc()
+ * handling of NULL pointers in realloc()
  */
 void *
 MyRealloc (void *Ptr, size_t Size, const char *Text)
@@ -701,13 +701,18 @@
 
 /* ---------------------------------------------------------------------------
  * frees memory and sets pointer to NULL
+ * too troublesome for modern C compiler,
+ * warning: dereferencing type-punned pointer will break strict-aliasing rules
+ * Use MYFREE() macro instead
  */
+#if 0
 void
 MyFree (char **Ptr)
 {
   SaveFree (*Ptr);
   *Ptr = NULL;
 }
+#endif
 
 /* ---------------------------------------------------------------------------
  * frees memory used by a polygon
@@ -717,7 +722,7 @@
 {
   if (Polygon)
     {
-      MyFree ((char **) &Polygon->Points);
+      MYFREE (Polygon->Points);
       memset (Polygon, 0, sizeof (PolygonType));
     }
 }
@@ -730,7 +735,7 @@
 {
   if (Boxlist)
     {
-      MyFree ((char **) &Boxlist->Box);
+      MYFREE (Boxlist->Box);
       memset (Boxlist, 0, sizeof (BoxListType));
     }
 }
@@ -748,7 +753,7 @@
 	FreeNetMemory (net);
       }
       END_LOOP;
-      MyFree ((char **) &Netlist->Net);
+      MYFREE (Netlist->Net);
       memset (Netlist, 0, sizeof (NetListType));
     }
 }
@@ -766,7 +771,7 @@
 	FreeNetListMemory (netlist);
       }
       END_LOOP;
-      MyFree ((char **) &Netlistlist->NetList);
+      MYFREE (Netlistlist->NetList);
       memset (Netlistlist, 0, sizeof (NetListListType));
     }
 }
@@ -779,7 +784,7 @@
 {
   if (Net)
     {
-      MyFree ((char **) &Net->Connection);
+      MYFREE (Net->Connection);
       memset (Net, 0, sizeof (NetType));
     }
 }
@@ -794,25 +799,25 @@
     {
       ELEMENTNAME_LOOP (Element);
       {
-	MyFree (&textstring);
+	MYFREE (textstring);
       }
       END_LOOP;
       PIN_LOOP (Element);
       {
-	MyFree (&pin->Name);
-	MyFree (&pin->Number);
+	MYFREE (pin->Name);
+	MYFREE (pin->Number);
       }
       END_LOOP;
       PAD_LOOP (Element);
       {
-	MyFree (&pad->Name);
-	MyFree (&pad->Number);
+	MYFREE (pad->Name);
+	MYFREE (pad->Number);
       }
       END_LOOP;
-      MyFree ((char **) &Element->Pin);
-      MyFree ((char **) &Element->Pad);
-      MyFree ((char **) &Element->Line);
-      MyFree ((char **) &Element->Arc);
+      MYFREE (Element->Pin);
+      MYFREE (Element->Pad);
+      MYFREE (Element->Line);
+      MYFREE (Element->Arc);
       memset (Element, 0, sizeof (ElementType));
     }
 }
@@ -827,14 +832,14 @@
 
   if (PCBPtr)
     {
-      MyFree (&PCBPtr->Name);
-      MyFree (&PCBPtr->Filename);
-      MyFree (&PCBPtr->PrintFilename);
+      MYFREE (PCBPtr->Name);
+      MYFREE (PCBPtr->Filename);
+      MYFREE (PCBPtr->PrintFilename);
       FreeDataMemory (PCBPtr->Data);
-      MyFree ((char **) &PCBPtr->Data);
+      MYFREE (PCBPtr->Data);
       /* release font symbols */
       for (i = 0; i <= MAX_FONTPOSITION; i++)
-	MyFree ((char **) &PCBPtr->Font.Symbol[i].Line);
+	MYFREE (PCBPtr->Font.Symbol[i].Line);
       FreeLibraryMemory (&PCBPtr->NetlistLib);
       /* clear struct */
       memset (PCBPtr, 0, sizeof (PCBType));
@@ -854,7 +859,7 @@
     {
       VIA_LOOP (Data);
       {
-	MyFree (&via->Name);
+	MYFREE (via->Name);
       }
       END_LOOP;
       ELEMENT_LOOP (Data);
@@ -867,26 +872,26 @@
 	{
 	  TEXT_LOOP (layer);
 	  {
-	    MyFree (&text->TextString);
+	    MYFREE (text->TextString);
 	  }
 	  END_LOOP;
 	  if (layer->Name)
-	    MyFree (&layer->Name);
+	    MYFREE (layer->Name);
 	  LINE_LOOP (layer);
 	  {
 	    if (line->Number)
-	      MyFree (&line->Number);
+	      MYFREE (line->Number);
 	  }
 	  END_LOOP;
-	  MyFree ((char **) &layer->Line);
-	  MyFree ((char **) &layer->Arc);
-	  MyFree ((char **) &layer->Text);
+	  MYFREE (layer->Line);
+	  MYFREE (layer->Arc);
+	  MYFREE (layer->Text);
 	  POLYGON_LOOP (layer);
 	  {
 	    FreePolygonMemory (polygon);
 	  }
 	  END_LOOP;
-	  MyFree ((char **) &layer->Polygon);
+	  MYFREE (layer->Polygon);
 	  if (layer->line_tree)
 	    r_destroy_tree (&layer->line_tree);
 	  if (layer->arc_tree)
@@ -960,17 +965,12 @@
 static void
 DSRealloc (DynamicStringTypePtr Ptr, size_t Length)
 {
-  if (Ptr->Data == NULL)
-    {
-      Ptr->MaxLength = Length + 512;
-      Ptr->Data = MyRealloc (Ptr->Data, Ptr->MaxLength, "ReallocDS()");
-      Ptr->Data[0] = '\0';
-      return;
-    }
-  if (Length >= Ptr->MaxLength)
+  int input_null = (Ptr->Data == NULL);
+  if (input_null || Length >= Ptr->MaxLength)
     {
       Ptr->MaxLength = Length + 512;
       Ptr->Data = MyRealloc (Ptr->Data, Ptr->MaxLength, "ReallocDS()");
+      if (input_null) Ptr->Data[0] = '\0';
     }
 }
 
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/mymem.h /home/ldoolitt/src/pcb-20060625/src/mymem.h
--- pcb/src/mymem.h	2006-04-05 16:03:09.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/mymem.h	2006-06-19 11:44:25.000000000 -0700
@@ -97,7 +97,8 @@
 void *MyMalloc (size_t, const char *);
 void *MyRealloc (void *, size_t, const char *);
 char *MyStrdup (char *s, const char *);
-void MyFree (char **);
+/* void MyFree (void **); */
+#define MYFREE(x) do { SaveFree(x); (x)=NULL; } while (0)
 void FreePolygonMemory (PolygonTypePtr);
 void FreeElementMemory (ElementTypePtr);
 void FreePCBMemory (PCBTypePtr);
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/parse_l.l /home/ldoolitt/src/pcb-20060625/src/parse_l.l
--- pcb/src/parse_l.l	2006-04-27 20:09:14.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/parse_l.l	2006-06-26 09:16:35.000000000 -0700
@@ -199,7 +199,7 @@
 
 		/* release old command and create new from template */
 	if (command)
-	  MyFree(&command);
+	  MYFREE(command);
 	command = EvaluateFilename(Executable, Path, Filename, Parameter);
 
 		/* open pipe to stdout of command */
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/rats.c /home/ldoolitt/src/pcb-20060625/src/rats.c
--- pcb/src/rats.c	2006-06-09 20:07:41.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/rats.c	2006-06-26 09:17:09.000000000 -0700
@@ -228,7 +228,7 @@
     return (NULL);
   FreeNetListMemory (Wantlist);
   SaveFree (Wantlist);
-  /*  MyFree ((char **) &Wantlist); *//* awkward */
+  /*  MYFREE (Wantlist); *//* awkward */
   badnet = False;
 
   /* find layer groups of the component side and solder side */
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/remove.c /home/ldoolitt/src/pcb-20060625/src/remove.c
--- pcb/src/remove.c	2006-01-15 17:35:02.000000000 -0800
+++ /home/ldoolitt/src/pcb-20060625/src/remove.c	2006-06-19 11:33:47.000000000 -0700
@@ -128,7 +128,7 @@
 DestroyVia (PinTypePtr Via)
 {
   r_delete_entry (DestroyTarget->via_tree, (BoxTypePtr) Via);
-  MyFree (&Via->Name);
+  MYFREE (Via->Name);
   *Via = DestroyTarget->Via[--DestroyTarget->ViaN];
   r_substitute (DestroyTarget->via_tree, (BoxTypePtr)
 		(BoxType *) & DestroyTarget->Via[DestroyTarget->ViaN],
@@ -144,7 +144,7 @@
 DestroyLine (LayerTypePtr Layer, LineTypePtr Line)
 {
   r_delete_entry (Layer->line_tree, (BoxTypePtr) Line);
-  MyFree (&Line->Number);
+  MYFREE (Line->Number);
   *Line = Layer->Line[--Layer->LineN];
   /* tricky - line pointers are moved around */
   r_substitute (Layer->line_tree, (BoxType *) & Layer->Line[Layer->LineN],
@@ -211,7 +211,7 @@
 static void *
 DestroyText (LayerTypePtr Layer, TextTypePtr Text)
 {
-  MyFree (&Text->TextString);
+  MYFREE (Text->TextString);
   r_delete_entry (Layer->text_tree, (BoxTypePtr) Text);
   *Text = Layer->Text[--Layer->TextN];
   r_substitute (Layer->text_tree, (BoxType *) & Layer->Text[Layer->TextN],
diff -ur --exclude=CVS --exclude=Makefile.in --exclude=configure --exclude=aclocal.m4 pcb/src/undo.c /home/ldoolitt/src/pcb-20060625/src/undo.c
--- pcb/src/undo.c	2006-06-09 20:07:41.000000000 -0700
+++ /home/ldoolitt/src/pcb-20060625/src/undo.c	2006-06-19 11:32:46.000000000 -0700
@@ -1002,7 +1002,7 @@
 	  if (undo->Type == UNDO_CHANGENAME)
 	    SaveFree (undo->Data.ChangeName.Name);
 	}
-      MyFree ((void *) &UndoList);
+      MYFREE (UndoList);
       FreeDataMemory (RemoveList);
 
       /* reset some counters */


More information about the geda-dev mailing list