-
Notifications
You must be signed in to change notification settings - Fork 2
/
Memory_routines.cpp
201 lines (167 loc) · 7.28 KB
/
Memory_routines.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************/
/*** FILE : Memory_routines.c ***/
/*** AUTHOR: Sekhar Muddana ***/
/*** PUBLIC ROUTINES: ***/
/*** struct unexp_tnode *Unexp_tnode_alloc() ***/
/*** void Free_tnode() ***/
/*** PROD_TREEPTR prod_talloc() ***/
/*** struct polynomial *Poly_alloc() ***/
/*** struct term_node *Term_node_alloc() ***/
/*** struct term_head *Term_head_alloc() ***/
/*** char *Str_alloc() ***/
/*** struct id_queue_node *Id_queue_node_alloc() ***/
/*** void No_memory_panic() ***/
/*** void Short_string_panic() ***/
/*** PRIVATE ROUTINES: ***/
/*** MODULE DESCRIPTION: ***/
/*** This module contains routines to deal with allocation ***/
/*** and freeing memory space. ***/
/*** When Memory can not be allocated, No_memory_panic() is ***/
/*** called, which issues No memory error and exits to $ ***/
/*** prompt. ***/
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include "Memory_routines.h"
#include "Po_parse_poly.h"
#include "Po_prod_bst.h"
#include "Po_parse_exptext.h"
#include "Id_routines.h"
#define DB_MEM_ALLOC 0
static struct unexp_tnode *Free_tnode_queue = NULL;
static int Num_free_tnodes = 0;
extern jmp_buf env;
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: */
/* Free_tnode_queue_ptr -- queue of free tnodes. */
/* RETURNS: */
/* Pointer to a free tnode. */
/* FUNCTION: */
/* If there are some free tnodes on the queue, return pointer */
/* to first tnode on the queue and delete this tnode from the */
/* queue, else allocate space by calling Mymalloc() and return */
/* a pointer to the allocated tnode. */
/*******************************************************************/
struct unexp_tnode *Unexp_tnode_alloc()
{
struct unexp_tnode *new_tnode;
#if DB_MEM_ALLOC
static int i = 1;
printf("in unexp alloc %d\n",i++);
#endif
if (Free_tnode_queue != NULL) {
new_tnode = Free_tnode_queue;
Free_tnode_queue = Free_tnode_queue->next;
Num_free_tnodes--;
}
else {
new_tnode = ((struct unexp_tnode *) Mymalloc(sizeof(struct unexp_tnode)));
}
new_tnode->op = -1;
new_tnode->s_letter = ' ';
new_tnode->scalar_num = 0;
new_tnode->operand1 = NULL;
new_tnode->operand2 = NULL;
new_tnode->operand3 = NULL;
new_tnode->next = NULL;
return(new_tnode);
}
void Free_tnode(struct unexp_tnode *Pntr)
{
#if DB_MEM_ALLOC
static int i = 1;
printf("in free tnode %d\n",i++);
#endif
if (Num_free_tnodes < 2000) {
Pntr->next = Free_tnode_queue;
Free_tnode_queue = Pntr;
Num_free_tnodes++;
}
else
free(Pntr);
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: None. */
/* RETURNS: */
/* Pointer to a new PROD_TNODE. */
/*******************************************************************/
PROD_TREEPTR prod_talloc()
{
PROD_TREEPTR temp;
temp = ((PROD_TREEPTR) Mymalloc(sizeof(PROD_TREENODE))) ;
return (temp);
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: None. */
/* RETURNS: */
/* Pointer to a new polynomial. */
/*******************************************************************/
struct polynomial *Poly_alloc()
{
struct polynomial *poly;
poly = ((struct polynomial *) Mymalloc(sizeof(struct polynomial)));
return(poly);
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: None. */
/* RETURNS: */
/* Pointer to a new Term_node. */
/*******************************************************************/
struct term_node *Term_node_alloc()
{
struct term_node *temp;
temp = ((struct term_node *) Mymalloc(sizeof(struct term_node)));
temp->left = temp->right = NULL;
temp->letter = ' ';
temp->number = 0;
return (temp);
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: None. */
/* RETURNS: */
/* Pointer to a new Term_head. */
/*******************************************************************/
struct term_head *Term_head_alloc()
{
struct term_head *temp;
temp = ((struct term_head *) Mymalloc(sizeof(struct term_head)));
temp->next = NULL;
return(temp);
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: */
/* size -- amount of space needs to be allocated. */
/* RETURNS: */
/* Pointer to allocated block of size size. */
/*******************************************************************/
void *Mymalloc(int size)
{
void *temp;
if (size <= 0) {
fprintf(stderr,"\n Severe. Trying to allocate 0 bytes. \n");
return(NULL);
}
temp = malloc(size);
if (temp == NULL)
No_memory_panic();
return(temp);
}
/*******************************************************************/
/* MODIFIES: None. */
/* REQUIRES: None. */
/* RETURNS: None. */
/* FUNCTION: */
/* Issue a warning and exit to $ prompt. */
/*******************************************************************/
void No_memory_panic()
{
fprintf(stderr,"\nMemory overflow.\n");
longjmp(env,2);
}