ref: 352ab3f5f8eceb576afdfccbde7ab7cf93f69433
parent: fdccc6ded272018cf4c5ca040041d3a938b5debd
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Tue Oct 29 00:11:18 EDT 2024
more cleanup; replace all alloc/free with LLT_-prefixed macros
--- a/3rd/mp/mpaux.c
+++ b/3rd/mp/mpaux.c
@@ -75,13 +75,13 @@
return;
} else {
if(b->p == (mpdigit*)&b[1]){
- b->p = (mpdigit*)malloc(n*Dbytes);
+ b->p = (mpdigit*)LLT_ALLOC(n*Dbytes);
if(b->p == nil)
sysfatal("mpbits: %r");
memmove(b->p, &b[1], Dbytes*b->top);
memset(&b[1], 0, Dbytes*b->size);
} else {
- b->p = (mpdigit*)realloc(b->p, n*Dbytes);
+ b->p = (mpdigit*)LLT_REALLOC(b->p, n*Dbytes);
if(b->p == nil)
sysfatal("mpbits: %r");
}
@@ -101,8 +101,8 @@
sysfatal("freeing mp constant");
memset(b->p, 0, b->size*Dbytes);
if(b->p != (mpdigit*)&b[1])
- free(b->p);
- free(b);
+ LLT_FREE(b->p);
+ LLT_FREE(b);
}
mpint*
--- a/3rd/mp/mpfmt.c
+++ b/3rd/mp/mpfmt.c
@@ -11,7 +11,7 @@
if(n < 0)
return -1;
rv = (*enc)(buf, len, p, n);
- free(p);
+ LLT_FREE(p);
return rv;
}
@@ -158,7 +158,7 @@
for(rv=1; (base >> rv) > 1; rv++)
;
len = 10 + (b->top*Dbits / rv);
- buf = malloc(len);
+ buf = LLT_ALLOC(len);
if(buf == nil)
return nil;
alloced = 1;
@@ -200,7 +200,7 @@
}
if(rv < 0){
if(alloced)
- free(buf);
+ LLT_FREE(buf);
return nil;
}
return buf;
--- a/3rd/mp/mpmul.c
+++ b/3rd/mp/mpmul.c
@@ -90,7 +90,7 @@
mpvecadd(res+n, reslen-n, diffprod, u0len+v0len, res+n);
memmove(p, res, (alen+blen)*Dbytes);
- free(t);
+ LLT_FREE(t);
}
#define KARATSUBAMIN 32
--- a/3rd/mp/mptobe.c
+++ b/3rd/mp/mptobe.c
@@ -13,7 +13,7 @@
m++;
if(p == nil){
n = m;
- p = malloc(n);
+ p = LLT_ALLOC(n);
if(p == nil)
sysfatal("mptobe: %r");
} else {
--- a/bitvector-ops.c
+++ b/bitvector-ops.c
@@ -331,7 +331,7 @@
return;
nw = (offs+nbits+31)>>5;
- temp = (nw > MALLOC_CUTOFF) ? malloc(nw*4) : a;
+ temp = (nw > MALLOC_CUTOFF) ? LLT_ALLOC(nw*4) : a;
for(i = 0; i < nw/2; i++){
temp[i] = bitreverse(b[nw-i-1]);
temp[nw-i-1] = bitreverse(b[i]);
@@ -342,7 +342,7 @@
tail = (offs+nbits)&31;
bitvector_copy(b, offs, temp, (32-tail)&31, nbits);
if(nw > MALLOC_CUTOFF)
- free(temp);
+ LLT_FREE(temp);
}
uint64_t
@@ -445,7 +445,7 @@
{ \
uint32_t nw = (doffs+nbits+31)>>5; \
uint32_t atmp[MALLOC_CUTOFF+1]; \
- uint32_t *temp = nw>MALLOC_CUTOFF ? malloc((nw+1)*4) : atmp; \
+ uint32_t *temp = nw>MALLOC_CUTOFF ? LLT_ALLOC((nw+1)*4) : atmp; \
uint32_t i, anw, bnw; \
if(aoffs == boffs){ \
anw = (aoffs+nbits+31)>>5; \
@@ -466,7 +466,7 @@
temp[i] = OP(a[i], b[i]); \
bitvector_copy(dest, doffs, temp, aoffs, nbits); \
if(nw>MALLOC_CUTOFF) \
- free(temp); \
+ LLT_FREE(temp); \
}
#define BV_AND(a, b) ((a)&(b))
--- a/bitvector.h
+++ b/bitvector.h
@@ -1,3 +1,6 @@
+// a mask with n set lo or hi bits
+#define lomask(n) (uint32_t)((((uint32_t)1)<<(n))-1)
+
uint32_t bitreverse(uint32_t x);
uint32_t *bitvector_new(uint64_t n, int initzero);
uint32_t *bitvector_resize(uint32_t *b, uint64_t oldsz, uint64_t newsz, int initzero);
--- a/cvalues.c
+++ b/cvalues.c
@@ -4,6 +4,7 @@
#include "cvalues.h"
#include "types.h"
#include "overflows.h"
+#include "iostream.h"
// trigger unconditional GC after this many bytes are allocated
#define ALLOC_LIMIT_TRIGGER 67108864
@@ -43,7 +44,7 @@
{
if(nfinalizers == maxfinalizers){
size_t nn = maxfinalizers == 0 ? 256 : maxfinalizers*2;
- cvalue_t **temp = realloc(Finalizers, nn*sizeof(cvalue_t*));
+ cvalue_t **temp = LLT_REALLOC(Finalizers, nn*sizeof(cvalue_t*));
if(temp == nil)
lerrorf(MemoryError, "out of memory");
Finalizers = temp;
@@ -74,7 +75,7 @@
t->vtable->finalize(tagptr(tmp, TAG_CVALUE));
if(!isinlined(tmp) && owned(tmp)){
memset(cv_data(tmp), 0xbb, cv_len(tmp));
- free(cv_data(tmp));
+ LLT_FREE(cv_data(tmp));
}
ndel++;
}
@@ -152,7 +153,7 @@
gc(0);
pcv = alloc_words(CVALUE_NWORDS);
pcv->type = type;
- pcv->data = malloc(sz);
+ pcv->data = LLT_ALLOC(sz);
autorelease(pcv);
malloc_pressure += sz;
}
@@ -238,7 +239,7 @@
size_t sz = cv_len(cv);
if(cv_isstr(cv))
sz++;
- void *data = malloc(sz);
+ void *data = LLT_ALLOC(sz);
memmove(data, cv_data(cv), sz);
cv->data = data;
autorelease(cv);
@@ -638,8 +639,6 @@
lerrorf(ArgError, "invalid c type");
}
-extern fltype_t *iostreamtype;
-
// get pointer and size for any plain-old-data value
void
to_sized_ptr(value_t v, char **pdata, size_t *psz)
@@ -736,7 +735,7 @@
size_t len = cv_len(cv);
if(cv_isstr(cv))
len++;
- ncv->data = malloc(len);
+ ncv->data = LLT_ALLOC(len);
memmove(ncv->data, cv_data(cv), len);
autorelease(ncv);
if(hasparent(cv)){
--- a/equal.c
+++ b/equal.c
@@ -377,7 +377,7 @@
if(cv->type == mpinttype){
len = mptobe(*(mpint**)data, nil, 0, (uint8_t**)&data);
h = memhash(data, len);
- free(data);
+ LLT_FREE(data);
}else{
h = memhash(data, cv_len(cv));
}
--- a/flisp.c
+++ b/flisp.c
@@ -282,6 +282,27 @@
}
char *
+uint2str(char *dest, size_t len, uint64_t num, uint32_t base)
+{
+ int i = len-1;
+ uint64_t b = (uint64_t)base;
+ char ch;
+ dest[i--] = '\0';
+ while(i >= 0){
+ ch = (char)(num % b);
+ if(ch < 10)
+ ch += '0';
+ else
+ ch = ch-10+'a';
+ dest[i--] = ch;
+ num /= b;
+ if(num == 0)
+ break;
+ }
+ return &dest[i+1];
+}
+
+char *
symbol_name(value_t v)
{
if(ismanaged(v)){
@@ -563,7 +584,7 @@
grow_stack(void)
{
size_t newsz = N_STACK * 2;
- value_t *ns = realloc(Stack, newsz*sizeof(value_t));
+ value_t *ns = LLT_REALLOC(Stack, newsz*sizeof(value_t));
if(ns == nil)
lerrorf(MemoryError, "stack overflow");
Stack = ns;
@@ -2062,9 +2083,6 @@
{
int i;
- llt_init();
- setlocale(LC_NUMERIC, "C");
-
heapsize = initial_heapsize;
fromspace = LLT_ALLOC(heapsize);
@@ -2075,7 +2093,7 @@
htable_new(&printconses, 32);
comparehash_init();
N_STACK = 262144;
- Stack = malloc(N_STACK*sizeof(value_t));
+ Stack = LLT_ALLOC(N_STACK*sizeof(value_t));
FL_NIL = NIL = builtin(OP_THE_EMPTY_LIST);
FL_T = builtin(OP_BOOL_CONST_T);
--- a/flisp.h
+++ b/flisp.h
@@ -230,6 +230,8 @@
value_t mk_cons(void);
void *alloc_words(int n);
+char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base);
+
/* error handling */
typedef struct _fl_readstate_t {
htable_t backrefs;
--- a/flmain.c
+++ b/flmain.c
@@ -2,7 +2,13 @@
#include "flisp.h"
#include "cvalues.h"
#include "print.h"
+#include "iostream.h"
+#include "ieee754.h"
+#include "random.h"
+double D_PNAN, D_NNAN, D_PINF, D_NINF;
+float F_PNAN, F_NNAN, F_PINF, F_NINF;
+
static value_t
argv_list(int argc, char *argv[])
{
@@ -18,22 +24,28 @@
return lst;
}
-extern fltype_t *iostreamtype;
-
int
flmain(const char *boot, int bootsz, int argc, char **argv)
{
- value_t f;
- ios_t *s;
- int r;
+ D_PNAN = D_NNAN = strtod("+NaN", nil);
+ D_PINF = D_NINF = strtod("+Inf", nil);
+ union ieee754_double *d;
+ d = (union ieee754_double *)&D_NNAN;
+ d->ieee.negative = 1;
+ d = (union ieee754_double *)&D_NINF;
+ d->ieee.negative = 1;
+
+ randomize();
+ ios_init_stdstreams();
+
fl_init(512*1024);
- f = cvalue(iostreamtype, sizeof(ios_t));
- s = value2c(ios_t*, f);
+ value_t f = cvalue(iostreamtype, sizeof(ios_t));
+ ios_t *s = value2c(ios_t*, f);
ios_static_buffer(s, boot, bootsz);
- r = 1;
+ int r = 1;
FL_TRY_EXTERN{
if(fl_load_system_image(f) == 0){
fl_applyn(1, symbol_value(symbol("__start")), argv_list(argc, argv));
--- a/ios.c
+++ b/ios.c
@@ -891,7 +891,7 @@
if((c = vsnprintf(buf, sizeof(buf), format, args)) < nelem(buf))
str = buf;
else{
- str = malloc(c+1);
+ str = LLT_ALLOC(c+1);
vsnprintf(str, sizeof(c+1), format, args);
}
if(c > 0)
@@ -898,7 +898,7 @@
#endif
ios_write(s, str, c);
if(str != buf)
- free(str);
+ LLT_FREE(str);
return c;
}
--- a/iostream.h
+++ b/iostream.h
@@ -1,2 +1,4 @@
+extern fltype_t *iostreamtype;
+
value_t stream_to_string(value_t *ps);
void iostream_init(void);
--- a/llt.c
+++ /dev/null
@@ -1,66 +1,0 @@
-#include "llt.h"
-#include "random.h"
-#include "ieee754.h"
-
-double D_PNAN, D_NNAN, D_PINF, D_NINF;
-float F_PNAN, F_NNAN, F_PINF, F_NINF;
-
-void
-llt_init(void)
-{
- D_PNAN = D_NNAN = strtod("+NaN", nil);
- D_PINF = D_NINF = strtod("+Inf", nil);
-
- union ieee754_double *d;
- d = (union ieee754_double *)&D_NNAN;
- d->ieee.negative = 1;
- d = (union ieee754_double *)&D_NINF;
- d->ieee.negative = 1;
-
-#ifdef __plan9__
- *(uint32_t*)&F_PNAN = 0x7fc00000;
- *(uint32_t*)&F_NNAN = 0xffc00000;
- *(uint32_t*)&F_PINF = 0x7f800000;
- *(uint32_t*)&F_NINF = 0xff800000;
-#else
- union ieee754_float *f;
- F_PNAN = F_NNAN = strtof("+NaN", nil);
- F_PINF = F_NINF = strtof("+Inf", nil);
- f = (union ieee754_float *)&F_NNAN;
- f->ieee.negative = 1;
- f = (union ieee754_float *)&F_NINF;
- f->ieee.negative = 1;
-#endif
-
- randomize();
- ios_init_stdstreams();
-}
-
-char *
-uint2str(char *dest, size_t len, uint64_t num, uint32_t base)
-{
- int i = len-1;
- uint64_t b = (uint64_t)base;
- char ch;
- dest[i--] = '\0';
- while(i >= 0){
- ch = (char)(num % b);
- if(ch < 10)
- ch += '0';
- else
- ch = ch-10+'a';
- dest[i--] = ch;
- num /= b;
- if(num == 0)
- break;
- }
- return &dest[i+1];
-}
-
-int
-isdigit_base(char c, int base)
-{
- if(base < 11)
- return c >= '0' && c < '0'+base;
- return (c >= '0' && c <= '9') || (c >= 'a' && c < 'a'+base-10) || (c >= 'A' && c < 'A'+base-10);
-}
--- a/llt.h
+++ b/llt.h
@@ -8,33 +8,6 @@
#include "htableh.inc"
HTPROT(ptrhash)
-#ifdef __GNUC__
-#define __unlikely(x) __builtin_expect(!!(x), 0)
-#define __likely(x) __builtin_expect(!!(x), 1)
-#else
-#define __unlikely(x) (x)
-#define __likely(x) (x)
-#endif
-
-#ifdef BOEHM_GC /* boehm GC allocator */
-#include <gc.h>
-#define LLT_ALLOC(n) GC_MALLOC(n)
-#define LLT_REALLOC(p, n) GC_REALLOC((p), (n))
-#define LLT_FREE(x) USED(x)
-#else /* standard allocator */
-#define LLT_ALLOC(n) malloc(n)
-#define LLT_REALLOC(p, n) realloc((p), (n))
-#define LLT_FREE(x) free(x)
-#endif
-
-#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
-#define bswap_32(x) \
- ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
- (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
-#define bswap_64(x) \
- (uint64_t)bswap_32((x) & 0xffffffffULL)<<32 | \
- (uint64_t)bswap_32(((x)>>32) & 0xffffffffULL)
-
#define DBL_MAXINT (1LL<<53)
#define FLT_MAXINT (1<<24)
#define BIT63 0x8000000000000000ULL
@@ -41,12 +14,10 @@
#define BIT31 0x80000000UL
#ifdef BITS64
-#define NBITS 64
#define TOP_BIT BIT63
typedef uint64_t lltuint_t;
typedef int64_t lltint_t;
#else
-#define NBITS 32
#define TOP_BIT BIT31
typedef uint32_t lltuint_t;
typedef int32_t lltint_t;
@@ -54,18 +25,9 @@
#define LOG2_10 3.3219280948873626
#define rel_zero(a, b) (fabs((a)/(b)) < DBL_EPSILON)
-#define LABS(n) (((n)^((n)>>(NBITS-1))) - ((n)>>(NBITS-1)))
-#define NBABS(n, nb) (((n)^((n)>>((nb)-1))) - ((n)>>((nb)-1)))
#define LLT_ALIGN(x, sz) (((x) + (sz-1)) & (-sz))
-// a mask with n set lo or hi bits
-#define lomask(n) (uint32_t)((((uint32_t)1)<<(n))-1)
-
extern double D_PNAN, D_NNAN, D_PINF, D_NINF;
extern float F_PNAN, F_NNAN, F_PINF, F_NINF;
-
-char *uint2str(char *dest, size_t len, uint64_t num, uint32_t base);
-int isdigit_base(char c, int base);
-void llt_init(void);
int flmain(const char *boot, int bootsz, int argc, char **argv);
--- a/main_plan9.c
+++ b/main_plan9.c
@@ -1,8 +1,8 @@
#include "llt.h"
-static const char boot[] = {
+static char boot[] =
#include "flisp.boot.h"
-};
+;
void
main(int argc, char **argv)
@@ -10,5 +10,11 @@
argv0 = argv[0];
setfcr(FPPDBL|FPRNR|FPOVFL);
tmfmtinstall();
+
+ *(uint32_t*)&F_PNAN = 0x7fc00000;
+ *(uint32_t*)&F_NNAN = 0xffc00000;
+ *(uint32_t*)&F_PINF = 0x7f800000;
+ *(uint32_t*)&F_NINF = 0xff800000;
+
exit(flmain(boot, sizeof(boot), argc, argv));
}
--- a/main_posix.c
+++ b/main_posix.c
@@ -1,4 +1,5 @@
#include "llt.h"
+#include "ieee754.h"
static const char boot[] =
#include "flisp.boot.h"
@@ -7,5 +8,15 @@
int
main(int argc, char **argv)
{
+ union ieee754_float *f;
+ F_PNAN = F_NNAN = strtof("+NaN", nil);
+ F_PINF = F_NINF = strtof("+Inf", nil);
+ f = (union ieee754_float *)&F_NNAN;
+ f->ieee.negative = 1;
+ f = (union ieee754_float *)&F_NINF;
+ f->ieee.negative = 1;
+
+ setlocale(LC_NUMERIC, "C");
+
return flmain(boot, sizeof(boot), argc, argv);
}
--- a/meson.build
+++ b/meson.build
@@ -68,7 +68,6 @@
'htable.c',
'ios.c',
'iostream.c',
- 'llt.c',
'main_posix.c',
'operators.c',
'print.c',
--- a/mkfile
+++ b/mkfile
@@ -25,7 +25,6 @@
htable.$O\
ios.$O\
iostream.$O\
- llt.$O\
main_plan9.$O\
operators.$O\
print.$O\
--- a/plan9/platform.h
+++ b/plan9/platform.h
@@ -3,6 +3,10 @@
#include <ctype.h>
#include <mp.h>
+#define LLT_ALLOC(n) malloc(n)
+#define LLT_REALLOC(p, n) realloc((p), (n))
+#define LLT_FREE(x) free(x)
+
#if defined(__amd64__) || \
defined(__arm64__) || \
defined(__mips64__) || \
@@ -36,9 +40,6 @@
#define signbit(r) ((*(uint64_t*)&(r)) & BIT63)
#define isfinite(d) (((*(uint64_t*)&(d))&0x7ff0000000000000ULL) != 0x7ff0000000000000ULL)
-#define LC_NUMERIC 0
-#define setlocale(x, y)
-
#define PRId64 "lld"
#define PRIu64 "llud"
#ifdef BITS64
@@ -86,6 +87,17 @@
#else
#define BYTE_ORDER LITTLE_ENDIAN
#endif
+
+#define __unlikely(x) (x)
+#define __likely(x) (x)
+
+#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
+#define bswap_32(x) \
+ ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
+ (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
+#define bswap_64(x) \
+ (uint64_t)bswap_32((x) & 0xffffffffULL)<<32 | \
+ (uint64_t)bswap_32(((x)>>32) & 0xffffffffULL)
typedef s8int int8_t;
typedef s16int int16_t;
--- a/posix/platform.h
+++ b/posix/platform.h
@@ -25,6 +25,10 @@
#include <wctype.h>
#include <wchar.h>
+#define LLT_ALLOC(n) malloc(n)
+#define LLT_REALLOC(p, n) realloc((p), (n))
+#define LLT_FREE(x) free(x)
+
#ifndef __SIZEOF_POINTER__
#error pointer size unknown
#elif __SIZEOF_POINTER__ == 8
@@ -35,6 +39,23 @@
#define nil NULL
#define USED(x) ((void)(x))
#define nelem(x) (int)(sizeof(x)/sizeof((x)[0]))
+
+#ifdef __GNUC__
+#define __unlikely(x) __builtin_expect(!!(x), 0)
+#define __likely(x) __builtin_expect(!!(x), 1)
+#endif
+
+#ifdef HAVE_BYTESWAP
+#include <byteswap.h>
+#else
+#define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
+#define bswap_32(x) \
+ ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
+ (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
+#define bswap_64(x) \
+ (uint64_t)bswap_32((x) & 0xffffffffULL)<<32 | \
+ (uint64_t)bswap_32(((x)>>32) & 0xffffffffULL)
+#endif
#define PATHSEP '/'
#define PATHSEPSTRING "/"
--- a/print.c
+++ b/print.c
@@ -725,7 +725,7 @@
HPOS += ios_printf(f, "%s", s);
else
HPOS += ios_printf(f, "#%s(%s)", symbol_name(type), s);
- free(s);
+ LLT_FREE(s);
}else if(issymbol(type)){
// handle other integer prims. we know it's smaller than uint64
// at this point, so int64 is big enough to capture everything.
--- a/read.c
+++ b/read.c
@@ -11,6 +11,8 @@
TOK_OPENC, TOK_CLOSEC,
};
+static value_t do_read_sexpr(value_t label);
+
#if defined(__plan9__)
static int errno;
#define VLONG_MAX ~(1LL<<63)
@@ -243,7 +245,13 @@
return issym;
}
-static value_t do_read_sexpr(value_t label);
+static int
+isdigit_base(char c, int base)
+{
+ if(base < 11)
+ return c >= '0' && c < '0'+base;
+ return (c >= '0' && c <= '9') || (c >= 'a' && c < 'a'+base-10) || (c >= 'A' && c < 'A'+base-10);
+}
static uint32_t
peek(void)
@@ -484,13 +492,13 @@
value_t s;
Rune r = 0;
- buf = malloc(sz);
+ buf = LLT_ALLOC(sz);
while(1){
if(i >= sz-4){ // -4: leaves room for longest utf8 sequence
sz *= 2;
- temp = realloc(buf, sz);
+ temp = LLT_REALLOC(buf, sz);
if(temp == nil){
- free(buf);
+ LLT_FREE(buf);
lerrorf(ParseError, "out of memory reading string");
}
buf = temp;
@@ -497,7 +505,7 @@
}
c = ios_getc(F);
if(c == IOS_EOF){
- free(buf);
+ LLT_FREE(buf);
lerrorf(ParseError, "unexpected end of input in string");
}
if(c == '"')
@@ -505,7 +513,7 @@
else if(c == '\\'){
c = ios_getc(F);
if(c == IOS_EOF){
- free(buf);
+ LLT_FREE(buf);
lerrorf(ParseError, "end of input in escape sequence");
}
j = 0;
@@ -533,7 +541,7 @@
if(j)
r = strtol(eseq, nil, 16);
if(!j || r > Runemax){
- free(buf);
+ LLT_FREE(buf);
lerrorf(ParseError, "invalid escape sequence");
}
if(ndig == 2)
@@ -543,7 +551,7 @@
}else{
char esc = read_escape_control_char((char)c);
if(esc == (char)c && !strchr("\\'\"`", esc)){
- free(buf);
+ LLT_FREE(buf);
lerrorf(ParseError, "invalid escape sequence: \\%c", (char)c);
}
buf[i++] = esc;
@@ -554,7 +562,7 @@
}
s = cvalue_string(i);
memmove(cvalue_data(s), buf, i);
- free(buf);
+ LLT_FREE(buf);
return s;
}
--- a/types.c
+++ b/types.c
@@ -40,7 +40,7 @@
if(isarray){
fltype_t *eltype = get_type(car_(cdr_(t)));
if(eltype->size == 0){
- free(ft);
+ LLT_FREE(ft);
lerrorf(ArgError, "invalid array element type");
}
ft->elsz = eltype->size;