-
Notifications
You must be signed in to change notification settings - Fork 1
/
FACT_alloc.c
141 lines (112 loc) · 3.1 KB
/
FACT_alloc.c
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
/* This file is part of FACT.
*
* FACT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FACT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FACT. If not, see <http://www.gnu.org/licenses/>.
*/
#include "FACT.h"
#include "FACT_mpc.h"
#include "FACT_num.h"
#include "FACT_scope.h"
#include "FACT_types.h"
#include "FACT_hash.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GC_THREADS
#include <gc/gc.h>
#include <assert.h>
#ifndef VALGRIND_DEBUG
inline void *FACT_malloc (size_t alloc_size)
{
void *temp;
temp = GC_malloc (alloc_size);
/* Check for NULL pointer. */
if (temp == NULL) {
fprintf (stderr, "Failed to allocate block of size %zu, aborting.\n", alloc_size);
abort ();
}
return temp;
}
inline void *FACT_malloc_atomic (size_t alloc_size)
{
void *temp;
temp = GC_malloc_atomic (alloc_size);
/* Check for NULL pointer. */
if (temp == NULL) {
fprintf (stderr, "Failed to allocate block of size %zu, aborting.\n", alloc_size);
abort ();
}
memset (temp, 0, alloc_size);
return temp;
}
inline void *FACT_realloc (void *old, size_t new_size)
{
void *temp;
temp = GC_realloc (old, new_size);
/* Check for NULL pointer. */
if (temp == NULL) {
fprintf (stderr, "Failed to reallocate block of size %zu, aborting.\n", new_size);
abort ();
}
return temp;
}
inline void FACT_free (void *p)
{
GC_free (p);
}
#else
# include "FACT_alloc.h"
#endif
FACT_num_t FACT_alloc_num (void) /* Allocate and initialize a num type. */
{
FACT_num_t temp;
temp = FACT_malloc (sizeof (struct FACT_num));
mpc_init (temp->value);
return temp;
}
FACT_num_t *FACT_alloc_num_array (size_t n)
{
FACT_num_t *temp;
temp = FACT_malloc (sizeof (FACT_num_t) * n); /* Allocate the nodes. */
/* Initialize all the nodes. */
do {
n--;
temp[n] = FACT_alloc_num ();
} while (n > 0);
return temp;
}
FACT_scope_t FACT_alloc_scope (void) /* Allocate and initialize a scope type. */
{
FACT_scope_t temp;
/* Allocate the memory. */
temp = FACT_malloc (sizeof (struct FACT_scope));
temp->marked = FACT_malloc_atomic (sizeof (bool));
temp->array_size = FACT_malloc_atomic (sizeof (size_t));
temp->code = FACT_malloc_atomic (sizeof (size_t));
temp->vars = FACT_malloc (sizeof (FACT_table_t ));
temp->array_up = FACT_malloc (sizeof (FACT_scope_t **));
temp->name = "lambda";
temp->lock_stat = UNLOCKED;
return temp;
}
FACT_scope_t *FACT_alloc_scope_array (size_t n)
{
FACT_scope_t *temp;
temp = FACT_malloc (sizeof (FACT_scope_t) * n); /* Allocate the nodes. */
/* Initialize all the nodes. */
do {
n--;
temp[n] = FACT_alloc_scope ();
} while (n > 0);
return temp;
}