-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutil.c
646 lines (523 loc) · 14.3 KB
/
util.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
* util.c - commonly used utility functions.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <inttypes.h>
#include <time.h>
#include <math.h>
#ifdef __amigaos__
#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <inline/exec.h>
#include <inline/dos.h>
#include <inline/intuition.h>
extern struct ExecBase *SysBase;
extern struct DOSBase *DOSBase;
extern struct IntuitionBase *IntuitionBase;
#endif
#include "util.h"
#include "options.h"
#include "logger.h"
#define CHUNK_DEFAULT_SIZE 8 * 1024
typedef struct U_memChunk_ *U_memChunk;
typedef struct U_memPool_ *U_memPool;
typedef struct U_memRec_ *U_memRec;
struct U_memChunk_
{
void *mem_start, *mem_next;
size_t avail;
U_memChunk next;
};
struct U_memPool_
{
size_t chunk_size;
int num_chunks;
int num_allocs;
U_memChunk first, last;
// for allocs >chunk_size / nonchunk mallocs:
U_memRec mem;
size_t mem_alloced;
};
struct U_memRec_
{
U_memRec next;
size_t size;
void *mem;
};
static char *g_pool_names[UP_numPools] = { "FRONTEND", "TYPES", "TEMP", "ASSEM", "CODEGEN", "ENV", "FLOWGRAPH", "LINSCAN", "SYMBOL",
"REGALLOC", "LIVENESS", "LINK", "IDE", "OPTIONS", "RUN_CHILD" };
static U_memPool g_pools[UP_numPools] = { NULL, NULL };
static float g_start_time;
float U_getTime(void)
{
#ifdef __amigaos__
struct DateStamp datetime;
DateStamp(&datetime);
return datetime.ds_Minute * 60.0 + datetime.ds_Tick / 50.0;
#else
clock_t t = clock();
return ((float)t)/CLOCKS_PER_SEC;
#endif
}
static void *checked_malloc (size_t len)
{
#ifdef __amigaos__
void *p = AllocMem(len, 0);
#else
void *p = malloc(len);
#endif
if (!p)
{
LOG_printf(LOG_ERROR, "\nran out of memory!\n");
exit(1);
}
// fprintf(stderr, "checked_malloc len=%zu -> p=%p\n", len, p);
return p;
}
static void U_memfree (void *mem, size_t size)
{
#ifdef __amigaos__
FreeMem (mem, size);
#else
free(mem);
#endif
}
static U_memChunk U_MemChunk(size_t chunk_size)
{
U_memChunk chunk = checked_malloc(sizeof(*chunk));
chunk->mem_start = chunk->mem_next = checked_malloc (chunk_size);
chunk->avail = chunk_size;
chunk->next = NULL;
return chunk;
}
static void U_memPoolInit (U_memPool pool, size_t chunk_size)
{
pool->chunk_size = chunk_size;
pool->first = pool->last = U_MemChunk(chunk_size);
pool->num_chunks = 1;
pool->num_allocs = 0;
pool->mem = NULL;
pool->mem_alloced = 0;
}
static U_memPool U_MemPool(size_t chunk_size)
{
U_memPool pool = checked_malloc(sizeof(*pool));
U_memPoolInit (pool, chunk_size);
return pool;
}
void *U_poolNonChunkAlloc (U_poolId pid, size_t size)
{
U_memPool pool = g_pools[pid];
assert (pool);
U_memRec mem_prev = pool->mem;
pool->mem = checked_malloc (sizeof(*pool->mem));
pool->mem->mem = checked_malloc (size);
pool->mem->size = size;
pool->mem->next = mem_prev;
pool->mem_alloced += size;
return pool->mem->mem;
}
void *U_poolNonChunkCAlloc (U_poolId pid, size_t size)
{
void *p = U_poolNonChunkAlloc(pid, size);
memset (p, 0, size);
return p;
}
void *U_poolAlloc (U_poolId pid, size_t size)
{
U_memPool pool = g_pools[pid];
assert (pool);
if (size > pool->chunk_size)
return U_poolNonChunkAlloc(pid, size);
// alignment
size += size % 2;
U_memChunk chunk = pool->last;
if (chunk->avail < size)
{
chunk = pool->last = pool->last->next = U_MemChunk(pool->chunk_size);
pool->num_chunks++;
}
void *mem = chunk->mem_next;
chunk->mem_next += size;
chunk->avail -= size;
pool->num_allocs++;
return mem;
}
void *U_poolCalloc (U_poolId pid, size_t nmemb, size_t len)
{
void *p = U_poolAlloc(pid, nmemb * len);
memset (p, 0, nmemb * len);
return p;
}
static void U_poolFree (U_poolId pid, bool destroy)
{
U_memPool pool = g_pools[pid];
LOG_printf (OPT_get(OPTION_VERBOSE) ? LOG_INFO : LOG_DEBUG, "freeing memory pool: %-12s %5d allocs, %6zd bytes in %2d chunks + %6zd non-chunked bytes.\n", g_pool_names[pid], pool->num_allocs, (pool->num_chunks * pool->chunk_size) / 1, pool->num_chunks, pool->mem_alloced);
//U_delay(1000);
U_memChunk nextChunk = pool->first->next;
for (U_memChunk chunk = pool->first; chunk; chunk = nextChunk)
{
nextChunk = chunk->next;
//LOG_printf (OPT_get(OPTION_VERBOSE) ? LOG_INFO : LOG_DEBUG, "freeing memory pool: freeing chunk %d bytes at 0x%08lx\n", pool->chunk_size, chunk->mem_start);
//U_delay(1000);
U_memfree(chunk->mem_start, pool->chunk_size);
U_memfree(chunk, sizeof (*chunk));
}
while (pool->mem)
{
U_memRec mem_next = pool->mem->next;
//LOG_printf (OPT_get(OPTION_VERBOSE) ? LOG_INFO : LOG_DEBUG, "freeing memory pool: freeing mem %d bytes at 0x%08lx\n", pool->mem->size, pool->mem->mem);
//U_delay(1000);
U_memfree (pool->mem->mem, pool->mem->size);
U_memfree (pool->mem, sizeof (*pool->mem));
pool->mem = mem_next;
}
if (destroy)
U_memfree(pool, sizeof (*pool));
LOG_printf (OPT_get(OPTION_VERBOSE) ? LOG_INFO : LOG_DEBUG, "freeing memory pool: done.\n");
//U_delay(1000);
}
void U_poolReset (U_poolId pid)
{
U_poolFree(pid, /*destroy=*/FALSE);
U_memPool pool = g_pools[pid];
U_memPoolInit (pool, pool->chunk_size);
}
void U_memstat(void)
{
float t = U_getTime();
double tdiff = t-g_start_time;
for (int i=0; i<UP_numPools; i++)
{
LOG_printf (OPT_get(OPTION_VERBOSE) ? LOG_INFO : LOG_DEBUG, "%5dms: mpool: %-12s %4d allocs, %6zd bytes in %2d chunks + %6zd non-chunked bytes.\n",
(int)(tdiff * 1000.0), g_pool_names[i], g_pools[i]->num_allocs, (g_pools[i]->num_chunks * g_pools[i]->chunk_size) / 1, g_pools[i]->num_chunks, g_pools[i]->mem_alloced);
}
}
string String(U_poolId pid, const char *s)
{
string p = U_poolAlloc (pid, strlen(s)+1);
strcpy(p,s);
return p;
}
string strconcat(U_poolId pid, const char *s1, const char*s2)
{
int l1 = strlen(s1);
int l2 = strlen(s2);
char *buf = U_poolAlloc (pid, l1+l2+1);
memcpy(buf, s1, l1);
memcpy(buf+l1, s2, l2);
buf[l1+l2] = 0;
return buf;
}
string strlower(U_poolId pid, const char *s)
{
int l = strlen(s);
string p = U_poolAlloc (pid, l+1);
for (int i = 0; i<l; i++)
{
p[i] = tolower(s[i]);
}
p[l]=0;
return p;
}
#define MAXBUF 1024
string strprintf(U_poolId pid, const char *format, ...)
{
char buf[MAXBUF];
va_list args;
int l;
char *res;
va_start (args, format);
vsnprintf (buf, MAXBUF, format, args);
va_end (args);
l = strlen(buf);
res = U_poolAlloc (pid, l+1);
res[l] = 0;
memcpy(res, buf, l);
return res;
}
int strcicmp(string a, string b)
{
for (;; a++, b++) {
int d = tolower(*a) - tolower(*b);
if (d != 0 || !*a)
return d;
}
}
void strserialize(FILE *out, string str)
{
uint16_t l = strlen(str);
uint16_t l_swapped = ENDIAN_SWAP_16 (l);
fwrite(&l_swapped, 2, 1, out);
fwrite(str, l, 1, out);
}
string strdeserialize(U_poolId pid, FILE *in)
{
uint16_t l;
if (fread(&l, 2, 1, in) != 1)
return NULL;
l = ENDIAN_SWAP_16 (l);
string res = U_poolAlloc (pid, l+1);
if (fread(res, l, 1, in) != 1)
return NULL;
res[l]=0;
// LOG_printf (LOG_DEBUG, "deserialized string l=%d, res=%s\n", l, res);
return res;
}
bool str2int(string str, int *i)
{
assert (str);
int res = 0;
char *c = str;
bool negative = FALSE;
bool first = TRUE;
while (*c)
{
if (first && (*c=='-'))
{
negative = TRUE;
c++;
continue;
}
switch (*c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
res = res*10 + (*c)-'0';
break;
case '\r':
case '\n':
break;
default:
return FALSE;
}
c++;
}
if (negative)
res *= -1;
*i = res;
return TRUE;
}
uint32_t encode_ffp(float f)
{
uint32_t res, fl;
uint32_t *fp = (uint32_t *) &f;
fl = *fp;
if (f==0.0)
return 0;
// exponent
res = (fl & 0x7F800000) >> 23;
res = res - 126 + 0x40;
// overflow
if ((char) res < 0)
return res;
// mantissa
res |= (fl << 8) | 0x80000000;
// sign
if (f < 0)
res |= 0x00000080;
return res;
}
float decode_ffp(uint32_t fl)
{
uint32_t res;
if (fl == 0)
return 0.0;
// exponent
// 0x 7 F 8 0 0 0 0 0
// 0000 0000 0000 0000 0000 0000 Seee eeee (FFP )
// 0eee eeee e000 0000 0000 0000 0000 0000 (IEEE)
res = (fl & 0x0000007f) + 126 - 0x40;
res = res << 23;
// printf ("exponent converted: 0x%08x\n", res);
// mantissa
// 1mmm mmmm mmmm mmmm mmmm mmmm Seee eeee (FFP )
// 0eee eeee emmm mmmm mmmm mmmm mmmm mmmm (IEEE)
res |= ((fl & 0x7fffff00) >> 8);
// printf ("mantissa converted: 0x%08x\n", res);
// sign
if (fl & 0x80)
res |= 0x80000000;
// printf ("sign converted: 0x%08x\n", res);
float *fp = (float*) &res;
return *fp;
}
void U_float2str(double v, char *buffer, int buf_len)
{
const char * buffer_stop = &buffer[buf_len-1];
char * buffer_start = buffer;
const char * digit_encoding = "0123456789abcdef";
const int radix = 10;
char *output_buffer = buffer_start;
int precision = 6;
if (isinf(v))
{
if (v < 0)
(*output_buffer++) = '-';
strcpy(output_buffer,"inf");
}
else if (isnan(v))
{
strcpy(output_buffer,"nan");
}
else
{
double roundoff_fudge = 0.0;
int num_leading_digits;
int max_digits = -1;
int digit;
if (v < 0.0)
{
(*output_buffer++) = '-';
v = (-v);
}
roundoff_fudge = pow((double)radix,(double)(precision + 1));
if(roundoff_fudge > 0.0)
v += 5.0 / roundoff_fudge;
// compute number of leading digits
double v2 = v;
if (v2 < radix)
{
num_leading_digits = 1;
}
else
{
num_leading_digits = 0;
while(floor(v2) > 0.0)
{
num_leading_digits++;
v2 /= radix;
}
}
if (v >= 1.0)
{
/* 'Normalize' the number so that we have a zero in
front of the mantissa. We can't lose here: we
simply scale the value without any loss of
precision (we just change the floating point
exponent). */
v /= pow((double)radix,(double)num_leading_digits);
for (int i = 0 ; (max_digits != 0) && (i < num_leading_digits) && (output_buffer < buffer_stop) ; i++)
{
v *= radix;
digit = floor(v);
(*output_buffer++) = digit_encoding[digit];
v -= digit;
if(max_digits > 0)
max_digits--;
}
}
else
{
/* NOTE: any 'significant' digits (for %g conversion)
will follow the decimal point. */
(*output_buffer++) = '0';
}
/* Now for the fractional part. */
if (output_buffer < buffer_stop)
{
(*output_buffer++) = '.';
int i;
for (i = 0 ; (i < precision) && (output_buffer < buffer_stop) ; i++)
{
v *= radix;
digit = floor(v);
(*output_buffer++) = digit_encoding[digit];
v -= digit;
}
/* Strip trailing digits and decimal point */
while (output_buffer > buffer_start+2 && output_buffer[-1] == '0' && output_buffer[-2] != '.')
output_buffer--;
//if(output_buffer > buffer_start && output_buffer[-1] == '.')
// output_buffer--;
}
*output_buffer = '\0';
}
}
void U_delay (uint16_t millis)
{
#ifdef __amigaos__
Delay (millis / 20);
#endif
}
#ifdef __amigaos__
#define MAX_BODY_LINES 20
bool U_request (struct Window *win, char *posTxt, char *negTxt, char* format, ...)
{
assert (negTxt);
static struct IntuiText itBody[MAX_BODY_LINES];
static struct IntuiText itPos = { 0, 0, 0, 6, 3, NULL, NULL, NULL };
static struct IntuiText itNeg = { 0, 0, 0, 6, 3, NULL, NULL, NULL };
static char buf[1024];
va_list args;
va_start(args, format);
vsnprintf (buf, 1024, format, args);
va_end(args);
char *s = buf;
char *c = buf;
uint16_t i=0;
bool finished = FALSE;
while (!finished && (i<MAX_BODY_LINES))
{
if ( (*c=='\n') || (*c==0) )
{
finished = *c==0;
*c = 0;
if (i)
itBody[i-1].NextText = &itBody[i];
itBody[i].FrontPen = 1,
itBody[i].BackPen = 0;
itBody[i].DrawMode = 0;
itBody[i].LeftEdge = 6;
itBody[i].TopEdge = 3 + i*8;
itBody[i].ITextFont = NULL;
itBody[i].IText = (STRPTR)s;
itBody[i].NextText = NULL;
i++;
c++;
s=c;
}
else
{
c++;
}
}
bool res = FALSE;
itNeg.IText = (STRPTR) negTxt;
itPos.IText = (STRPTR) posTxt;
res = AutoRequest(win, &itBody[0], posTxt ? &itPos : NULL, &itNeg, 0, 0, 640, 22 + i*8);
return res;
}
#endif
void U_deinit (void)
{
LOG_printf (OPT_get(OPTION_VERBOSE) ? LOG_INFO : LOG_DEBUG, "U_deinit.\n");
for (int i=0; i<UP_numPools; i++)
{
U_poolFree(i, /*destroy=*/TRUE);
}
LOG_printf (OPT_get(OPTION_VERBOSE) ? LOG_INFO : LOG_DEBUG, "U_deinit done.\n");
//U_delay(1000);
}
void U_init (void)
{
g_start_time = U_getTime();
for (int i=0; i<UP_numPools; i++)
g_pools[i] = U_MemPool (CHUNK_DEFAULT_SIZE);
}