OpenCores
URL https://opencores.org/ocsvn/jtag_stapl_player/jtag_stapl_player/trunk

Subversion Repositories jtag_stapl_player

[/] [jtag_stapl_player/] [trunk/] [jamheap.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sukhanov
/****************************************************************************/
2
/*                                                                                                                                                      */
3
/*      Module:                 jamheap.c                                                                                               */
4
/*                                                                                                                                                      */
5
/*                                      Copyright (C) Altera Corporation 1997                                   */
6
/*                                                                                                                                                      */
7
/*      Description:    Heap management functions.  The heap is implemented as  */
8
/*                                      a linked list of blocks of variable size.                               */
9
/*                                                                                                                                                      */
10
/*      Revisions:              1.1 added support for dynamic memory allocation                 */
11
/*                                                                                                                                                      */
12
/****************************************************************************/
13
 
14
/****************************************************************************/
15
/*                                                                                                                                                      */
16
/*      Actel version 1.1             May 2003                                                                  */
17
/*                                                                                                                                                      */
18
/****************************************************************************/
19
 
20
#include "jamport.h"
21
#include "jamexprt.h"
22
#include "jamdefs.h"
23
#include "jamsym.h"
24
#include "jamstack.h"
25
#include "jamheap.h"
26
#include "jamjtag.h"
27
#include "jamutil.h"
28
 
29
/****************************************************************************/
30
/*                                                                                                                                                      */
31
/*      Global variables                                                                                                                */
32
/*                                                                                                                                                      */
33
/****************************************************************************/
34
 
35
JAMS_HEAP_RECORD *jam_heap = NULL;
36
 
37
void *jam_heap_top = NULL;
38
 
39
long jam_heap_records = 0L;
40
 
41
/****************************************************************************/
42
/*                                                                                                                                                      */
43
 
44
JAM_RETURN_TYPE jam_init_heap(void)
45
 
46
/*                                                                                                                                                      */
47
/*      Description:    Initializes the heap area.  This is where all array             */
48
/*                                      data is stored.                                                                                 */
49
/*                                                                                                                                                      */
50
/*      Returns:                JAMC_SUCCESS for success, or JAMC_OUT_OF_MEMORY if no   */
51
/*                                      memory was available for the heap.                                              */
52
/*                                                                                                                                                      */
53
/****************************************************************************/
54
{
55
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
56
        void **symbol_table = NULL;
57
        JAMS_STACK_RECORD *stack = NULL;
58
        long *jtag_buffer = NULL;
59
 
60
        jam_heap_records = 0L;
61
 
62
        if (jam_workspace != NULL)
63
        {
64
                symbol_table = (void **) jam_workspace;
65
                stack = (JAMS_STACK_RECORD *) &symbol_table[JAMC_MAX_SYMBOL_COUNT];
66
                jtag_buffer = (long *) &stack[JAMC_MAX_NESTING_DEPTH];
67
                jam_heap = (JAMS_HEAP_RECORD *)
68
                        (((char *) jtag_buffer) + JAMC_JTAG_BUFFER_SIZE);
69
                jam_heap_top = (void *) jam_heap;
70
 
71
                /*
72
                *       Check that there is some memory available for the heap
73
                */
74
                if (((long)jam_heap) > (((long)jam_workspace_size) +
75
                        ((long)jam_workspace)))
76
                {
77
                        status = JAMC_OUT_OF_MEMORY;
78
                }
79
        }
80
        else
81
        {
82
                /* initialize heap to empty list */
83
                jam_heap = NULL;
84
        }
85
 
86
        return (status);
87
}
88
 
89
void jam_free_heap(void)
90
{
91
        int record = 0;
92
        JAMS_HEAP_RECORD *heap_ptr = NULL;
93
        JAMS_HEAP_RECORD *tmp_heap_ptr = NULL;
94
 
95
        if ((jam_heap != NULL) && (jam_workspace == NULL))
96
        {
97
                heap_ptr = jam_heap;
98
                for (record = 0; record < jam_heap_records; ++record)
99
                {
100
                        if (heap_ptr != NULL)
101
                        {
102
                                tmp_heap_ptr = heap_ptr;
103
                                heap_ptr = heap_ptr->next;
104
                                jam_free(tmp_heap_ptr);
105
                        }
106
                }
107
        }
108
}
109
 
110
/****************************************************************************/
111
/*                                                                                                                                                      */
112
 
113
JAM_RETURN_TYPE jam_add_heap_record
114
(
115
        JAMS_SYMBOL_RECORD *symbol_record,
116
        JAMS_HEAP_RECORD **heap_record,
117
        long dimension
118
)
119
 
120
/*                                                                                                                                                      */
121
/*      Description:    Adds a heap record of the specified size to the heap.   */
122
/*                                                                                                                                                      */
123
/*      Returns:                JAMC_SUCCESS for success, or JAMC_OUT_OF_MEMORY if not  */
124
/*                                      enough memory was available.                                                    */
125
/*                                                                                                                                                      */
126
/****************************************************************************/
127
{
128
        int count = 0;
129
        int element = 0;
130
        long space_needed = 0L;
131
        BOOL cached = FALSE;
132
        JAMS_HEAP_RECORD *heap_ptr = NULL;
133
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
134
 
135
        /*
136
        *       Compute space needed for array or cache buffer.  Initialized arrays
137
        *       will not be cached if their size is less than the cache buffer size.
138
        */
139
        switch (symbol_record->type)
140
        {
141
        case JAM_INTEGER_ARRAY_WRITABLE:
142
                space_needed = dimension * sizeof(long);
143
                break;
144
 
145
        case JAM_BOOLEAN_ARRAY_WRITABLE:
146
                space_needed = ((dimension >> 5) + ((dimension & 0x1f) ? 1 : 0)) *
147
                        sizeof(long);
148
                break;
149
 
150
        case JAM_INTEGER_ARRAY_INITIALIZED:
151
                space_needed = dimension * sizeof(long);
152
/*              if (space_needed > JAMC_ARRAY_CACHE_SIZE)       */
153
/*              {                                                                                       */
154
/*                      space_needed = JAMC_ARRAY_CACHE_SIZE;   */
155
/*                      cached = TRUE;                                                  */
156
/*              }                                                                                       */
157
                break;
158
 
159
        case JAM_BOOLEAN_ARRAY_INITIALIZED:
160
                space_needed = ((dimension >> 5) + ((dimension & 0x1f) ? 1 : 0)) *
161
                        sizeof(long);
162
/*              if (space_needed > JAMC_ARRAY_CACHE_SIZE)       */
163
/*              {                                                                                       */
164
/*                      space_needed = JAMC_ARRAY_CACHE_SIZE;   */
165
/*                      cached = TRUE;                                                  */
166
/*              }                                                                                       */
167
                break;
168
 
169
        case JAM_PROCEDURE_BLOCK:
170
                space_needed = ((dimension >> 2) + 1) * sizeof(long);
171
                break;
172
 
173
        default:
174
                status = JAMC_INTERNAL_ERROR;
175
                break;
176
        }
177
 
178
        /*
179
        *       Check if there is enough space
180
        */
181
        if (status == JAMC_SUCCESS)
182
        {
183
                if (jam_workspace != NULL)
184
                {
185
                        heap_ptr = (JAMS_HEAP_RECORD *) jam_heap_top;
186
 
187
                        jam_heap_top = (void *) ((long)heap_ptr +
188
                                (long)sizeof(JAMS_HEAP_RECORD) + space_needed);
189
 
190
                        if ((long)jam_heap_top > (long)jam_symbol_bottom)
191
                        {
192
                                status = JAMC_OUT_OF_MEMORY;
193
                        }
194
                }
195
                else
196
                {
197
#if PORT==DOS
198
                        if ((sizeof(JAMS_HEAP_RECORD) + space_needed) < 0x10000L)
199
                        {
200
                                heap_ptr = (JAMS_HEAP_RECORD *) jam_malloc((unsigned int)
201
                                        (sizeof(JAMS_HEAP_RECORD) + space_needed));
202
                        }
203
                        /* else error: cannot allocate a buffer greater than 64K */
204
#else
205
                        heap_ptr = (JAMS_HEAP_RECORD *) jam_malloc((unsigned int)
206
                                (sizeof(JAMS_HEAP_RECORD) + space_needed));
207
#endif
208
 
209
                        if (heap_ptr == NULL)
210
                        {
211
                                status = JAMC_OUT_OF_MEMORY;
212
                        }
213
                        else if (jam_heap == NULL)
214
                        {
215
                                jam_heap = heap_ptr;
216
                        }
217
                }
218
        }
219
 
220
        /*
221
        *       Add the new record to the heap
222
        */
223
        if (status == JAMC_SUCCESS)
224
        {
225
                heap_ptr->symbol_record = symbol_record;
226
                heap_ptr->dimension = dimension;
227
                heap_ptr->cached = cached;
228
                heap_ptr->position = 0L;
229
 
230
                if (jam_workspace != NULL)
231
                {
232
                        /* point next pointer to position of next block */
233
                        heap_ptr->next = (JAMS_HEAP_RECORD *) jam_heap_top;
234
                }
235
                else
236
                {
237
                        /* add new heap block to beginning of list */
238
                        heap_ptr->next = jam_heap;
239
                        jam_heap = heap_ptr;
240
                }
241
 
242
                /* initialize data area to zero */
243
                count = (int) (space_needed / sizeof(long));
244
                for (element = 0; element < count; ++element)
245
                {
246
                        heap_ptr->data[element] = 0L;
247
                }
248
 
249
                ++jam_heap_records;
250
 
251
                *heap_record = heap_ptr;
252
        }
253
 
254
        return (status);
255
}
256
 
257
/****************************************************************************/
258
/*                                                                                                                                                      */
259
 
260
void *jam_get_temp_workspace
261
(
262
        long size
263
)
264
 
265
/*                                                                                                                                                      */
266
/*      Description:    Gets a pointer to the unused area of the heap for               */
267
/*                                      temporary use.  This area will be used for heap records */
268
/*                                      if jam_add_heap_record() is called.                                             */
269
/*                                                                                                                                                      */
270
/*      Returns:                pointer to memory, or NULL if memory not available              */
271
/*                                                                                                                                                      */
272
/****************************************************************************/
273
{
274
        void *temp_workspace = NULL;
275
 
276
        if (jam_workspace != NULL)
277
        {
278
                if (((long)jam_heap_top) + size <= (long)jam_symbol_bottom)
279
                {
280
                        temp_workspace = jam_heap_top;
281
                }
282
        }
283
        else
284
        {
285
                temp_workspace = jam_malloc((unsigned int) size);
286
        }
287
 
288
        return (temp_workspace);
289
}
290
 
291
/****************************************************************************/
292
/*                                                                                                                                                      */
293
 
294
void jam_free_temp_workspace
295
(
296
        void *ptr
297
)
298
 
299
/*                                                                                                                                                      */
300
/*      Description:    Frees memory buffer allocated by jam_get_temp_workspace */
301
/*                                                                                                                                                      */
302
/*      Returns:                Nothing                                                                                                 */
303
/*                                                                                                                                                      */
304
/****************************************************************************/
305
{
306
        if ((ptr != NULL) && (jam_workspace == NULL))
307
        {
308
                jam_free(ptr);
309
        }
310
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.