6 * PCB, interactive printed circuit board design
7 * Copyright (C) 1994,1995,1996 Thomas Nau
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * Contact addresses for paper mail and Email:
24 * Thomas Nau, Schlehenweg 15, 88471 Baustetten, Germany
25 * Thomas.Nau@rz.uni-ulm.de
30 /* memory management functions
48 #ifdef HAVE_LIBDMALLOC
54 /* ---------------------------------------------------------------------------
57 static void DSRealloc (DynamicStringTypePtr
, size_t);
59 /* ---------------------------------------------------------------------------
60 * get next slot for a rubberband connection, allocates memory if necessary
63 GetRubberbandMemory (void)
65 RubberbandTypePtr ptr
= Crosshair
.AttachedObject
.Rubberband
;
67 /* realloc new memory if necessary and clear it */
68 if (Crosshair
.AttachedObject
.RubberbandN
>=
69 Crosshair
.AttachedObject
.RubberbandMax
)
71 Crosshair
.AttachedObject
.RubberbandMax
+= STEP_RUBBERBAND
;
73 Crosshair
.AttachedObject
.RubberbandMax
*
74 sizeof (RubberbandType
), "GetRubberbandMemory()");
75 Crosshair
.AttachedObject
.Rubberband
= ptr
;
76 memset (ptr
+ Crosshair
.AttachedObject
.RubberbandN
, 0,
77 STEP_RUBBERBAND
* sizeof (RubberbandType
));
79 return (ptr
+ Crosshair
.AttachedObject
.RubberbandN
++);
83 GetPointerMemory (PointerListTypePtr list
)
85 void **ptr
= list
->Ptr
;
87 /* realloc new memory if necessary and clear it */
88 if (list
->PtrN
>= list
->PtrMax
)
90 list
->PtrMax
= STEP_POINT
+ (2 * list
->PtrMax
);
91 ptr
= MyRealloc (ptr
, list
->PtrMax
* sizeof (void *),
92 "GetPointerMemory()");
94 memset (ptr
+ list
->PtrN
, 0,
95 (list
->PtrMax
- list
->PtrN
) * sizeof (void *));
97 return (ptr
+ list
->PtrN
++);
101 FreePointerListMemory (PointerListTypePtr list
)
104 memset (list
, 0, sizeof (PointerListType
));
107 /* ---------------------------------------------------------------------------
108 * get next slot for a box, allocates memory if necessary
111 GetBoxMemory (BoxListTypePtr Boxes
)
113 BoxTypePtr box
= Boxes
->Box
;
115 /* realloc new memory if necessary and clear it */
116 if (Boxes
->BoxN
>= Boxes
->BoxMax
)
118 Boxes
->BoxMax
= STEP_POINT
+ (2 * Boxes
->BoxMax
);
119 box
= MyRealloc (box
, Boxes
->BoxMax
* sizeof (BoxType
),
122 memset (box
+ Boxes
->BoxN
, 0,
123 (Boxes
->BoxMax
- Boxes
->BoxN
) * sizeof (BoxType
));
125 return (box
+ Boxes
->BoxN
++);
129 /* ---------------------------------------------------------------------------
130 * get next slot for a connection, allocates memory if necessary
133 GetConnectionMemory (NetTypePtr Net
)
135 ConnectionTypePtr con
= Net
->Connection
;
137 /* realloc new memory if necessary and clear it */
138 if (Net
->ConnectionN
>= Net
->ConnectionMax
)
140 Net
->ConnectionMax
+= STEP_POINT
;
141 con
= MyRealloc (con
, Net
->ConnectionMax
* sizeof (ConnectionType
),
142 "GetConnectionMemory()");
143 Net
->Connection
= con
;
144 memset (con
+ Net
->ConnectionN
, 0,
145 STEP_POINT
* sizeof (ConnectionType
));
147 return (con
+ Net
->ConnectionN
++);
150 /* ---------------------------------------------------------------------------
151 * get next slot for a subnet, allocates memory if necessary
154 GetNetMemory (NetListTypePtr Netlist
)
156 NetTypePtr net
= Netlist
->Net
;
158 /* realloc new memory if necessary and clear it */
159 if (Netlist
->NetN
>= Netlist
->NetMax
)
161 Netlist
->NetMax
+= STEP_POINT
;
162 net
= MyRealloc (net
, Netlist
->NetMax
* sizeof (NetType
),
165 memset (net
+ Netlist
->NetN
, 0, STEP_POINT
* sizeof (NetType
));
167 return (net
+ Netlist
->NetN
++);
170 /* ---------------------------------------------------------------------------
171 * get next slot for a net list, allocates memory if necessary
174 GetNetListMemory (NetListListTypePtr Netlistlist
)
176 NetListTypePtr netlist
= Netlistlist
->NetList
;
178 /* realloc new memory if necessary and clear it */
179 if (Netlistlist
->NetListN
>= Netlistlist
->NetListMax
)
181 Netlistlist
->NetListMax
+= STEP_POINT
;
183 (netlist
, Netlistlist
->NetListMax
* sizeof (NetListType
),
184 "GetNetListMemory()");
185 Netlistlist
->NetList
= netlist
;
186 memset (netlist
+ Netlistlist
->NetListN
, 0,
187 STEP_POINT
* sizeof (NetListType
));
189 return (netlist
+ Netlistlist
->NetListN
++);
192 /* ---------------------------------------------------------------------------
193 * get next slot for a pin, allocates memory if necessary
196 GetPinMemory (ElementTypePtr Element
)
198 PinTypePtr pin
= Element
->Pin
;
199 Boolean onBoard
= False
;
201 /* realloc new memory if necessary and clear it */
202 if (Element
->PinN
>= Element
->PinMax
)
204 if (PCB
->Data
->pin_tree
)
208 if (r_delete_entry (PCB
->Data
->pin_tree
, (BoxType
*) pin
))
213 Element
->PinMax
+= STEP_PIN
;
214 pin
= MyRealloc (pin
, Element
->PinMax
* sizeof (PinType
),
217 memset (pin
+ Element
->PinN
, 0, STEP_PIN
* sizeof (PinType
));
222 r_insert_entry (PCB
->Data
->pin_tree
, (BoxType
*) pin
, 0);
227 return (pin
+ Element
->PinN
++);
230 /* ---------------------------------------------------------------------------
231 * get next slot for a pad, allocates memory if necessary
234 GetPadMemory (ElementTypePtr Element
)
236 PadTypePtr pad
= Element
->Pad
;
237 Boolean onBoard
= False
;
239 /* realloc new memory if necessary and clear it */
240 if (Element
->PadN
>= Element
->PadMax
)
242 if (PCB
->Data
->pad_tree
)
246 if (r_delete_entry (PCB
->Data
->pad_tree
, (BoxType
*) pad
))
251 Element
->PadMax
+= STEP_PAD
;
252 pad
= MyRealloc (pad
, Element
->PadMax
* sizeof (PadType
),
255 memset (pad
+ Element
->PadN
, 0, STEP_PAD
* sizeof (PadType
));
260 r_insert_entry (PCB
->Data
->pad_tree
, (BoxType
*) pad
, 0);
265 return (pad
+ Element
->PadN
++);
268 /* ---------------------------------------------------------------------------
269 * get next slot for a via, allocates memory if necessary
272 GetViaMemory (DataTypePtr Data
)
274 PinTypePtr via
= Data
->Via
;
276 /* realloc new memory if necessary and clear it */
277 if (Data
->ViaN
>= Data
->ViaMax
)
279 Data
->ViaMax
+= STEP_VIA
;
281 r_destroy_tree (&Data
->via_tree
);
282 via
= MyRealloc (via
, Data
->ViaMax
* sizeof (PinType
),
285 memset (via
+ Data
->ViaN
, 0, STEP_VIA
* sizeof (PinType
));
286 Data
->via_tree
= r_create_tree (NULL
, 0, 0);
289 r_insert_entry (Data
->via_tree
, (BoxType
*) via
, 0);
293 return (via
+ Data
->ViaN
++);
296 /* ---------------------------------------------------------------------------
297 * get next slot for a Rat, allocates memory if necessary
300 GetRatMemory (DataTypePtr Data
)
302 RatTypePtr rat
= Data
->Rat
;
304 /* realloc new memory if necessary and clear it */
305 if (Data
->RatN
>= Data
->RatMax
)
307 Data
->RatMax
+= STEP_RAT
;
308 /* all of the pointers move, so rebuild the whole tree */
310 r_destroy_tree (&Data
->rat_tree
);
311 rat
= MyRealloc (rat
, Data
->RatMax
* sizeof (RatType
),
314 memset (rat
+ Data
->RatN
, 0, STEP_RAT
* sizeof (RatType
));
315 Data
->rat_tree
= r_create_tree (NULL
, 0, 0);
318 r_insert_entry (Data
->rat_tree
, (BoxTypePtr
) line
, 0);
322 return (rat
+ Data
->RatN
++);
325 /* ---------------------------------------------------------------------------
326 * get next slot for a line, allocates memory if necessary
329 GetLineMemory (LayerTypePtr Layer
)
331 LineTypePtr line
= Layer
->Line
;
333 /* realloc new memory if necessary and clear it */
334 if (Layer
->LineN
>= Layer
->LineMax
)
336 Layer
->LineMax
+= STEP_LINE
;
337 /* all of the pointers move, so rebuild the whole tree */
338 if (Layer
->line_tree
)
339 r_destroy_tree (&Layer
->line_tree
);
340 line
= MyRealloc (line
, Layer
->LineMax
* sizeof (LineType
),
343 memset (line
+ Layer
->LineN
, 0, STEP_LINE
* sizeof (LineType
));
344 Layer
->line_tree
= r_create_tree (NULL
, 0, 0);
347 r_insert_entry (Layer
->line_tree
, (BoxTypePtr
) line
, 0);
351 return (line
+ Layer
->LineN
++);
354 /* ---------------------------------------------------------------------------
355 * get next slot for an arc, allocates memory if necessary
358 GetArcMemory (LayerTypePtr Layer
)
360 ArcTypePtr arc
= Layer
->Arc
;
362 /* realloc new memory if necessary and clear it */
363 if (Layer
->ArcN
>= Layer
->ArcMax
)
365 Layer
->ArcMax
+= STEP_ARC
;
367 r_destroy_tree (&Layer
->arc_tree
);
368 arc
= MyRealloc (arc
, Layer
->ArcMax
* sizeof (ArcType
),
371 memset (arc
+ Layer
->ArcN
, 0, STEP_ARC
* sizeof (ArcType
));
372 Layer
->arc_tree
= r_create_tree (NULL
, 0, 0);
375 r_insert_entry (Layer
->arc_tree
, (BoxTypePtr
) arc
, 0);
379 return (arc
+ Layer
->ArcN
++);
382 /* ---------------------------------------------------------------------------
383 * get next slot for a text object, allocates memory if necessary
386 GetTextMemory (LayerTypePtr Layer
)
388 TextTypePtr text
= Layer
->Text
;
390 /* realloc new memory if necessary and clear it */
391 if (Layer
->TextN
>= Layer
->TextMax
)
393 Layer
->TextMax
+= STEP_TEXT
;
394 if (Layer
->text_tree
)
395 r_destroy_tree (&Layer
->text_tree
);
396 text
= MyRealloc (text
, Layer
->TextMax
* sizeof (TextType
),
399 memset (text
+ Layer
->TextN
, 0, STEP_TEXT
* sizeof (TextType
));
400 Layer
->text_tree
= r_create_tree (NULL
, 0, 0);
403 r_insert_entry (Layer
->text_tree
, (BoxTypePtr
) text
, 0);
407 return (text
+ Layer
->TextN
++);
410 /* ---------------------------------------------------------------------------
411 * get next slot for a polygon object, allocates memory if necessary
414 GetPolygonMemory (LayerTypePtr Layer
)
416 PolygonTypePtr polygon
= Layer
->Polygon
;
418 /* realloc new memory if necessary and clear it */
419 if (Layer
->PolygonN
>= Layer
->PolygonMax
)
421 Layer
->PolygonMax
+= STEP_POLYGON
;
422 if (Layer
->polygon_tree
)
423 r_destroy_tree (&Layer
->polygon_tree
);
424 polygon
= MyRealloc (polygon
, Layer
->PolygonMax
* sizeof (PolygonType
),
425 "GetPolygonMemory()");
426 Layer
->Polygon
= polygon
;
427 memset (polygon
+ Layer
->PolygonN
, 0,
428 STEP_POLYGON
* sizeof (PolygonType
));
429 Layer
->polygon_tree
= r_create_tree (NULL
, 0, 0);
430 POLYGON_LOOP (Layer
);
432 r_insert_entry (Layer
->polygon_tree
, (BoxType
*) polygon
, 0);
436 return (polygon
+ Layer
->PolygonN
++);
439 /* ---------------------------------------------------------------------------
440 * gets the next slot for a point in a polygon struct, allocates memory
444 GetPointMemoryInPolygon (PolygonTypePtr Polygon
)
446 PointTypePtr points
= Polygon
->Points
;
448 /* realloc new memory if necessary and clear it */
449 if (Polygon
->PointN
>= Polygon
->PointMax
)
451 Polygon
->PointMax
+= STEP_POLYGONPOINT
;
452 points
= MyRealloc (points
, Polygon
->PointMax
* sizeof (PointType
),
453 "GetPointMemoryInPolygon()");
454 Polygon
->Points
= points
;
455 memset (points
+ Polygon
->PointN
, 0,
456 STEP_POLYGONPOINT
* sizeof (PointType
));
458 return (points
+ Polygon
->PointN
++);
461 /* ---------------------------------------------------------------------------
462 * get next slot for an element, allocates memory if necessary
465 GetElementMemory (DataTypePtr Data
)
467 ElementTypePtr element
= Data
->Element
;
470 /* realloc new memory if necessary and clear it */
471 if (Data
->ElementN
>= Data
->ElementMax
)
473 Data
->ElementMax
+= STEP_ELEMENT
;
474 if (Data
->element_tree
)
475 r_destroy_tree (&Data
->element_tree
);
476 element
= MyRealloc (element
, Data
->ElementMax
* sizeof (ElementType
),
477 "GetElementMemory()");
478 Data
->Element
= element
;
479 memset (element
+ Data
->ElementN
, 0,
480 STEP_ELEMENT
* sizeof (ElementType
));
481 Data
->element_tree
= r_create_tree (NULL
, 0, 0);
482 for (i
= 0; i
< MAX_ELEMENTNAMES
; i
++)
484 if (Data
->name_tree
[i
])
485 r_destroy_tree (&Data
->name_tree
[i
]);
486 Data
->name_tree
[i
] = r_create_tree (NULL
, 0, 0);
491 r_insert_entry (Data
->element_tree
, (BoxType
*) element
, 0);
494 pin
->Element
= element
;
499 pad
->Element
= element
;
502 ELEMENTTEXT_LOOP (element
);
504 text
->Element
= element
;
505 r_insert_entry (Data
->name_tree
[n
], (BoxType
*) text
, 0);
511 return (element
+ Data
->ElementN
++);
514 /* ---------------------------------------------------------------------------
515 * get next slot for a library menu, allocates memory if necessary
518 GetLibraryMenuMemory (LibraryTypePtr lib
)
520 LibraryMenuTypePtr menu
= lib
->Menu
;
522 /* realloc new memory if necessary and clear it */
523 if (lib
->MenuN
>= lib
->MenuMax
)
525 lib
->MenuMax
+= STEP_LIBRARYMENU
;
526 menu
= MyRealloc (menu
, lib
->MenuMax
* sizeof (LibraryMenuType
),
527 "GetLibraryMenuMemory()");
529 memset (menu
+ lib
->MenuN
, 0,
530 STEP_LIBRARYMENU
* sizeof (LibraryMenuType
));
532 return (menu
+ lib
->MenuN
++);
535 /* ---------------------------------------------------------------------------
536 * get next slot for a library entry, allocates memory if necessary
539 GetLibraryEntryMemory (LibraryMenuTypePtr Menu
)
541 LibraryEntryTypePtr entry
= Menu
->Entry
;
543 /* realloc new memory if necessary and clear it */
544 if (Menu
->EntryN
>= Menu
->EntryMax
)
546 Menu
->EntryMax
+= STEP_LIBRARYENTRY
;
547 entry
= MyRealloc (entry
, Menu
->EntryMax
* sizeof (LibraryEntryType
),
548 "GetLibraryEntryMemory()");
550 memset (entry
+ Menu
->EntryN
, 0,
551 STEP_LIBRARYENTRY
* sizeof (LibraryEntryType
));
553 return (entry
+ Menu
->EntryN
++);
556 /* ---------------------------------------------------------------------------
557 * get next slot for a DrillElement, allocates memory if necessary
560 GetDrillElementMemory (DrillTypePtr Drill
)
562 ElementTypePtr
*element
;
564 element
= Drill
->Element
;
566 /* realloc new memory if necessary and clear it */
567 if (Drill
->ElementN
>= Drill
->ElementMax
)
569 Drill
->ElementMax
+= STEP_ELEMENT
;
571 MyRealloc (element
, Drill
->ElementMax
* sizeof (ElementTypeHandle
),
572 "GetDrillElementMemory()");
573 Drill
->Element
= element
;
574 memset (element
+ Drill
->ElementN
, 0,
575 STEP_ELEMENT
* sizeof (ElementTypeHandle
));
577 return (element
+ Drill
->ElementN
++);
580 /* ---------------------------------------------------------------------------
581 * get next slot for a DrillPoint, allocates memory if necessary
584 GetDrillPinMemory (DrillTypePtr Drill
)
590 /* realloc new memory if necessary and clear it */
591 if (Drill
->PinN
>= Drill
->PinMax
)
593 Drill
->PinMax
+= STEP_POINT
;
594 pin
= MyRealloc (pin
, Drill
->PinMax
* sizeof (PinTypeHandle
),
595 "GetDrillPinMemory()");
597 memset (pin
+ Drill
->PinN
, 0, STEP_POINT
* sizeof (PinTypeHandle
));
599 return (pin
+ Drill
->PinN
++);
602 /* ---------------------------------------------------------------------------
603 * get next slot for a Drill, allocates memory if necessary
606 GetDrillInfoDrillMemory (DrillInfoTypePtr DrillInfo
)
608 DrillTypePtr drill
= DrillInfo
->Drill
;
610 /* realloc new memory if necessary and clear it */
611 if (DrillInfo
->DrillN
>= DrillInfo
->DrillMax
)
613 DrillInfo
->DrillMax
+= STEP_DRILL
;
614 drill
= MyRealloc (drill
, DrillInfo
->DrillMax
* sizeof (DrillType
),
615 "GetDrillInfoDrillMemory()");
616 DrillInfo
->Drill
= drill
;
617 memset (drill
+ DrillInfo
->DrillN
, 0, STEP_DRILL
* sizeof (DrillType
));
619 return (drill
+ DrillInfo
->DrillN
++);
622 /* ---------------------------------------------------------------------------
623 * allocates memory with error handling
626 MyCalloc (size_t Number
, size_t Size
, const char *Text
)
631 fprintf (stderr
, "MyCalloc %d by %d from %s ", Number
, Size
, Text
);
633 /* InitComponentLookup() at least can ask for zero here, so return something
641 if ((p
= calloc (Number
, Size
)) == NULL
)
642 MyFatal ("out of memory during malloc() in '%s'()\n",
643 (Text
? Text
: "(unknown)"));
645 fprintf (stderr
, "returned 0x%x\n", p
);
651 MyMalloc (size_t Size
, const char *Text
)
656 fprintf (stderr
, "MyMalloc %d by %d from %s ", Number
, Size
, Text
);
658 /* avoid malloc of 0 bytes */
661 if ((p
= malloc (Size
)) == NULL
)
662 MyFatal ("out of memory during malloc() in '%s'()\n",
663 (Text
? Text
: "(unknown)"));
665 fprintf (stderr
, "returned 0x%x\n", p
);
670 /* ---------------------------------------------------------------------------
671 * allocates memory with error handling
672 * this is a save version because BSD doesn't support the
673 * handling of NULL pointers in realloc()
676 MyRealloc (void *Ptr
, size_t Size
, const char *Text
)
681 fprintf (stderr
, "0x%x Realloc to %d from %s ", Ptr
, Size
, Text
);
685 p
= Ptr
? realloc (Ptr
, Size
) : malloc (Size
);
687 MyFatal ("out of memory during realloc() in '%s'()\n",
688 (Text
? Text
: "(unknown)"));
690 fprintf (stderr
, "returned 0x%x\n", p
);
695 /* ---------------------------------------------------------------------------
696 * allocates memory for a new string, does some error processing
699 MyStrdup (char *S
, const char *Text
)
703 /* bug-fix by Ulrich Pegelow (ulrpeg@bigcomm.gun.de) */
704 if (S
&& ((p
= strdup (S
)) == NULL
))
705 MyFatal ("out of memory during g_strdup() in '%s'\n",
706 (Text
? Text
: "(unknown)"));
708 fprintf (stderr
, "g_strdup returning 0x%x\n", p
);
713 /* ---------------------------------------------------------------------------
714 * frees memory and sets pointer to NULL
715 * too troublesome for modern C compiler,
716 * warning: dereferencing type-punned pointer will break strict-aliasing rules
717 * Use MYFREE() macro instead
728 /* ---------------------------------------------------------------------------
729 * frees memory used by a polygon
732 FreePolygonMemory (PolygonTypePtr Polygon
)
736 MYFREE (Polygon
->Points
);
737 if (Polygon
->Clipped
)
738 poly_Free (&Polygon
->Clipped
);
739 poly_FreeContours (&Polygon
->NoHoles
);
740 memset (Polygon
, 0, sizeof (PolygonType
));
744 /* ---------------------------------------------------------------------------
745 * frees memory used by a box list
748 FreeBoxListMemory (BoxListTypePtr Boxlist
)
752 MYFREE (Boxlist
->Box
);
753 memset (Boxlist
, 0, sizeof (BoxListType
));
757 /* ---------------------------------------------------------------------------
758 * frees memory used by a net
761 FreeNetListMemory (NetListTypePtr Netlist
)
770 MYFREE (Netlist
->Net
);
771 memset (Netlist
, 0, sizeof (NetListType
));
775 /* ---------------------------------------------------------------------------
776 * frees memory used by a net list
779 FreeNetListListMemory (NetListListTypePtr Netlistlist
)
783 NETLIST_LOOP (Netlistlist
);
785 FreeNetListMemory (netlist
);
788 MYFREE (Netlistlist
->NetList
);
789 memset (Netlistlist
, 0, sizeof (NetListListType
));
793 /* ---------------------------------------------------------------------------
794 * frees memory used by a subnet
797 FreeNetMemory (NetTypePtr Net
)
801 MYFREE (Net
->Connection
);
802 memset (Net
, 0, sizeof (NetType
));
805 /* ---------------------------------------------------------------------------
806 * frees memory used by an attribute list
809 FreeAttributeListMemory (AttributeListTypePtr list
)
813 for (i
= 0; i
< list
->Number
; i
++)
815 SaveFree (list
->List
[i
].name
);
816 SaveFree (list
->List
[i
].value
);
818 SaveFree (list
->List
);
823 /* ---------------------------------------------------------------------------
824 * frees memory used by an element
827 FreeElementMemory (ElementTypePtr Element
)
831 ELEMENTNAME_LOOP (Element
);
839 MYFREE (pin
->Number
);
845 MYFREE (pad
->Number
);
848 MYFREE (Element
->Pin
);
849 MYFREE (Element
->Pad
);
850 MYFREE (Element
->Line
);
851 MYFREE (Element
->Arc
);
852 FreeAttributeListMemory (&Element
->Attributes
);
853 memset (Element
, 0, sizeof (ElementType
));
857 /* ---------------------------------------------------------------------------
858 * free memory used by PCB
861 FreePCBMemory (PCBTypePtr PCBPtr
)
867 MYFREE (PCBPtr
->Name
);
868 MYFREE (PCBPtr
->Filename
);
869 MYFREE (PCBPtr
->PrintFilename
);
871 FreeDataMemory (PCBPtr
->Data
);
872 MYFREE (PCBPtr
->Data
);
873 /* release font symbols */
874 for (i
= 0; i
<= MAX_FONTPOSITION
; i
++)
875 MYFREE (PCBPtr
->Font
.Symbol
[i
].Line
);
876 FreeLibraryMemory (&PCBPtr
->NetlistLib
);
877 FreeAttributeListMemory (&PCBPtr
->Attributes
);
879 memset (PCBPtr
, 0, sizeof (PCBType
));
883 fprintf (stderr
, "Warning: Tried to FreePCBMemory(null)\n");
887 /* ---------------------------------------------------------------------------
888 * free memory used by data struct
891 FreeDataMemory (DataTypePtr Data
)
905 FreeElementMemory (element
);
909 for (layer
= Data
->Layer
, i
= 0; i
< MAX_LAYER
+ 2; layer
++, i
++)
911 FreeAttributeListMemory (&layer
->Attributes
);
914 MYFREE (text
->TextString
);
918 MYFREE (layer
->Name
);
922 MYFREE (line
->Number
);
925 MYFREE (layer
->Line
);
927 MYFREE (layer
->Text
);
928 POLYGON_LOOP (layer
);
930 FreePolygonMemory (polygon
);
933 MYFREE (layer
->Polygon
);
934 if (layer
->line_tree
)
935 r_destroy_tree (&layer
->line_tree
);
937 r_destroy_tree (&layer
->arc_tree
);
938 if (layer
->text_tree
)
939 r_destroy_tree (&layer
->text_tree
);
940 if (layer
->polygon_tree
)
941 r_destroy_tree (&layer
->polygon_tree
);
944 if (Data
->element_tree
)
945 r_destroy_tree (&Data
->element_tree
);
946 for (i
= 0; i
< MAX_ELEMENTNAMES
; i
++)
947 if (Data
->name_tree
[i
])
948 r_destroy_tree (&Data
->name_tree
[i
]);
950 r_destroy_tree (&Data
->via_tree
);
952 r_destroy_tree (&Data
->pin_tree
);
954 r_destroy_tree (&Data
->pad_tree
);
956 r_destroy_tree (&Data
->rat_tree
);
958 memset (Data
, 0, sizeof (DataType
));
962 fprintf (stderr
, "Warning: Tried to FreeDataMemory(null)\n");
966 /* ---------------------------------------------------------------------------
967 * releases the memory that's allocated by the library
970 FreeLibraryMemory (LibraryTypePtr lib
)
976 SaveFree ((void *) entry
->AllocatedMemory
);
977 SaveFree ((void *) entry
->ListEntry
);
980 SaveFree ((void *) menu
->Entry
);
981 SaveFree ((void *) menu
->Name
);
984 SaveFree ((void *) lib
->Menu
);
987 memset (lib
, 0, sizeof (LibraryType
));
990 /* ---------------------------------------------------------------------------
991 * a 'save' free routine which first does a quick check if the pointer
992 * is zero. The routine isn't implemented as a macro to make additional
993 * safety features easier to implement
999 fprintf (stderr
, "Freeing 0x%x\n", Ptr
);
1005 /* ---------------------------------------------------------------------------
1006 * reallocates memory for a dynamic length string if necessary
1009 DSRealloc (DynamicStringTypePtr Ptr
, size_t Length
)
1011 int input_null
= (Ptr
->Data
== NULL
);
1012 if (input_null
|| Length
>= Ptr
->MaxLength
)
1014 Ptr
->MaxLength
= Length
+ 512;
1015 Ptr
->Data
= MyRealloc (Ptr
->Data
, Ptr
->MaxLength
, "ReallocDS()");
1017 Ptr
->Data
[0] = '\0';
1021 /* ---------------------------------------------------------------------------
1022 * adds one character to a dynamic string
1025 DSAddCharacter (DynamicStringTypePtr Ptr
, char Char
)
1027 size_t position
= Ptr
->Data
? strlen (Ptr
->Data
) : 0;
1029 DSRealloc (Ptr
, position
+ 1);
1030 Ptr
->Data
[position
++] = Char
;
1031 Ptr
->Data
[position
] = '\0';
1034 /* ---------------------------------------------------------------------------
1035 * add a string to a dynamic string
1038 DSAddString (DynamicStringTypePtr Ptr
, const char *S
)
1040 size_t position
= Ptr
->Data
? strlen (Ptr
->Data
) : 0;
1044 DSRealloc (Ptr
, position
+ 1 + strlen (S
));
1045 strcat (&Ptr
->Data
[position
], S
);
1049 /* ----------------------------------------------------------------------
1050 * clears a dynamic string
1053 DSClearString (DynamicStringTypePtr Ptr
)
1056 Ptr
->Data
[0] = '\0';
1059 /* ---------------------------------------------------------------------------
1060 * strips leading and trailing blanks from the passed string and
1061 * returns a pointer to the new 'duped' one or NULL if the old one
1062 * holds only white space characters
1065 StripWhiteSpaceAndDup (char *S
)
1073 /* strip leading blanks */
1074 for (p1
= S
; *p1
&& isspace ((int) *p1
); p1
++);
1076 /* strip trailing blanks and get string length */
1077 length
= strlen (p1
);
1078 for (p2
= p1
+ length
- 1; length
&& isspace ((int) *p2
); p2
--, length
--);
1080 /* string is not empty -> allocate memory */
1083 p2
= MyRealloc (NULL
, length
+ 1, "StripWhiteSpace()");
1084 strncpy (p2
, p1
, length
);
1085 *(p2
+ length
) = '\0';