1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California.  All rights reserved.
4 Authors: UCB CAD Group
5          1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * Memory alloction functions
10  */
11 
12 #include "spice.h"
13 #include "misc.h"
14 
15 
16 char *
tmalloc(num)17 tmalloc(num)
18 
19 /* Malloc num bytes and initialize to zero. Fatal error if the space can't
20  * be malloc'd.   Return NULL for a request for 0 bytes.
21  */
22 int num;
23 {
24     char *s;
25 #ifdef MALLOCTRACE
26     int i;
27     static char *mem_alloc();
28 #endif
29 
30     if (!num)
31         return NULL;
32 
33 #ifdef MALLOCTRACE
34     s = mem_alloc((unsigned) num, 1, &i);
35 #else
36     s = malloc((unsigned) num);
37 #endif
38     if (!s) {
39         fprintf(stderr,
40             "malloc: Internal Error: can't allocate %d bytes.\n", num);
41         exit(EXIT_BAD);
42     }
43     bzero(s, num);
44     return (s);
45 }
46 
47 
48 char *
trealloc(str,num)49 trealloc(str, num)
50 
51 char *str;
52 int num;
53 {
54     char *s;
55 #ifdef MALLOCTRACE
56     int i;
57     static char *mem_alloc();
58 #endif
59 
60     if (!num) {
61         if (str)
62             free(str);
63         return (NULL);
64     }
65 
66 #ifdef MALLOCTRACE
67     s = mem_alloc((unsigned) num, 1, &i);
68     if (s) {
69         bcopy(str, s, num); /* Hope this won't cause a mem fault. */
70         free(str);
71     }
72 #else
73     if (!str)
74         s = tmalloc(num);
75     else
76         s = realloc(str, (unsigned) num);
77 #endif
78 
79     if (!s) {
80         fprintf(stderr,
81             "realloc: Internal Error: can't allocate %d bytes.\n", num);
82         exit(EXIT_BAD);
83     }
84     return (s);
85 }
86 
87 
88 void
txfree(ptr)89 txfree(ptr)
90 
91 char *ptr;
92 {
93     if (ptr)
94         free(ptr);
95 }
96